|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Api; |
| 4 | + |
| 5 | +use PHPStan\Analyser\Scope; |
| 6 | +use PHPStan\DependencyInjection\AutowiredService; |
| 7 | +use PHPStan\Reflection\ClassConstantReflection; |
| 8 | +use PHPStan\Reflection\ClassReflection; |
| 9 | +use PHPStan\Rules\RestrictedUsage\RestrictedClassConstantUsageExtension; |
| 10 | +use PHPStan\Rules\RestrictedUsage\RestrictedUsage; |
| 11 | +use function count; |
| 12 | +use function sprintf; |
| 13 | +use function str_contains; |
| 14 | + |
| 15 | +#[AutowiredService] |
| 16 | +final class ApiClassConstantUsageExtension implements RestrictedClassConstantUsageExtension |
| 17 | +{ |
| 18 | + |
| 19 | + public function __construct(private ApiRuleHelper $apiRuleHelper) |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + public function isRestrictedClassConstantUsage( |
| 24 | + ClassConstantReflection $constantReflection, |
| 25 | + Scope $scope, |
| 26 | + ): ?RestrictedUsage |
| 27 | + { |
| 28 | + $declaringClass = $constantReflection->getDeclaringClass(); |
| 29 | + if (!$this->apiRuleHelper->isPhpStanCode($scope, $declaringClass->getName(), $declaringClass->getFileName())) { |
| 30 | + return null; |
| 31 | + } |
| 32 | + |
| 33 | + if ($this->isClassCovered($declaringClass)) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + if ($constantReflection->getName() === 'class') { |
| 38 | + // Foo::class is fine when the class has a method covered by the backward compatibility promise |
| 39 | + foreach ($declaringClass->getNativeReflection()->getMethods() as $methodReflection) { |
| 40 | + $methodDocComment = $methodReflection->getDocComment(); |
| 41 | + if ($methodDocComment !== false && str_contains($methodDocComment, '@api')) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + } |
| 45 | + } else { |
| 46 | + $constantDocComment = $constantReflection->getDocComment(); |
| 47 | + if ($constantDocComment !== null && str_contains($constantDocComment, '@api')) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return RestrictedUsage::create( |
| 53 | + sprintf( |
| 54 | + 'Accessing %s::%s is not covered by backward compatibility promise. The class might change in a minor PHPStan version.', |
| 55 | + $declaringClass->getDisplayName(), |
| 56 | + $constantReflection->getName(), |
| 57 | + ), |
| 58 | + 'phpstanApi.classConstant', |
| 59 | + sprintf( |
| 60 | + "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", |
| 61 | + 'https://github.com/phpstan/phpstan/discussions', |
| 62 | + ), |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + private function isClassCovered(ClassReflection $classReflection): bool |
| 67 | + { |
| 68 | + $docBlock = $classReflection->getResolvedPhpDoc(); |
| 69 | + if ($docBlock === null) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + foreach ($docBlock->getPhpDocNodes() as $phpDocNode) { |
| 74 | + if (count($phpDocNode->getTagsByName('@api')) > 0) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments