From 43e92136b454a867b972650a3609858fad1e7543 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Mon, 22 Jan 2024 23:04:43 +0100 Subject: [PATCH] Support all Expr nodes when in dynamic constant fetch Because besides `Variable`, there's also `PropertyFetch` (`Foo::{$this->type}`), `StaticPropertyFetch` (`Foo::{Foo::$bar}`) and more. Fix #241, again. --- src/Usages/ClassConstantUsages.php | 20 ++++++++----------- tests/src/Blade.php | 4 ++++ .../src/disallowed/constantDynamicUsages.php | 5 +++++ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Usages/ClassConstantUsages.php b/src/Usages/ClassConstantUsages.php index 841a24d..f29826e 100644 --- a/src/Usages/ClassConstantUsages.php +++ b/src/Usages/ClassConstantUsages.php @@ -5,7 +5,6 @@ use PhpParser\Node; use PhpParser\Node\Expr\ClassConstFetch; -use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; @@ -85,18 +84,15 @@ public function processNode(Node $node, Scope $scope): array if ($node->name instanceof Identifier) { return $this->getConstantRuleErrors($scope, (string)$node->name, $this->typeResolver->getType($node->class, $scope)); } - if ($node->name instanceof Variable) { - $type = $scope->getType($node->name); - $errors = []; - foreach ($type->getConstantStrings() as $constantString) { - $errors = array_merge( - $errors, - $this->getConstantRuleErrors($scope, $constantString->getValue(), $this->typeResolver->getType($node->class, $scope)) - ); - } - return $errors; + $type = $scope->getType($node->name); + $errors = []; + foreach ($type->getConstantStrings() as $constantString) { + $errors = array_merge( + $errors, + $this->getConstantRuleErrors($scope, $constantString->getValue(), $this->typeResolver->getType($node->class, $scope)) + ); } - throw new ShouldNotHappenException(sprintf('$node->name should be %s but is %s', Identifier::class, get_class($node->name))); + return $errors; } diff --git a/tests/src/Blade.php b/tests/src/Blade.php index 05faded..3c0f7ba 100644 --- a/tests/src/Blade.php +++ b/tests/src/Blade.php @@ -14,6 +14,10 @@ class Blade public const MOVIE = Blade::WESLEY; + public static $runner = 'RUNNER'; + + public $deckard = 'DECKARD'; + public function runner(int $everything = 0, bool $yes = false, string $roland = '303'): void { diff --git a/tests/src/disallowed/constantDynamicUsages.php b/tests/src/disallowed/constantDynamicUsages.php index 45c5f4d..e617c05 100644 --- a/tests/src/disallowed/constantDynamicUsages.php +++ b/tests/src/disallowed/constantDynamicUsages.php @@ -8,3 +8,8 @@ echo Blade::{$kind}; /** @var 'DECKARD'|'MOVIE'|'RUNNER' $kind2 */ echo Blade::{$kind2}; + +$blade = new Blade(); +echo Blade::{Blade::$runner}; +echo Blade::{$blade::$runner}; +echo Blade::{$blade->deckard};