-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathClassRegistryInitExtension.php
More file actions
85 lines (74 loc) · 2.29 KB
/
Copy pathClassRegistryInitExtension.php
File metadata and controls
85 lines (74 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
declare(strict_types=1);
namespace ARiddlestone\PHPStanCakePHP2;
use ARiddlestone\PHPStanCakePHP2\Service\SchemaService;
use Exception;
use Inflector;
use PhpParser\ConstExprEvaluationException;
use PhpParser\ConstExprEvaluator;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\BooleanType;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension as ReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
final class ClassRegistryInitExtension implements ReturnTypeExtension
{
private ReflectionProvider $reflectionProvider;
private SchemaService $schemaService;
public function __construct(
ReflectionProvider $reflectionProvider,
SchemaService $schemaService
) {
$this->reflectionProvider = $reflectionProvider;
$this->schemaService = $schemaService;
}
public function getClass(): string
{
return 'ClassRegistry';
}
public function isStaticMethodSupported(
MethodReflection $methodReflection
): bool {
return $methodReflection->getName() === 'init';
}
/**
* @throws ShouldNotHappenException
* @throws ConstExprEvaluationException
* @throws Exception
*/
public function getTypeFromStaticMethodCall(
MethodReflection $methodReflection,
StaticCall $methodCall,
Scope $scope
): Type {
$arg1 = $methodCall->getArgs()[0]->value;
$evaluator = new ConstExprEvaluator();
$arg1 = $evaluator->evaluateSilently($arg1);
if (! is_string($arg1)) {
return $this->getDefaultType();
}
if ($this->reflectionProvider->hasClass($arg1)) {
return new ObjectType($arg1);
}
if ($this->schemaService->hasTable(Inflector::tableize($arg1))) {
return new ObjectType('Model');
}
return $this->getDefaultType();
}
/**
* @throws ShouldNotHappenException
*/
private function getDefaultType(): Type
{
return new UnionType([
new BooleanType(),
new ObjectWithoutClassType(),
]);
}
}