|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Deprecations; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\StaticPropertyFetch; |
| 7 | +use PhpParser\Node\Identifier; |
| 8 | +use PhpParser\Node\Name; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Broker\Broker; |
| 11 | +use PHPStan\Reflection\DeprecatableReflection; |
| 12 | +use PHPStan\Rules\RuleLevelHelper; |
| 13 | +use PHPStan\Type\ErrorType; |
| 14 | +use PHPStan\Type\Type; |
| 15 | + |
| 16 | +class AccessDeprecatedStaticPropertyRule implements \PHPStan\Rules\Rule |
| 17 | +{ |
| 18 | + |
| 19 | + /** @var Broker */ |
| 20 | + private $broker; |
| 21 | + |
| 22 | + /** @var RuleLevelHelper */ |
| 23 | + private $ruleLevelHelper; |
| 24 | + |
| 25 | + public function __construct(Broker $broker, RuleLevelHelper $ruleLevelHelper) |
| 26 | + { |
| 27 | + $this->broker = $broker; |
| 28 | + $this->ruleLevelHelper = $ruleLevelHelper; |
| 29 | + } |
| 30 | + |
| 31 | + public function getNodeType(): string |
| 32 | + { |
| 33 | + return StaticPropertyFetch::class; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @param StaticPropertyFetch $node |
| 38 | + * @param \PHPStan\Analyser\Scope $scope |
| 39 | + * @return string[] errors |
| 40 | + */ |
| 41 | + public function processNode(Node $node, Scope $scope): array |
| 42 | + { |
| 43 | + if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { |
| 44 | + return []; |
| 45 | + } |
| 46 | + |
| 47 | + if (!$node->name instanceof Identifier) { |
| 48 | + return []; |
| 49 | + } |
| 50 | + |
| 51 | + $propertyName = $node->name->name; |
| 52 | + $referencedClasses = []; |
| 53 | + |
| 54 | + if ($node->class instanceof Name) { |
| 55 | + $referencedClasses[] = (string) $node->class; |
| 56 | + } else { |
| 57 | + $classTypeResult = $this->ruleLevelHelper->findTypeToCheck( |
| 58 | + $scope, |
| 59 | + $node->class, |
| 60 | + '', // We don't care about the error message |
| 61 | + function (Type $type) use ($propertyName) { |
| 62 | + return $type->canAccessProperties()->yes() && $type->hasProperty($propertyName); |
| 63 | + } |
| 64 | + ); |
| 65 | + |
| 66 | + if ($classTypeResult->getType() instanceof ErrorType) { |
| 67 | + return []; |
| 68 | + } |
| 69 | + |
| 70 | + $referencedClasses = $classTypeResult->getReferencedClasses(); |
| 71 | + } |
| 72 | + |
| 73 | + foreach ($referencedClasses as $referencedClass) { |
| 74 | + try { |
| 75 | + $class = $this->broker->getClass($referencedClass); |
| 76 | + $property = $class->getProperty($propertyName, $scope); |
| 77 | + } catch (\PHPStan\Broker\ClassNotFoundException $e) { |
| 78 | + continue; |
| 79 | + } catch (\PHPStan\Reflection\MissingPropertyFromReflectionException $e) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + if ($property instanceof DeprecatableReflection && $property->isDeprecated()) { |
| 84 | + return [sprintf( |
| 85 | + 'Access to deprecated static property $%s of class %s.', |
| 86 | + $propertyName, |
| 87 | + $referencedClass |
| 88 | + )]; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return []; |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments