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
27 changes: 27 additions & 0 deletions src/Rules/Api/ApiClassConstFetchRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Reflection\ClassConstantReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand Down Expand Up @@ -60,6 +61,12 @@ public function processNode(Node $node, Scope $scope): array
'https://github.com/phpstan/phpstan/discussions',
))->build();

if ($node->name->toLowerString() !== 'class' && $classReflection->hasConstant($node->name->toString())) {
if ($this->isConstantCovered($classReflection->getConstant($node->name->toString()))) {
return [];
}
}

$docBlock = $classReflection->getResolvedPhpDoc();
if ($docBlock !== null) {
foreach ($docBlock->getPhpDocNodes() as $phpDocNode) {
Expand Down Expand Up @@ -88,4 +95,24 @@ public function processNode(Node $node, Scope $scope): array
return [$ruleError];
}

private function isConstantCovered(ClassConstantReflection $constantReflection): bool
{
$classDocBlock = $constantReflection->getDeclaringClass()->getResolvedPhpDoc();
if ($classDocBlock !== null) {
foreach ($classDocBlock->getPhpDocNodes() as $phpDocNode) {
$apiTags = $phpDocNode->getTagsByName('@api');
if (count($apiTags) > 0) {
return true;
}
}
}

$constantDocComment = $constantReflection->getDocComment();
if ($constantDocComment === null) {
return false;
}

return str_contains($constantDocComment, '@api');
}

}
10 changes: 10 additions & 0 deletions src/Rules/Api/ApiTraitUseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function count;
use function sprintf;

/**
Expand Down Expand Up @@ -47,6 +48,15 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

$docBlock = $traitReflection->getResolvedPhpDoc();
if ($docBlock !== null) {
foreach ($docBlock->getPhpDocNodes() as $phpDocNode) {
if (count($phpDocNode->getTagsByName('@api')) > 0) {
continue 2;
}
}
}

$errors[] = RuleErrorBuilder::message(sprintf(
'Using %s is not covered by backward compatibility promise. The trait might change in a minor PHPStan version.',
$traitReflection->getDisplayName(),
Expand Down
2 changes: 2 additions & 0 deletions src/Turbo/TurboExtensionEnabler.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ private static function isCompatible(): bool
/**
* Whether activateIfCompatible() declared the native classes — the
* shadowed class names resolve to them only in that case.
*
* @api
*/
public static function isActive(): bool
{
Expand Down
8 changes: 8 additions & 0 deletions src/Turbo/TurboExtensionSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ final class TurboExtensionSelector
*/
public const MINIMUM_PHP_VERSION_ID = 80300;

/** @api */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ApiClassConstFetchRule needs to be improved to actually point out BC-covered and not-covered constants.

Feel free to also improve ApiTraitUseRule. LateResolvableTypeTrait should gain @api, the rule shouldn't report it, but using other traits should.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in f9e96a0. The constant rule now accepts @api on the constant or on its declaring class, as the method rules do. The trait rule accepts @api on the trait, and LateResolvableTypeTrait has it now.

public const EXTENSION_PATH_INI = 'phpstan.turboExtensionPath';

/**
Expand All @@ -75,6 +76,13 @@ public static function getRestartExtensionPath(): ?string
return $path;
}

/**
* The extension path a spawned worker needs as `-d extension=`, along with
* `-d` EXTENSION_PATH_INI with the same path. Null when the worker gets the
* extension from the php.ini it inherits, or when there is no extension.
*
* @api
*/
public static function findExtensionForWorkers(?string $pharPath = null): ?string
{
if (TurboExtensionEnabler::isLoaded()) {
Expand Down
1 change: 1 addition & 0 deletions src/Type/Traits/LateResolvableTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;

/** @api */
trait LateResolvableTypeTrait
{

Expand Down
9 changes: 7 additions & 2 deletions tests/PHPStan/Rules/Api/ApiClassConstFetchRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ public function testRuleOutOfPhpStan(): void
$this->analyse([__DIR__ . '/data/class-const-fetch-out-of-phpstan.php'], [
[
'Accessing PHPStan\Command\AnalyseCommand::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.',
16,
19,
$tip,
],
[
'Accessing PHPStan\Analyser\NodeScopeResolver::FOO is not covered by backward compatibility promise. The class might change in a minor PHPStan version.',
20,
23,
$tip,
],
[
'Accessing PHPStan\Turbo\TurboExtensionSelector::MINIMUM_PHP_VERSION_ID is not covered by backward compatibility promise. The class might change in a minor PHPStan version.',
28,
$tip,
],
]);
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Api/ApiTraitUseRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testRuleOutOfPhpStan(): void
$this->analyse([__DIR__ . '/data/trait-use-out-of-phpstan.php'], [
[
'Using PHPStan\Type\JustNullableTypeTrait is not covered by backward compatibility promise. The trait might change in a minor PHPStan version.',
15,
16,
$tip,
],
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace App\ClassConstFetch;

use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\ScopeFactory;
use PHPStan\Command\AnalyseCommand;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Turbo\TurboExtensionSelector;

class Foo
{
Expand All @@ -18,6 +21,11 @@ public function doFoo()
echo ScopeFactory::class;

echo NodeScopeResolver::FOO;

echo Scope::SUPERGLOBAL_VARIABLES;
echo MutatingScope::SUPERGLOBAL_VARIABLES;
echo TurboExtensionSelector::EXTENSION_PATH_INI;
echo TurboExtensionSelector::MINIMUM_PHP_VERSION_ID;
}

}
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Api/data/trait-use-out-of-phpstan.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\TraitUse;

use PHPStan\Type\JustNullableTypeTrait;
use PHPStan\Type\Traits\LateResolvableTypeTrait;

trait FooTrait
{
Expand All @@ -16,3 +17,10 @@ class Foo
use FooTrait;

}

class Bar
{

use LateResolvableTypeTrait;

}
Loading