Skip to content

Commit

Permalink
Implement property name as an expression in AccessStaticPropertiesRule
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 18, 2020
1 parent 265afac commit 6dda018
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
27 changes: 24 additions & 3 deletions src/Rules/Properties/AccessStaticPropertiesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]);
}

}
10 changes: 0 additions & 10 deletions tests/PHPStan/Rules/Properties/AccessStaticPropertiesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]);
}

}

0 comments on commit 6dda018

Please sign in to comment.