Skip to content

Commit 6dda018

Browse files
committed
Implement property name as an expression in AccessStaticPropertiesRule
1 parent 265afac commit 6dda018

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

src/Rules/Properties/AccessStaticPropertiesRule.php

+24-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
use PHPStan\Reflection\ReflectionProvider;
1010
use PHPStan\Rules\ClassCaseSensitivityCheck;
1111
use PHPStan\Rules\ClassNameNodePair;
12+
use PHPStan\Rules\RuleError;
1213
use PHPStan\Rules\RuleErrorBuilder;
1314
use PHPStan\Rules\RuleLevelHelper;
15+
use PHPStan\Type\Constant\ConstantStringType;
1416
use PHPStan\Type\ErrorType;
1517
use PHPStan\Type\ObjectType;
1618
use PHPStan\Type\StringType;
@@ -49,11 +51,30 @@ public function getNodeType(): string
4951

5052
public function processNode(Node $node, Scope $scope): array
5153
{
52-
if (!$node->name instanceof Node\VarLikeIdentifier) {
53-
return [];
54+
if ($node->name instanceof Node\VarLikeIdentifier) {
55+
$names = [$node->name->name];
56+
} else {
57+
$names = array_map(static function (ConstantStringType $type): string {
58+
return $type->getValue();
59+
}, TypeUtils::getConstantStrings($scope->getType($node->name)));
60+
}
61+
62+
$errors = [];
63+
foreach ($names as $name) {
64+
$errors = array_merge($errors, $this->processSingleProperty($scope, $node, $name));
5465
}
5566

56-
$name = $node->name->name;
67+
return $errors;
68+
}
69+
70+
/**
71+
* @param Scope $scope
72+
* @param StaticPropertyFetch $node
73+
* @param string $name
74+
* @return RuleError[]
75+
*/
76+
private function processSingleProperty(Scope $scope, StaticPropertyFetch $node, string $name): array
77+
{
5778
$messages = [];
5879
if ($node->class instanceof Name) {
5980
$class = (string) $node->class;

tests/PHPStan/Rules/Properties/AccessStaticPropertiesInAssignRuleTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,14 @@ public function testRule(): void
3131
]);
3232
}
3333

34+
public function testRuleExpressionNames(): void
35+
{
36+
$this->analyse([__DIR__ . '/data/properties-from-array-into-static-object.php'], [
37+
[
38+
'Access to an undefined static property PropertiesFromArrayIntoStaticObject\Foo::$noop.',
39+
29,
40+
],
41+
]);
42+
}
43+
3444
}

tests/PHPStan/Rules/Properties/AccessStaticPropertiesRuleTest.php

-10
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,4 @@ public function testClassExists(): void
186186
$this->analyse([__DIR__ . '/data/static-properties-class-exists.php'], []);
187187
}
188188

189-
public function testRuleExpressionNames(): void
190-
{
191-
$this->analyse([__DIR__ . '/data/properties-from-array-into-static-object.php'], [
192-
[
193-
'Cannot access static property $noop on PropertiesFromArrayIntoStaticObject\Foo.',
194-
29,
195-
],
196-
]);
197-
}
198-
199189
}

0 commit comments

Comments
 (0)