-
-
Notifications
You must be signed in to change notification settings - Fork 394
/
ModelPropertyStaticCallRule.php
154 lines (122 loc) · 4.55 KB
/
ModelPropertyStaticCallRule.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
<?php
declare(strict_types=1);
namespace NunoMaduro\Larastan\Rules\ModelProperties;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\TypeWithClassName;
/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\StaticCall>
*/
class ModelPropertyStaticCallRule implements Rule
{
/** @var ReflectionProvider */
private $reflectionProvider;
/** @var ModelPropertiesRuleHelper */
private $modelPropertiesRuleHelper;
/** @var RuleLevelHelper */
private $ruleLevelHelper;
public function __construct(ReflectionProvider $reflectionProvider, ModelPropertiesRuleHelper $ruleHelper, RuleLevelHelper $ruleLevelHelper)
{
$this->reflectionProvider = $reflectionProvider;
$this->modelPropertiesRuleHelper = $ruleHelper;
$this->ruleLevelHelper = $ruleLevelHelper;
}
public function getNodeType(): string
{
return Node\Expr\StaticCall::class;
}
/**
* @param Node\Expr\StaticCall $node
* @param Scope $scope
* @return string[]
*
* @throws \PHPStan\ShouldNotHappenException
*/
public function processNode(Node $node, Scope $scope): array
{
if (! $node->name instanceof Node\Identifier) {
return [];
}
if (count($node->getArgs()) === 0) {
return [];
}
$methodName = $node->name->name;
$class = $node->class;
if ($class instanceof Node\Name) {
$className = (string) $class;
$lowercasedClassName = strtolower($className);
if (in_array($lowercasedClassName, ['self', 'static'], true)) {
if (! $scope->isInClass()) {
return [];
}
$modelReflection = $scope->getClassReflection();
} elseif ($lowercasedClassName === 'parent') {
if (! $scope->isInClass()) {
return [];
}
$currentClassReflection = $scope->getClassReflection();
$parentClass = $currentClassReflection->getParentClass();
if ($parentClass === null) {
return [];
}
if ($scope->getFunctionName() === null) {
throw new \PHPStan\ShouldNotHappenException();
}
$modelReflection = $parentClass;
} else {
if (! $this->reflectionProvider->hasClass($className)) {
return [];
}
$modelReflection = $this->reflectionProvider->getClass($className);
}
} else {
$classTypeResult = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$class,
'',
static function (Type $type) use ($methodName): bool {
return $type->canCallMethods()->yes() && $type->hasMethod($methodName)->yes();
}
);
$classType = $classTypeResult->getType();
if ($classType instanceof ErrorType) {
return [];
}
$strings = TypeUtils::getConstantStrings($classType);
if (count($strings) === 1) {
$modelClassName = $strings[0]->getValue();
} elseif ($classType instanceof TypeWithClassName) {
$modelClassName = $classType->getClassName();
} else {
return [];
}
$modelReflection = $this->reflectionProvider->getClass($modelClassName);
}
if (! $modelReflection->isSubclassOf(Model::class)) {
return [];
}
if (! $modelReflection->hasMethod($methodName)) {
return [];
}
$methodReflection = $modelReflection->getMethod($methodName, $scope);
$className = $methodReflection->getDeclaringClass()->getName();
if ($className !== Builder::class &&
$className !== EloquentBuilder::class &&
$className !== Relation::class &&
$className !== Model::class
) {
return [];
}
return $this->modelPropertiesRuleHelper->check($methodReflection, $scope, $node->getArgs(), $modelReflection);
}
}