-
Notifications
You must be signed in to change notification settings - Fork 432
/
UnusedPrivateMethodRule.php
171 lines (153 loc) · 4.86 KB
/
UnusedPrivateMethodRule.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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\DeadCode;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PHPStan\Analyser\Scope;
use PHPStan\Node\ClassMethodsNode;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantStringType;
use function array_map;
use function count;
use function sprintf;
use function strtolower;
/**
* @implements Rule<ClassMethodsNode>
*/
class UnusedPrivateMethodRule implements Rule
{
public function getNodeType(): string
{
return ClassMethodsNode::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$node->getClass() instanceof Node\Stmt\Class_ && !$node->getClass() instanceof Node\Stmt\Enum_) {
return [];
}
$classReflection = $node->getClassReflection();
$constructor = null;
if ($classReflection->hasConstructor()) {
$constructor = $classReflection->getConstructor();
}
$methods = [];
foreach ($node->getMethods() as $method) {
if (!$method->getNode()->isPrivate()) {
continue;
}
if ($method->isDeclaredInTrait()) {
continue;
}
$methodName = $method->getNode()->name->toString();
if ($constructor !== null && $constructor->getName() === $methodName) {
continue;
}
if (strtolower($methodName) === '__clone') {
continue;
}
$methods[strtolower($methodName)] = $method;
}
$arrayCalls = [];
foreach ($node->getMethodCalls() as $methodCall) {
$methodCallNode = $methodCall->getNode();
if ($methodCallNode instanceof Node\Expr\Array_) {
$arrayCalls[] = $methodCall;
continue;
}
$callScope = $methodCall->getScope();
if ($methodCallNode->name instanceof Identifier) {
$methodNames = [$methodCallNode->name->toString()];
} else {
$methodNameType = $callScope->getType($methodCallNode->name);
$strings = $methodNameType->getConstantStrings();
if (count($strings) === 0) {
return [];
}
$methodNames = array_map(static fn (ConstantStringType $type): string => $type->getValue(), $strings);
}
if ($methodCallNode instanceof Node\Expr\MethodCall) {
$calledOnType = $callScope->getType($methodCallNode->var);
} else {
if ($methodCallNode->class instanceof Node\Name) {
$calledOnType = $callScope->resolveTypeByName($methodCallNode->class);
} else {
$calledOnType = $callScope->getType($methodCallNode->class);
}
}
$inMethod = $callScope->getFunction();
if (!$inMethod instanceof MethodReflection) {
continue;
}
foreach ($methodNames as $methodName) {
$methodReflection = $callScope->getMethodReflection($calledOnType, $methodName);
if ($methodReflection === null) {
continue;
}
if ($methodReflection->getDeclaringClass()->getName() !== $classReflection->getName()) {
continue;
}
if ($inMethod->getName() === $methodName) {
continue;
}
unset($methods[strtolower($methodName)]);
}
}
if (count($methods) > 0) {
foreach ($arrayCalls as $arrayCall) {
/** @var Node\Expr\Array_ $array */
$array = $arrayCall->getNode();
$arrayScope = $arrayCall->getScope();
$arrayType = $arrayScope->getType($array);
if (!$arrayType->isCallable()->yes()) {
continue;
}
foreach ($arrayType->getConstantArrays() as $constantArray) {
foreach ($constantArray->findTypeAndMethodNames() as $typeAndMethod) {
if ($typeAndMethod->isUnknown()) {
return [];
}
if (!$typeAndMethod->getCertainty()->yes()) {
return [];
}
$calledOnType = $typeAndMethod->getType();
$methodReflection = $arrayScope->getMethodReflection($calledOnType, $typeAndMethod->getMethod());
if ($methodReflection === null) {
continue;
}
if ($methodReflection->getDeclaringClass()->getName() !== $classReflection->getName()) {
continue;
}
$inMethod = $arrayScope->getFunction();
if (!$inMethod instanceof MethodReflection) {
continue;
}
if ($inMethod->getName() === $typeAndMethod->getMethod()) {
continue;
}
unset($methods[strtolower($typeAndMethod->getMethod())]);
}
}
}
}
$errors = [];
foreach ($methods as $method) {
$originalMethodName = $method->getNode()->name->toString();
$methodType = 'Method';
if ($method->getNode()->isStatic()) {
$methodType = 'Static method';
}
$errors[] = RuleErrorBuilder::message(sprintf('%s %s::%s() is unused.', $methodType, $classReflection->getDisplayName(), $originalMethodName))
->line($method->getNode()->getLine())
->identifier('deadCode.unusedMethod')
->metadata([
'classOrder' => $node->getClass()->getAttribute('statementOrder'),
'classDepth' => $node->getClass()->getAttribute('statementDepth'),
'classStartLine' => $node->getClass()->getStartLine(),
'methodName' => $originalMethodName,
])
->build();
}
return $errors;
}
}