Repository navigation
Expand file tree
/
Copy pathIsClassMethodUsedAnalyzer.php
More file actions
231 lines (190 loc) · 8.31 KB
/
Copy pathIsClassMethodUsedAnalyzer.php
File metadata and controls
231 lines (190 loc) · 8.31 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
declare(strict_types=1);
namespace Rector\DeadCode\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\NodeVisitor;
use PHPStan\Analyser\Scope;
use PHPStan\Parser\ArrayMapArgVisitor;
use PHPStan\Reflection\ClassReflection;
use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher;
use Rector\NodeCollector\ValueObject\ArrayCallable;
use Rector\NodeCollector\ValueObject\ArrayCallableDynamicMethod;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpParser\AstResolver;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Reflection\ReflectionResolver;
final readonly class IsClassMethodUsedAnalyzer
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
private AstResolver $astResolver,
private BetterNodeFinder $betterNodeFinder,
private ValueResolver $valueResolver,
private ArrayCallableMethodMatcher $arrayCallableMethodMatcher,
private CallCollectionAnalyzer $callCollectionAnalyzer,
private ReflectionResolver $reflectionResolver,
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser
) {
}
public function isClassMethodUsed(Class_ $class, ClassMethod $classMethod, Scope $scope): bool
{
$classMethodName = $this->nodeNameResolver->getName($classMethod);
// 1. direct normal calls
if ($this->isClassMethodCalledInLocalMethodCall($class, $classMethodName)) {
return true;
}
// 2. direct null-safe calls
if ($this->isClassMethodCalledInLocalNullsafeMethodCall($class, $classMethodName)) {
return true;
}
// 3. direct static calls
if ($this->isClassMethodUsedInLocalStaticCall($class, $classMethodName)) {
return true;
}
// 4. magic array calls!
if ($this->isClassMethodCalledInLocalArrayCall($class, $classMethod, $scope)) {
return true;
}
// 4. private method exists in trait and is overwritten by the class
return $this->doesMethodExistInTrait($classMethod, $classMethodName);
}
private function isClassMethodUsedInLocalStaticCall(Class_ $class, string $classMethodName): bool
{
$className = (string) $this->nodeNameResolver->getName($class);
/** @var StaticCall[] $staticCalls */
$staticCalls = $this->betterNodeFinder->findInstanceOf($class, StaticCall::class);
return $this->callCollectionAnalyzer->isExists($staticCalls, $classMethodName, $className);
}
private function isClassMethodCalledInLocalMethodCall(Class_ $class, string $classMethodName): bool
{
$className = (string) $this->nodeNameResolver->getName($class);
/** @var MethodCall[] $methodCalls */
$methodCalls = $this->betterNodeFinder->findInstanceOf($class, MethodCall::class);
return $this->callCollectionAnalyzer->isExists($methodCalls, $classMethodName, $className);
}
private function isClassMethodCalledInLocalNullsafeMethodCall(Class_ $class, string $classMethodName): bool
{
$className = (string) $this->nodeNameResolver->getName($class);
/** @var NullsafeMethodCall[] $methodCalls */
$methodCalls = $this->betterNodeFinder->findInstanceOf($class, NullsafeMethodCall::class);
return $this->callCollectionAnalyzer->isExists($methodCalls, $classMethodName, $className);
}
private function isInArrayMap(Class_ $class, Array_ $array): bool
{
if (! $array->getAttribute(ArrayMapArgVisitor::ATTRIBUTE_NAME) instanceof Arg) {
return false;
}
if (count($array->items) !== 2) {
return false;
}
$value = $this->valueResolver->getValue($array->items[1]->value);
if (! is_string($value)) {
return false;
}
return $class->getMethod($value) instanceof ClassMethod;
}
private function isClassMethodCalledInLocalArrayCall(Class_ $class, ClassMethod $classMethod, Scope $scope): bool
{
/** @var Array_[] $arrays */
$arrays = $this->betterNodeFinder->findInstanceOf($class, Array_::class);
$classMethodName = $this->nodeNameResolver->getName($classMethod);
foreach ($arrays as $array) {
if ($this->isInArrayMap($class, $array)) {
return true;
}
$arrayCallable = $this->arrayCallableMethodMatcher->match($array, $scope, $classMethodName);
if ($arrayCallable instanceof ArrayCallableDynamicMethod) {
return true;
}
if ($this->shouldSkipArrayCallable($class, $arrayCallable)) {
continue;
}
// the method is used
/** @var ArrayCallable $arrayCallable */
if ($this->nodeNameResolver->isName($classMethod->name, $arrayCallable->getMethod())) {
return true;
}
}
return false;
}
private function shouldSkipArrayCallable(Class_ $class, null|ArrayCallable $arrayCallable): bool
{
if (! $arrayCallable instanceof ArrayCallable) {
return true;
}
// is current class method?
return ! $this->nodeNameResolver->isName($class, $arrayCallable->getClass());
}
private function doesMethodExistInTrait(ClassMethod $classMethod, string $classMethodName): bool
{
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (! $classReflection instanceof ClassReflection) {
return false;
}
$traits = $this->astResolver->parseClassReflectionTraits($classReflection);
$className = $classReflection->getName();
return array_any(
$traits,
fn (Trait_ $trait): bool => $this->isUsedByTrait($trait, $classMethodName, $className)
);
}
private function isUsedByTrait(Trait_ $trait, string $classMethodName, string $className): bool
{
foreach ($trait->getMethods() as $classMethod) {
if ($classMethod->name->toString() === $classMethodName) {
return true;
}
/**
* Trait can't detect class type, so it rely on "this" or "self" or "static" or "ClassName::methodName()" usage...
*/
$callMethod = null;
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
(array) $classMethod->stmts,
function (Node $subNode) use ($className, $classMethodName, &$callMethod): ?int {
if ($subNode instanceof Class_ || $subNode instanceof Function_) {
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
if ($subNode instanceof MethodCall
&& $this->nodeNameResolver->isName($subNode->var, 'this')
&& $this->nodeNameResolver->isName($subNode->name, $classMethodName)) {
$callMethod = $subNode;
return NodeVisitor::STOP_TRAVERSAL;
}
if ($this->isStaticCallMatch($subNode, $className, $classMethodName)) {
$callMethod = $subNode;
return NodeVisitor::STOP_TRAVERSAL;
}
return null;
}
);
if ($callMethod instanceof CallLike) {
return true;
}
}
return false;
}
private function isStaticCallMatch(Node $subNode, string $className, string $classMethodName): bool
{
if (! $subNode instanceof StaticCall) {
return false;
}
if (! $subNode->class instanceof Name) {
return false;
}
return ($subNode->class->isSpecialClassName() || $subNode->class->toString() === $className)
&& $this->nodeNameResolver->isName($subNode->name, $classMethodName);
}
}