Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 90 additions & 2 deletions build/psalm/OcpSinceChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event): voi
$classLike = $event->getStmt();
$statementsSource = $event->getStatementsSource();

self::checkClassComment($classLike, $statementsSource);
if (!str_contains($statementsSource->getFilePath(), '/lib/public/')) {
return;
}

$isTesting = str_contains($statementsSource->getFilePath(), '/lib/public/Notification/')
|| str_contains($statementsSource->getFilePath(), 'CalendarEventStatus');

if ($isTesting) {
self::checkStatementAttributes($classLike, $statementsSource);
} else {
self::checkClassComment($classLike, $statementsSource);
}

foreach ($classLike->stmts as $stmt) {
if ($stmt instanceof ClassConst) {
Expand All @@ -32,11 +43,64 @@ public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event): voi
}

if ($stmt instanceof EnumCase) {
self::checkStatementComment($stmt, $statementsSource, 'enum');
if ($isTesting) {
self::checkStatementAttributes($classLike, $statementsSource);
} else {
self::checkStatementComment($stmt, $statementsSource, 'enum');
}
}
}
}

private static function checkStatementAttributes(ClassLike $stmt, FileSource $statementsSource): void {
$hasAppFrameworkAttribute = false;
$mustBeConsumable = false;
$isConsumable = false;
foreach ($stmt->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if (in_array($attr->name->getLast(), [
'Catchable',
'Consumable',
'Dispatchable',
'Implementable',
'Listenable',
'Throwable',
], true)) {
$hasAppFrameworkAttribute = true;
self::checkAttributeHasValidSinceVersion($attr, $statementsSource);
}
if (in_array($attr->name->getLast(), [
'Catchable',
'Consumable',
'Listenable',
], true)) {
$isConsumable = true;
}
if ($attr->name->getLast() === 'ExceptionalImplementable') {
$mustBeConsumable = true;
}
}
}

if ($mustBeConsumable && !$isConsumable) {
IssueBuffer::maybeAdd(
new InvalidDocblock(
'Attribute OCP\\AppFramework\\Attribute\\ExceptionalImplementable is only valid on classes that also have OCP\\AppFramework\\Attribute\\Consumable',
new CodeLocation($statementsSource, $stmt)
)
);
}

if (!$hasAppFrameworkAttribute) {
IssueBuffer::maybeAdd(
new InvalidDocblock(
'At least one of the OCP\\AppFramework\\Attribute attributes is required',
new CodeLocation($statementsSource, $stmt)
)
);
}
}

private static function checkClassComment(ClassLike $stmt, FileSource $statementsSource): void {
$docblock = $stmt->getDocComment();

Expand Down Expand Up @@ -124,4 +188,28 @@ private static function checkStatementComment(Stmt $stmt, FileSource $statements
);
}
}

