|
5 | 5 | use PhpParser\Node; |
6 | 6 | use PHPStan\Analyser\Scope; |
7 | 7 | use PHPStan\DependencyInjection\RegisteredRule; |
| 8 | +use PHPStan\Reflection\ClassConstantReflection; |
8 | 9 | use PHPStan\Reflection\ReflectionProvider; |
9 | 10 | use PHPStan\Rules\Rule; |
10 | 11 | use PHPStan\Rules\RuleErrorBuilder; |
@@ -60,6 +61,12 @@ public function processNode(Node $node, Scope $scope): array |
60 | 61 | 'https://github.com/phpstan/phpstan/discussions', |
61 | 62 | ))->build(); |
62 | 63 |
|
| 64 | + if ($node->name->toLowerString() !== 'class' && $classReflection->hasConstant($node->name->toString())) { |
| 65 | + if ($this->isConstantCovered($classReflection->getConstant($node->name->toString()))) { |
| 66 | + return []; |
| 67 | + } |
| 68 | + } |
| 69 | + |
63 | 70 | $docBlock = $classReflection->getResolvedPhpDoc(); |
64 | 71 | if ($docBlock !== null) { |
65 | 72 | foreach ($docBlock->getPhpDocNodes() as $phpDocNode) { |
@@ -88,4 +95,24 @@ public function processNode(Node $node, Scope $scope): array |
88 | 95 | return [$ruleError]; |
89 | 96 | } |
90 | 97 |
|
| 98 | + private function isConstantCovered(ClassConstantReflection $constantReflection): bool |
| 99 | + { |
| 100 | + $classDocBlock = $constantReflection->getDeclaringClass()->getResolvedPhpDoc(); |
| 101 | + if ($classDocBlock !== null) { |
| 102 | + foreach ($classDocBlock->getPhpDocNodes() as $phpDocNode) { |
| 103 | + $apiTags = $phpDocNode->getTagsByName('@api'); |
| 104 | + if (count($apiTags) > 0) { |
| 105 | + return true; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + $constantDocComment = $constantReflection->getDocComment(); |
| 111 | + if ($constantDocComment === null) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + return str_contains($constantDocComment, '@api'); |
| 116 | + } |
| 117 | + |
91 | 118 | } |
0 commit comments