-
-
Notifications
You must be signed in to change notification settings - Fork 394
/
RelationParserHelper.php
133 lines (103 loc) · 3.99 KB
/
RelationParserHelper.php
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
declare(strict_types=1);
namespace NunoMaduro\Larastan\Types;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\NodeFinder;
use PHPStan\Analyser\ScopeContext;
use PHPStan\Analyser\ScopeFactory;
use PHPStan\Parser\Parser;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\TypeWithClassName;
class RelationParserHelper
{
/** @var Parser */
private $parser;
/** @var ScopeFactory */
private $scopeFactory;
/** @var ReflectionProvider */
private $reflectionProvider;
public function __construct(Parser $parser, ScopeFactory $scopeFactory, ReflectionProvider $reflectionProvider)
{
$this->parser = $parser;
$this->scopeFactory = $scopeFactory;
$this->reflectionProvider = $reflectionProvider;
}
public function findRelatedModelInRelationMethod(
MethodReflection $methodReflection
): ?string {
if (method_exists($methodReflection, 'getDeclaringTrait') && $methodReflection->getDeclaringTrait() !== null) {
$fileName = $methodReflection->getDeclaringTrait()->getFileName();
} else {
$fileName = $methodReflection
->getDeclaringClass()
->getNativeReflection()
->getMethod($methodReflection->getName())
->getFileName();
}
if ($fileName === false || $fileName === null) {
return null;
}
$fileStmts = $this->parser->parseFile($fileName);
/** @var Node\Stmt\ClassMethod|null $relationMethod */
$relationMethod = $this->findMethod($methodReflection->getName(), $fileStmts);
if ($relationMethod === null) {
return null;
}
/** @var Node\Stmt\Return_|null $returnStmt */
$returnStmt = $this->findReturn($relationMethod);
if ($returnStmt === null || ! $returnStmt->expr instanceof MethodCall) {
return null;
}
$methodCall = $returnStmt->expr;
while ($methodCall->var instanceof MethodCall) {
$methodCall = $methodCall->var;
}
if (count($methodCall->getArgs()) < 1) {
return null;
}
$scope = $this->scopeFactory->create(ScopeContext::create($fileName));
$methodScope = $scope
->enterClass($methodReflection->getDeclaringClass())
->enterClassMethod($relationMethod, TemplateTypeMap::createEmpty(), [], null, null, null, false, false, false);
$argType = $methodScope->getType($methodCall->getArgs()[0]->value);
$returnClass = null;
$constantStrings = TypeUtils::getConstantStrings($argType);
if (count($constantStrings) === 1) {
$returnClass = $constantStrings[0]->getValue();
}
if ($argType instanceof GenericClassStringType) {
$modelType = $argType->getGenericType();
if (! $modelType instanceof TypeWithClassName) {
return null;
}
$returnClass = $modelType->getClassName();
}
if ($returnClass === null) {
return null;
}
return $this->reflectionProvider->hasClass($returnClass) ? $returnClass : null;
}
/**
* @param string $method
* @param mixed $statements
* @return Node|null
*/
private function findMethod(string $method, $statements): ?Node
{
return (new NodeFinder)->findFirst($statements, static function (Node $node) use ($method) {
return $node instanceof Node\Stmt\ClassMethod
&& $node->name->toString() === $method;
});
}
private function findReturn(Node\Stmt\ClassMethod $relationMethod): ?Node
{
/** @var Node[] $statements */
$statements = $relationMethod->stmts;
return (new NodeFinder)->findFirstInstanceOf($statements, Node\Stmt\Return_::class);
}
}