private static function checkAttributeHasValidSinceVersion(\PhpParser\Node\Attribute $stmt, FileSource $statementsSource): void {
foreach ($stmt->args as $arg) {
if ($arg->name?->name === 'since') {
if (!$arg->value instanceof \PhpParser\Node\Scalar\String_) {
IssueBuffer::maybeAdd(
new InvalidDocblock(
'Attribute since argument is not a valid version string',
new CodeLocation($statementsSource, $stmt)
)
);
} else {
if (!preg_match('/^[1-9][0-9]*(\.[0-9]+){0,3}$/', $arg->value->value)) {
IssueBuffer::maybeAdd(
new InvalidDocblock(
'Attribute since argument is not a valid version string',
new CodeLocation($statementsSource, $stmt)
)
);
}
}
}
}
}
}
8 changes: 8 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@
'OCP\\Activity\\ISetting' => $baseDir . '/lib/public/Activity/ISetting.php',
'OCP\\AppFramework\\ApiController' => $baseDir . '/lib/public/AppFramework/ApiController.php',
'OCP\\AppFramework\\App' => $baseDir . '/lib/public/AppFramework/App.php',
'OCP\\AppFramework\\Attribute\\ASince' => $baseDir . '/lib/public/AppFramework/Attribute/ASince.php',
'OCP\\AppFramework\\Attribute\\Catchable' => $baseDir . '/lib/public/AppFramework/Attribute/Catchable.php',
'OCP\\AppFramework\\Attribute\\Consumable' => $baseDir . '/lib/public/AppFramework/Attribute/Consumable.php',
'OCP\\AppFramework\\Attribute\\Dispatchable' => $baseDir . '/lib/public/AppFramework/Attribute/Dispatchable.php',
'OCP\\AppFramework\\Attribute\\ExceptionalImplementable' => $baseDir . '/lib/public/AppFramework/Attribute/ExceptionalImplementable.php',
'OCP\\AppFramework\\Attribute\\Implementable' => $baseDir . '/lib/public/AppFramework/Attribute/Implementable.php',
'OCP\\AppFramework\\Attribute\\Listenable' => $baseDir . '/lib/public/AppFramework/Attribute/Listenable.php',
'OCP\\AppFramework\\Attribute\\Throwable' => $baseDir . '/lib/public/AppFramework/Attribute/Throwable.php',
'OCP\\AppFramework\\AuthPublicShareController' => $baseDir . '/lib/public/AppFramework/AuthPublicShareController.php',
'OCP\\AppFramework\\Bootstrap\\IBootContext' => $baseDir . '/lib/public/AppFramework/Bootstrap/IBootContext.php',
'OCP\\AppFramework\\Bootstrap\\IBootstrap' => $baseDir . '/lib/public/AppFramework/Bootstrap/IBootstrap.php',
Expand Down
8 changes: 8 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Activity\\ISetting' => __DIR__ . '/../../..' . '/lib/public/Activity/ISetting.php',
'OCP\\AppFramework\\ApiController' => __DIR__ . '/../../..' . '/lib/public/AppFramework/ApiController.php',
'OCP\\AppFramework\\App' => __DIR__ . '/../../..' . '/lib/public/AppFramework/App.php',
'OCP\\AppFramework\\Attribute\\ASince' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Attribute/ASince.php',
'OCP\\AppFramework\\Attribute\\Catchable' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Attribute/Catchable.php',
'OCP\\AppFramework\\Attribute\\Consumable' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Attribute/Consumable.php',
'OCP\\AppFramework\\Attribute\\Dispatchable' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Attribute/Dispatchable.php',
'OCP\\AppFramework\\Attribute\\ExceptionalImplementable' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Attribute/ExceptionalImplementable.php',
'OCP\\AppFramework\\Attribute\\Implementable' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Attribute/Implementable.php',
'OCP\\AppFramework\\Attribute\\Listenable' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Attribute/Listenable.php',
'OCP\\AppFramework\\Attribute\\Throwable' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Attribute/Throwable.php',
'OCP\\AppFramework\\AuthPublicShareController' => __DIR__ . '/../../..' . '/lib/public/AppFramework/AuthPublicShareController.php',
'OCP\\AppFramework\\Bootstrap\\IBootContext' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Bootstrap/IBootContext.php',
'OCP\\AppFramework\\Bootstrap\\IBootstrap' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Bootstrap/IBootstrap.php',
Expand Down
34 changes: 34 additions & 0 deletions lib/public/AppFramework/Attribute/ASince.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCP\AppFramework\Attribute;

use Attribute;

/**
* Abstract base attribute to declare an API's stability.
*
* @since 32.0.0
*/
#[Consumable(since: '32.0.0')]
abstract class ASince {
/**
* @param string $since For shipped apps and server code such as core/ and lib/,
* this should be the server version. For other apps it
* should be the semantic app version.
*/
public function __construct(
protected string $since,
) {
}

public function getSince(): string {
return $this->since;
}
}
23 changes: 23 additions & 0 deletions lib/public/AppFramework/Attribute/Catchable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCP\AppFramework\Attribute;

use Attribute;

/**
* Attribute to declare that the exception is "catchable" by apps.
*
* @since 32.0.0
*/
#[Attribute(Attribute::TARGET_ALL | Attribute::IS_REPEATABLE)]
#[Consumable(since: '32.0.0')]
#[Implementable(since: '32.0.0')]
class Catchable extends ASince {
}
27 changes: 27 additions & 0 deletions lib/public/AppFramework/Attribute/Consumable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCP\AppFramework\Attribute;

use Attribute;

