Skip to content

Commit

Permalink
Support all Expr nodes when in dynamic constant fetch (#248)
Browse files Browse the repository at this point in the history
Because besides `Variable`, there's also
- `PropertyFetch` (`Foo::{$this->type}`)
- `StaticPropertyFetch` (`Foo::{Foo::$bar}`)
- and more.

Fix #241, again.
  • Loading branch information
spaze authored Jan 22, 2024
2 parents ce98abe + 43e9213 commit fe56632
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/Usages/ClassConstantUsages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}


Expand Down
4 changes: 4 additions & 0 deletions tests/src/Blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
5 changes: 5 additions & 0 deletions tests/src/disallowed/constantDynamicUsages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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};

0 comments on commit fe56632

Please sign in to comment.