Skip to content

Commit 467dd97

Browse files
authored
Merge pull request #3 from sidz/mark-as-default-if-varuable-passed-into-registry-init
Return Model if variable is passed into ClassRegistry::init method
2 parents bd6b9ca + 8121dde commit 467dd97

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/ClassRegistryInitExtension.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PHPStanCakePHP2;
66

77
use PhpParser\Node\Expr\ClassConstFetch;
8+
use PhpParser\Node\Expr\Variable;
89
use PhpParser\Node\Name;
910
use PhpParser\Node\Scalar\String_;
1011
use PHPStanCakePHP2\Service\SchemaService;
@@ -48,23 +49,29 @@ public function isStaticMethodSupported(MethodReflection $methodReflection): boo
4849
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
4950
{
5051
$value = $methodCall->getArgs()[0]->value;
51-
$evaluator = new ConstExprEvaluator();
52+
53+
if ($value instanceof Variable) {
54+
return new ObjectType('Model');
55+
}
5256

5357
if ($value instanceof ClassConstFetch && $value->class instanceof Name\FullyQualified) {
5458
$value = new String_($value->class->toString());
5559
}
5660

57-
$arg1 = $evaluator->evaluateSilently($value);
61+
$arg1 = (new ConstExprEvaluator())->evaluateSilently($value);
5862

5963
if (! is_string($arg1)) {
6064
return $this->getDefaultType();
6165
}
66+
6267
if ($this->reflectionProvider->hasClass($arg1)) {
6368
return new ObjectType($arg1);
6469
}
70+
6571
if ($this->schemaService->hasTable(Inflector::tableize($arg1))) {
6672
return new ObjectType('Model');
6773
}
74+
6875
return $this->getDefaultType();
6976
}
7077

tests/Feature/data/class_registry_init.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@
1515

1616
$class = ClassRegistry::init(BasicModel::class);
1717
assertType('BasicModel', $class);
18+
19+
$var = 'BasicModel';
20+
21+
assertType('Model', ClassRegistry::init($var));

0 commit comments

Comments
 (0)