/**
* Attribute to declare that the API stability is limited to "consuming" the
* class, interface, enum, etc. Apps are not allowed to implement or replace them.
*
* For events use @see \OCP\AppFramework\Attribute\Listenable
* For exceptions use @see \OCP\AppFramework\Attribute\Catchable
*
* @since 32.0.0
*/
#[Attribute(Attribute::TARGET_ALL | Attribute::IS_REPEATABLE)]
#[Consumable(since: '32.0.0')]
#[Implementable(since: '32.0.0')]
class Consumable extends ASince {
}
23 changes: 23 additions & 0 deletions lib/public/AppFramework/Attribute/Dispatchable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCP\AppFramework\Attribute;

use Attribute;

/**
* Attribute to declare that the event is "dispatchable" by apps.
*
* @since 32.0.0
*/
#[Attribute(Attribute::TARGET_ALL | Attribute::IS_REPEATABLE)]
#[Consumable(since: '32.0.0')]
#[Implementable(since: '32.0.0')]
class Dispatchable extends ASince {
}
38 changes: 38 additions & 0 deletions lib/public/AppFramework/Attribute/ExceptionalImplementable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCP\AppFramework\Attribute;

use Attribute;

/**
* Attribute to declare that the API marked as Consumable/Listenable/Catchable
* has an exception and is Implementable/Dispatchable/Throwable by a dedicated
* app. Changes to such an API have to be communicated to the affected app maintainers.
*
* @since 32.0.0
*/
#[Attribute(Attribute::TARGET_ALL | Attribute::IS_REPEATABLE)]
#[Consumable(since: '32.0.0')]
#[Implementable(since: '32.0.0')]
class ExceptionalImplementable {
public function __construct(
protected string $app,
protected ?string $class = null,
) {
}

public function getApp(): string {
return $this->app;
}

public function getClass(): ?string {
return $this->class;
}
}
27 changes: 27 additions & 0 deletions lib/public/AppFramework/Attribute/Implementable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCP\AppFramework\Attribute;

use Attribute;

/**
* Attribute to declare that the API stability is limited to "implementing" the
* class, interface, enum, etc.
*
* For events use @see \OCP\AppFramework\Attribute\Dispatchable
* For exceptions use @see \OCP\AppFramework\Attribute\Throwable
*
* @since 32.0.0
*/
#[Attribute(Attribute::TARGET_ALL | Attribute::IS_REPEATABLE)]
#[Consumable(since: '32.0.0')]
#[Implementable(since: '32.0.0')]
class Implementable extends ASince {
}
23 changes: 23 additions & 0 deletions lib/public/AppFramework/Attribute/Listenable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCP\AppFramework\Attribute;

use Attribute;

/**
* Attribute to declare that the event is "listenable" by apps.
*
* @since 32.0.0
*/
#[Attribute(Attribute::TARGET_ALL | Attribute::IS_REPEATABLE)]
#[Consumable(since: '32.0.0')]
#[Implementable(since: '32.0.0')]
class Listenable extends ASince {
}
23 changes: 23 additions & 0 deletions lib/public/AppFramework/Attribute/Throwable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCP\AppFramework\Attribute;

use Attribute;

/**
* Attribute to declare that the exception is "throwable" by apps.
*
* @since 32.0.0
*/
#[Attribute(Attribute::TARGET_ALL | Attribute::IS_REPEATABLE)]
#[Consumable(since: '32.0.0')]
#[Implementable(since: '32.0.0')]
class Throwable extends ASince {
}
8 changes: 3 additions & 5 deletions lib/public/Calendar/CalendarEventStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
*/
namespace OCP\Calendar;

/**
* The status of a calendar event.
*
* @since 32.0.0
*/
use OCP\AppFramework\Attribute\Listenable;

#[Listenable(since: '32.0.0')]
enum CalendarEventStatus: string {
case TENTATIVE = 'TENTATIVE';
case CONFIRMED = 'CONFIRMED';
Expand Down
6 changes: 3 additions & 3 deletions lib/public/Notification/AlreadyProcessedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
*/
namespace OCP\Notification;

/**
* @since 17.0.0
*/
use OCP\AppFramework\Attribute\Throwable;

#[Throwable(since: '17.0.0')]
class AlreadyProcessedException extends \RuntimeException {
/**
* @since 17.0.0
Expand Down
Loading
Loading