-
-
Notifications
You must be signed in to change notification settings - Fork 394
/
RelationExistenceRule.php
177 lines (141 loc) · 5.41 KB
/
RelationExistenceRule.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
declare(strict_types=1);
namespace NunoMaduro\Larastan\Rules;
use Illuminate\Database\Eloquent\Relations\Relation;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
/** @implements Rule<Node\Expr\CallLike> */
class RelationExistenceRule implements Rule
{
public function __construct(private ModelRuleHelper $modelRuleHelper)
{
}
/**
* @inheritDoc
*/
public function getNodeType(): string
{
return Node\Expr\CallLike::class;
}
/**
* @inheritDoc
*/
public function processNode(Node $node, Scope $scope): array
{
if (! $node instanceof MethodCall && ! $node instanceof Node\Expr\StaticCall) {
return [];
}
if (! $node->name instanceof Node\Identifier) {
return [];
}
if (! in_array($node->name->name, [
'has',
'with',
'orHas',
'doesntHave',
'orDoesntHave',
'whereHas',
'orWhereHas',
'whereDoesntHave',
'orWhereDoesntHave',
],
true)
) {
return [];
}
$args = $node->getArgs();
if (count($args) < 1) {
return [];
}
$valueType = $scope->getType($args[0]->value);
/** @var ConstantStringType[] $relations */
$relations = [];
if ($valueType->isConstantArray()->yes()) {
$arrays = $valueType->getConstantArrays();
foreach ($arrays as $array) {
$relations = array_merge(
$relations,
...array_map(function (Type $type) {
return TypeUtils::getConstantStrings($type);
}, $array->getKeyTypes()),
...array_map(function (Type $type) {
return TypeUtils::getConstantStrings($type);
}, $array->getValueTypes()),
);
}
} else {
$constants = TypeUtils::getConstantStrings($valueType);
if ($constants === []) {
return [];
}
$relations = $constants;
}
$errors = [];
foreach ($relations as $relationType) {
$relationName = explode(':', $relationType->getValue())[0];
$calledOnNode = $node instanceof MethodCall ? $node->var : $node->class;
if ($calledOnNode instanceof Node\Name) {
$calledOnType = new ObjectType($calledOnNode->toString());
} else {
$calledOnType = $scope->getType($calledOnNode);
}
$closure = function (Type $calledOnType, string $relationName, Node $node) use ($scope): array {
$modelReflection = $this->modelRuleHelper->findModelReflectionFromType($calledOnType);
if ($modelReflection === null) {
return [];
}
if (! $modelReflection->hasMethod($relationName)) {
return [
$this->getRuleError($relationName, $modelReflection, $node),
];
}
$relationMethod = $modelReflection->getMethod($relationName, $scope);
if (! (new ObjectType(Relation::class))->isSuperTypeOf(ParametersAcceptorSelector::selectSingle($relationMethod->getVariants())->getReturnType())->yes()) {
return [
$this->getRuleError($relationName, $modelReflection, $node),
];
}
return [];
};
if (str_contains($relationName, '.')) {
// Nested relations
$relations = explode('.', $relationName);
foreach ($relations as $relation) {
$result = $closure($calledOnType, $relation, $node);
if ($result !== []) {
return $result;
}
$modelReflection = $this->modelRuleHelper->findModelReflectionFromType($calledOnType);
if ($modelReflection === null) {
return [];
}
// Dynamic method return type extensions are not taken into account here.
// So we simulate a method call to the relation here to get the return type.
$calledOnType = $scope->getType(new MethodCall(new Node\Expr\New_(new Node\Name($modelReflection->getName())), new Node\Identifier($relation)));
}
return [];
}
$errors = array_merge($errors, $closure($calledOnType, $relationName, $node));
}
return $errors;
}
private function getRuleError(
string $relationName,
\PHPStan\Reflection\ClassReflection $modelReflection,
Node $node
): \PHPStan\Rules\RuleError {
return RuleErrorBuilder::message(sprintf("Relation '%s' is not found in %s model.", $relationName,
$modelReflection->getName()))
->identifier('rules.relationExistence')
->line($node->getAttribute('startLine'))
->build();
}
}