Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support all Expr nodes when in dynamic constant fetch #248

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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};