Repository navigation
Expand file tree
/
Copy pathRemoveParentDelegatingClassMethodRector.php
More file actions
276 lines (228 loc) · 8.17 KB
/
Copy pathRemoveParentDelegatingClassMethodRector.php
File metadata and controls
276 lines (228 loc) · 8.17 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
declare(strict_types=1);
namespace Rector\DeadCode\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeVisitor;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ExtendedParameterReflection;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Enum\ObjectReference;
use Rector\PHPStan\ScopeFetcher;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\ValueObject\MethodName;
/**
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveParentDelegatingClassMethodRector\RemoveParentDelegatingClassMethodRectorTest
*/
final class RemoveParentDelegatingClassMethodRector extends AbstractRector
{
public function __construct(
private readonly StaticTypeMapper $staticTypeMapper,
private readonly PhpDocInfoFactory $phpDocInfoFactory
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove class method that only delegates call to parent class method with the same values',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeCommand extends Command
{
protected function configure(): void
{
parent::configure();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeCommand extends Command
{
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?int
{
// constructors are handled by RemoveParentDelegatingConstructorRector
if ($this->isName($node, MethodName::CONSTRUCT)) {
return null;
}
if ($node->isFinal() || $node->isAbstract() || $node->isPrivate() || $node->byRef) {
return null;
}
if ($node->stmts === null || count($node->stmts) !== 1) {
return null;
}
if ($node->attrGroups !== []) {
return null;
}
if (! $this->isParentCallDelegatingParams($node, $node->stmts[0])) {
return null;
}
if ($this->hasRefiningDocblock($node)) {
return null;
}
$parentMethodReflection = $this->matchParentMethodReflection($node);
if (! $parentMethodReflection instanceof ExtendedMethodReflection) {
return null;
}
if (! $this->isSignatureMatching($node, $parentMethodReflection)) {
return null;
}
return NodeVisitor::REMOVE_NODE;
}
private function hasRefiningDocblock(ClassMethod $classMethod): bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
return array_any($phpDocInfo->getPhpDocNode()->children, fn (PhpDocChildNode $phpDocChildNode): bool => $phpDocChildNode instanceof PhpDocTagNode);
}
private function matchParentMethodReflection(ClassMethod $classMethod): ?ExtendedMethodReflection
{
$scope = ScopeFetcher::fetch($classMethod);
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return null;
}
if (! $classReflection->isClass()) {
return null;
}
$parentClassReflection = $classReflection->getParentClass();
if (! $parentClassReflection instanceof ClassReflection) {
return null;
}
$methodName = $classMethod->name->toString();
if (! $parentClassReflection->hasNativeMethod($methodName)) {
return null;
}
$extendedMethodReflection = $parentClassReflection->getNativeMethod($methodName);
// a private parent method is not visible in the child scope
if ($extendedMethodReflection->isPrivate()) {
return null;
}
return $extendedMethodReflection;
}
/**
* Body must be a sole parent::sameMethod($sameParams, ...) call, passing every param in the same order
*/
private function isParentCallDelegatingParams(ClassMethod $classMethod, Stmt $soleStmt): bool
{
$staticCall = $this->matchParentStaticCall($soleStmt, $classMethod->name->toString());
if (! $staticCall instanceof StaticCall) {
return false;
}
$args = $staticCall->getArgs();
$params = $classMethod->getParams();
if (count($args) !== count($params)) {
return false;
}
$paramNames = [];
foreach ($params as $param) {
if ($param->variadic || $param->byRef || $param->default instanceof Node || $param->attrGroups !== []) {
return false;
}
$paramNames[] = $this->getName($param->var);
}
$argNames = [];
foreach ($args as $arg) {
if ($arg->name instanceof Node || $arg->unpack) {
return false;
}
if (! $arg->value instanceof Variable) {
return false;
}
$argNames[] = $this->getName($arg->value);
}
return $paramNames === $argNames;
}
private function matchParentStaticCall(Stmt $stmt, string $methodName): ?StaticCall
{
if ($stmt instanceof Return_) {
$expr = $stmt->expr;
} elseif ($stmt instanceof Expression) {
$expr = $stmt->expr;
} else {
return null;
}
if (! $expr instanceof StaticCall) {
return null;
}
if ($expr->isFirstClassCallable()) {
return null;
}
if (! $this->isName($expr->class, ObjectReference::PARENT)) {
return null;
}
if (! $this->isName($expr->name, $methodName)) {
return null;
}
return $expr;
}
private function isSignatureMatching(
ClassMethod $classMethod,
ExtendedMethodReflection $extendedMethodReflection
): bool {
if ($classMethod->isStatic() !== $extendedMethodReflection->isStatic()) {
return false;
}
if ($classMethod->isPublic() !== $extendedMethodReflection->isPublic()) {
return false;
}
$extendedParametersAcceptor = $extendedMethodReflection->getOnlyVariant();
$parameterReflections = $extendedParametersAcceptor->getParameters();
foreach ($classMethod->getParams() as $position => $param) {
$parameterReflection = $parameterReflections[$position] ?? null;
if (! $parameterReflection instanceof ExtendedParameterReflection) {
return false;
}
// no type override
if ($param->type === null) {
continue;
}
// compare native types only, as the child method may add a native type
// the parent only has in a docblock - removing it would drop a runtime check
$parentParameterType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
$parameterReflection->getNativeType(),
TypeKind::PARAM
);
if (! $this->nodeComparator->areNodesEqual($param->type, $parentParameterType)) {
return false;
}
}
// no return type override
if (! $classMethod->returnType instanceof Node) {
return true;
}
$parentReturnType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
$extendedParametersAcceptor->getNativeReturnType(),
TypeKind::RETURN
);
return $this->nodeComparator->areNodesEqual($classMethod->returnType, $parentReturnType);
}
}