Repository navigation
Expand file tree
/
Copy pathRemoveUnusedPromotedPropertyRector.php
More file actions
199 lines (170 loc) · 6.16 KB
/
Copy pathRemoveUnusedPromotedPropertyRector.php
File metadata and controls
199 lines (170 loc) · 6.16 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
<?php
declare(strict_types=1);
namespace Rector\DeadCode\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\TraitUse;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\Reflection\ClassReflection;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\DeadCode\NodeAnalyzer\PropertyWriteonlyAnalyzer;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\NodeFinder\PropertyFetchFinder;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\ValueObject\MethodName;
use Rector\ValueObject\PhpVersion;
use Rector\ValueObject\Visibility;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
/**
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector\RemoveUnusedPromotedPropertyRectorTest
*/
final class RemoveUnusedPromotedPropertyRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly PropertyFetchFinder $propertyFetchFinder,
private readonly VisibilityManipulator $visibilityManipulator,
private readonly PropertyWriteonlyAnalyzer $propertyWriteonlyAnalyzer,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ReflectionResolver $reflectionResolver,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly PhpDocTagRemover $phpDocTagRemover,
private readonly DocBlockUpdater $docBlockUpdater
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove unused promoted property', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function __construct(
private $usedDependency,
private $someUnusedDependency
) {
}
public function getUsedDependency()
{
return $this->usedDependency;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function __construct(
private $usedDependency
) {
}
public function getUsedDependency()
{
return $this->usedDependency;
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
if (! $constructClassMethod instanceof ClassMethod) {
return null;
}
if ($constructClassMethod->params === []) {
return null;
}
if ($this->shouldSkipClass($node)) {
return null;
}
$hasChanged = false;
$phpDocInfo = null;
// only trailing params can be removed, removing earlier ones would shift positional arguments of callers
$isTrailingParam = true;
foreach (array_reverse($constructClassMethod->params, true) as $key => $param) {
// only private local scope; removing public property might be dangerous
if (! $this->visibilityManipulator->hasVisibility($param, Visibility::PRIVATE)) {
$isTrailingParam = false;
continue;
}
$paramName = $this->getName($param);
$propertyFetches = $this->propertyFetchFinder->findLocalPropertyFetchesByName($node, $paramName);
if ($propertyFetches !== []) {
$isTrailingParam = false;
continue;
}
// always changed on below code
$hasChanged = true;
// is variable used or param is followed by other params? only remove property, keep param
$variable = $this->betterNodeFinder->findVariableOfName((array) $constructClassMethod->stmts, $paramName);
if ($variable instanceof Variable || ! $isTrailingParam) {
$param->flags = 0;
$isTrailingParam = false;
continue;
}
$phpDocInfo ??= $this->phpDocInfoFactory->createFromNode($constructClassMethod);
if ($phpDocInfo instanceof PhpDocInfo) {
$paramTagValueNode = $phpDocInfo->getParamTagValueByName($paramName);
if ($paramTagValueNode instanceof ParamTagValueNode) {
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $paramTagValueNode);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($constructClassMethod);
}
}
// remove param
unset($constructClassMethod->params[$key]);
}
if ($hasChanged) {
return $node;
}
return null;
}
public function provideMinPhpVersion(): int
{
return PhpVersion::PHP_80;
}
private function shouldSkipClass(Class_ $class): bool
{
if ($class->attrGroups !== []) {
return true;
}
$magicGetMethod = $class->getMethod(MethodName::__GET);
if ($magicGetMethod instanceof ClassMethod) {
return true;
}
foreach ($class->stmts as $stmt) {
if ($stmt instanceof TraitUse) {
return true;
}
}
$classReflection = $this->reflectionResolver->resolveClassReflection($class);
if ($classReflection instanceof ClassReflection) {
$interfaces = $classReflection->getInterfaces();
foreach ($interfaces as $interface) {
if ($interface->hasNativeMethod(MethodName::CONSTRUCT)) {
return true;
}
}
}
return $this->propertyWriteonlyAnalyzer->hasClassDynamicPropertyNames($class);
}
}