|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Api; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Reflection\ReflectionProvider; |
| 8 | +use PHPStan\Rules\Rule; |
| 9 | +use PHPStan\Rules\RuleErrorBuilder; |
| 10 | +use function count; |
| 11 | +use function sprintf; |
| 12 | + |
| 13 | +/** |
| 14 | + * @implements Rule<Node\Expr\ClassConstFetch> |
| 15 | + */ |
| 16 | +class ApiClassConstFetchRule implements Rule |
| 17 | +{ |
| 18 | + |
| 19 | + public function __construct( |
| 20 | + private ApiRuleHelper $apiRuleHelper, |
| 21 | + private ReflectionProvider $reflectionProvider, |
| 22 | + ) |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + public function getNodeType(): string |
| 27 | + { |
| 28 | + return Node\Expr\ClassConstFetch::class; |
| 29 | + } |
| 30 | + |
| 31 | + public function processNode(Node $node, Scope $scope): array |
| 32 | + { |
| 33 | + if (!$node->name instanceof Node\Identifier) { |
| 34 | + return []; |
| 35 | + } |
| 36 | + |
| 37 | + if (!$node->class instanceof Node\Name) { |
| 38 | + return []; |
| 39 | + } |
| 40 | + |
| 41 | + $className = $scope->resolveName($node->class); |
| 42 | + if (!$this->reflectionProvider->hasClass($className)) { |
| 43 | + return []; |
| 44 | + } |
| 45 | + |
| 46 | + $classReflection = $this->reflectionProvider->getClass($className); |
| 47 | + if (!$this->apiRuleHelper->isPhpStanCode($scope, $classReflection->getName(), $classReflection->getFileName())) { |
| 48 | + return []; |
| 49 | + } |
| 50 | + |
| 51 | + $ruleError = RuleErrorBuilder::message(sprintf( |
| 52 | + 'Accessing %s::%s is not covered by backward compatibility promise. The class might change in a minor PHPStan version.', |
| 53 | + $classReflection->getDisplayName(), |
| 54 | + $node->name->toString(), |
| 55 | + ))->tip(sprintf( |
| 56 | + "If you think it should be covered by backward compatibility promise, open a discussion:\n %s\n\n See also:\n https://phpstan.org/developing-extensions/backward-compatibility-promise", |
| 57 | + 'https://github.com/phpstan/phpstan/discussions', |
| 58 | + ))->build(); |
| 59 | + |
| 60 | + $docBlock = $classReflection->getResolvedPhpDoc(); |
| 61 | + if ($docBlock === null) { |
| 62 | + return [$ruleError]; |
| 63 | + } |
| 64 | + |
| 65 | + foreach ($docBlock->getPhpDocNodes() as $phpDocNode) { |
| 66 | + $apiTags = $phpDocNode->getTagsByName('@api'); |
| 67 | + if (count($apiTags) > 0) { |
| 68 | + return []; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return [$ruleError]; |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments