Skip to content

Commit fe56632

Browse files
authored
Support all Expr nodes when in dynamic constant fetch (#248)
Because besides `Variable`, there's also - `PropertyFetch` (`Foo::{$this->type}`) - `StaticPropertyFetch` (`Foo::{Foo::$bar}`) - and more. Fix #241, again.
2 parents ce98abe + 43e9213 commit fe56632

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

src/Usages/ClassConstantUsages.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
use PhpParser\Node;
77
use PhpParser\Node\Expr\ClassConstFetch;
8-
use PhpParser\Node\Expr\Variable;
98
use PhpParser\Node\Identifier;
109
use PHPStan\Analyser\Scope;
1110
use PHPStan\Rules\Rule;
@@ -85,18 +84,15 @@ public function processNode(Node $node, Scope $scope): array
8584
if ($node->name instanceof Identifier) {
8685
return $this->getConstantRuleErrors($scope, (string)$node->name, $this->typeResolver->getType($node->class, $scope));
8786
}
88-
if ($node->name instanceof Variable) {
89-
$type = $scope->getType($node->name);
90-
$errors = [];
91-
foreach ($type->getConstantStrings() as $constantString) {
92-
$errors = array_merge(
93-
$errors,
94-
$this->getConstantRuleErrors($scope, $constantString->getValue(), $this->typeResolver->getType($node->class, $scope))
95-
);
96-
}
97-
return $errors;
87+
$type = $scope->getType($node->name);
88+
$errors = [];
89+
foreach ($type->getConstantStrings() as $constantString) {
90+
$errors = array_merge(
91+
$errors,
92+
$this->getConstantRuleErrors($scope, $constantString->getValue(), $this->typeResolver->getType($node->class, $scope))
93+
);
9894
}
99-
throw new ShouldNotHappenException(sprintf('$node->name should be %s but is %s', Identifier::class, get_class($node->name)));
95+
return $errors;
10096
}
10197

10298

tests/src/Blade.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class Blade
1414

1515
public const MOVIE = Blade::WESLEY;
1616

17+
public static $runner = 'RUNNER';
18+
19+
public $deckard = 'DECKARD';
20+
1721

1822
public function runner(int $everything = 0, bool $yes = false, string $roland = '303'): void
1923
{

tests/src/disallowed/constantDynamicUsages.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@
88
echo Blade::{$kind};
99
/** @var 'DECKARD'|'MOVIE'|'RUNNER' $kind2 */
1010
echo Blade::{$kind2};
11+
12+
$blade = new Blade();
13+
echo Blade::{Blade::$runner};
14+
echo Blade::{$blade::$runner};
15+
echo Blade::{$blade->deckard};

0 commit comments

Comments
 (0)