Skip to content

Commit

Permalink
Fix ClassRegistryInit doesn't work with ClassConstFetch AST
Browse files Browse the repository at this point in the history
  • Loading branch information
sidz committed Jan 28, 2024
1 parent 45eb613 commit 6a7822b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/ClassRegistryInitExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PHPStanCakePHP2;

use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name;
use PHPStanCakePHP2\Service\SchemaService;
use Inflector;
use PhpParser\ConstExprEvaluator;
Expand Down Expand Up @@ -44,9 +46,15 @@ public function isStaticMethodSupported(MethodReflection $methodReflection): boo

public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
{
$arg1 = $methodCall->getArgs()[0]->value;
$value = $methodCall->getArgs()[0]->value;
$evaluator = new ConstExprEvaluator();
$arg1 = $evaluator->evaluateSilently($arg1);

if ($value instanceof ClassConstFetch && $value->class instanceof Name) {
$value = $value->class->toString();
}

$arg1 = $evaluator->evaluateSilently($value);

Check failure on line 56 in src/ClassRegistryInitExtension.php

View workflow job for this annotation

GitHub Actions / build

Parameter #1 $expr of method PhpParser\ConstExprEvaluator::evaluateSilently() expects PhpParser\Node\Expr, PhpParser\Node\Expr|string given.

if (! is_string($arg1)) {
return $this->getDefaultType();
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Feature/data/class_registry_init.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@

$modelWithoutClass = ClassRegistry::init('TableWithoutModel');
assertType('Model', $modelWithoutClass);

$class = ClassRegistry::init(BasicModel::class);
assertType('BasicModel', $class);

0 comments on commit 6a7822b

Please sign in to comment.