diff --git a/src/Rules/Properties/AccessStaticPropertiesRule.php b/src/Rules/Properties/AccessStaticPropertiesRule.php index 1fec283302..9a4d0ee397 100644 --- a/src/Rules/Properties/AccessStaticPropertiesRule.php +++ b/src/Rules/Properties/AccessStaticPropertiesRule.php @@ -9,8 +9,10 @@ use PHPStan\Reflection\ReflectionProvider; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassNameNodePair; +use PHPStan\Rules\RuleError; use PHPStan\Rules\RuleErrorBuilder; use PHPStan\Rules\RuleLevelHelper; +use PHPStan\Type\Constant\ConstantStringType; use PHPStan\Type\ErrorType; use PHPStan\Type\ObjectType; use PHPStan\Type\StringType; @@ -49,11 +51,30 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (!$node->name instanceof Node\VarLikeIdentifier) { - return []; + if ($node->name instanceof Node\VarLikeIdentifier) { + $names = [$node->name->name]; + } else { + $names = array_map(static function (ConstantStringType $type): string { + return $type->getValue(); + }, TypeUtils::getConstantStrings($scope->getType($node->name))); + } + + $errors = []; + foreach ($names as $name) { + $errors = array_merge($errors, $this->processSingleProperty($scope, $node, $name)); } - $name = $node->name->name; + return $errors; + } + + /** + * @param Scope $scope + * @param StaticPropertyFetch $node + * @param string $name + * @return RuleError[] + */ + private function processSingleProperty(Scope $scope, StaticPropertyFetch $node, string $name): array + { $messages = []; if ($node->class instanceof Name) { $class = (string) $node->class; diff --git a/tests/PHPStan/Rules/Properties/AccessStaticPropertiesInAssignRuleTest.php b/tests/PHPStan/Rules/Properties/AccessStaticPropertiesInAssignRuleTest.php index b115855c9d..214fa443d7 100644 --- a/tests/PHPStan/Rules/Properties/AccessStaticPropertiesInAssignRuleTest.php +++ b/tests/PHPStan/Rules/Properties/AccessStaticPropertiesInAssignRuleTest.php @@ -31,4 +31,14 @@ public function testRule(): void ]); } + public function testRuleExpressionNames(): void + { + $this->analyse([__DIR__ . '/data/properties-from-array-into-static-object.php'], [ + [ + 'Access to an undefined static property PropertiesFromArrayIntoStaticObject\Foo::$noop.', + 29, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Properties/AccessStaticPropertiesRuleTest.php b/tests/PHPStan/Rules/Properties/AccessStaticPropertiesRuleTest.php index d20ff3a36c..5890eb94d1 100644 --- a/tests/PHPStan/Rules/Properties/AccessStaticPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/AccessStaticPropertiesRuleTest.php @@ -186,14 +186,4 @@ public function testClassExists(): void $this->analyse([__DIR__ . '/data/static-properties-class-exists.php'], []); } - public function testRuleExpressionNames(): void - { - $this->analyse([__DIR__ . '/data/properties-from-array-into-static-object.php'], [ - [ - 'Cannot access static property $noop on PropertiesFromArrayIntoStaticObject\Foo.', - 29, - ], - ]); - } - }