Skip to content

Commit 2fe3af0

Browse files
authored
Merge pull request #4 from sidz/fix-error-occured-if-not-a-string-passed-into-init
Fix error occurred when not a string is passed into ClassRegistry::init method
2 parents 467dd97 + e8a3efe commit 2fe3af0

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

src/ClassRegistryInitExtension.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
namespace PHPStanCakePHP2;
66

7+
use PhpParser\Node\Expr;
78
use PhpParser\Node\Expr\ClassConstFetch;
89
use PhpParser\Node\Expr\Variable;
910
use PhpParser\Node\Name;
1011
use PhpParser\Node\Scalar\String_;
12+
use PHPStan\Type\Constant\ConstantStringType;
1113
use PHPStanCakePHP2\Service\SchemaService;
1214
use Inflector;
1315
use PhpParser\ConstExprEvaluator;
@@ -48,27 +50,19 @@ public function isStaticMethodSupported(MethodReflection $methodReflection): boo
4850

4951
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
5052
{
51-
$value = $methodCall->getArgs()[0]->value;
53+
$argumentType = $scope->getType($methodCall->getArgs()[0]->value);
5254

53-
if ($value instanceof Variable) {
54-
return new ObjectType('Model');
55-
}
56-
57-
if ($value instanceof ClassConstFetch && $value->class instanceof Name\FullyQualified) {
58-
$value = new String_($value->class->toString());
59-
}
60-
61-
$arg1 = (new ConstExprEvaluator())->evaluateSilently($value);
62-
63-
if (! is_string($arg1)) {
55+
if (!$argumentType instanceof ConstantStringType) {
6456
return $this->getDefaultType();
6557
}
6658

67-
if ($this->reflectionProvider->hasClass($arg1)) {
68-
return new ObjectType($arg1);
59+
$value = $argumentType->getValue();
60+
61+
if ($this->reflectionProvider->hasClass($value)) {
62+
return new ObjectType($value);
6963
}
7064

71-
if ($this->schemaService->hasTable(Inflector::tableize($arg1))) {
65+
if ($this->schemaService->hasTable(Inflector::tableize($value))) {
7266
return new ObjectType('Model');
7367
}
7468

@@ -79,7 +73,7 @@ private function getDefaultType(): Type
7973
{
8074
return new UnionType([
8175
new BooleanType(),
82-
new ObjectWithoutClassType()
76+
new ObjectWithoutClassType(),
8377
]);
8478
}
8579
}

tests/Feature/data/class_registry_init.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@
1818

1919
$var = 'BasicModel';
2020

21-
assertType('Model', ClassRegistry::init($var));
21+
assertType('BasicModel', ClassRegistry::init($var));
22+
23+
assertType('bool|object', ClassRegistry::init([123]));

0 commit comments

Comments
 (0)