Skip to content

Commit bd6b9ca

Browse files
authored
Merge pull request #1 from sidz/fix-class-registry-to-be-able-to-work-with-class-const-fetch
Fix ClassRegistryInit doesn't work with ClassConstFetch AST
2 parents 37da253 + 3f89598 commit bd6b9ca

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/ClassRegistryInitExtension.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace PHPStanCakePHP2;
66

7+
use PhpParser\Node\Expr\ClassConstFetch;
8+
use PhpParser\Node\Name;
9+
use PhpParser\Node\Scalar\String_;
710
use PHPStanCakePHP2\Service\SchemaService;
811
use Inflector;
912
use PhpParser\ConstExprEvaluator;
@@ -44,9 +47,15 @@ public function isStaticMethodSupported(MethodReflection $methodReflection): boo
4447

4548
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
4649
{
47-
$arg1 = $methodCall->getArgs()[0]->value;
50+
$value = $methodCall->getArgs()[0]->value;
4851
$evaluator = new ConstExprEvaluator();
49-
$arg1 = $evaluator->evaluateSilently($arg1);
52+
53+
if ($value instanceof ClassConstFetch && $value->class instanceof Name\FullyQualified) {
54+
$value = new String_($value->class->toString());
55+
}
56+
57+
$arg1 = $evaluator->evaluateSilently($value);
58+
5059
if (! is_string($arg1)) {
5160
return $this->getDefaultType();
5261
}

tests/Feature/data/class_registry_init.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212

1313
$modelWithoutClass = ClassRegistry::init('TableWithoutModel');
1414
assertType('Model', $modelWithoutClass);
15+
16+
$class = ClassRegistry::init(BasicModel::class);
17+
assertType('BasicModel', $class);

0 commit comments

Comments
 (0)