-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
RemoveDuplicatedIfReturnRector.php
239 lines (203 loc) · 6.5 KB
/
RemoveDuplicatedIfReturnRector.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
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
<?php
declare(strict_types=1);
namespace Rector\DeadCode\Rector\FunctionLike;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeTraverser;
use PHPStan\Type\BooleanType;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\NodeCollector\ModifiedVariableNamesCollector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://github.com/rectorphp/rector/issues/2945
*
* @see \Rector\Tests\DeadCode\Rector\FunctionLike\RemoveDuplicatedIfReturnRector\RemoveDuplicatedIfReturnRectorTest
*/
final class RemoveDuplicatedIfReturnRector extends AbstractRector
{
public function __construct(
private readonly IfManipulator $ifManipulator,
private readonly ModifiedVariableNamesCollector $modifiedVariableNamesCollector,
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer,
NodeComparator $nodeComparator
) {
$this->nodeComparator = $nodeComparator;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove duplicated if stmt with return in function/method body',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($value)
{
if ($value) {
return true;
}
$value2 = 100;
if ($value) {
return true;
}
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($value)
{
if ($value) {
return true;
}
$value2 = 100;
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FunctionLike::class];
}
/**
* @param FunctionLike $node
*/
public function refactor(Node $node): ?Node
{
$ifWithOnlyReturnsByHash = $this->collectDuplicatedIfWithOnlyReturnByHash($node);
if ($ifWithOnlyReturnsByHash === []) {
return null;
}
$hasRemovedNode = false;
foreach ($ifWithOnlyReturnsByHash as $ifWithOnlyReturns) {
$isBool = $this->isBoolVarIfCondReturnTrueNextReturnBoolVar($ifWithOnlyReturns);
if (! $isBool && count($ifWithOnlyReturns) < 2) {
continue;
}
if (! $isBool) {
// keep first one
array_shift($ifWithOnlyReturns);
}
foreach ($ifWithOnlyReturns as $ifWithOnlyReturn) {
$this->removeNode($ifWithOnlyReturn);
$hasRemovedNode = true;
}
}
if ($hasRemovedNode) {
return $node;
}
return null;
}
/**
* @param If_[] $ifWithOnlyReturns
*/
private function isBoolVarIfCondReturnTrueNextReturnBoolVar(array $ifWithOnlyReturns): bool
{
if (count($ifWithOnlyReturns) > 1) {
return false;
}
/** @var Expr $cond */
$cond = $ifWithOnlyReturns[0]->cond;
if (! in_array($cond::class, [Variable::class, PropertyFetch::class, StaticPropertyFetch::class], true)) {
return false;
}
$type = $this->nodeTypeResolver->getType($cond);
if (! $type instanceof BooleanType) {
return false;
}
$next = $ifWithOnlyReturns[0]->getAttribute(AttributeKey::NEXT_NODE);
if (! $next instanceof Return_) {
return false;
}
$expr = $next->expr;
if (! $expr instanceof Expr) {
return false;
}
if (! $this->nodeComparator->areNodesEqual($expr, $cond)) {
return false;
}
/** @var Return_ $returnStmt */
$returnStmt = $ifWithOnlyReturns[0]->stmts[0];
if (! $returnStmt->expr instanceof Expr) {
return false;
}
return $this->valueResolver->isValue($returnStmt->expr, true);
}
/**
* @return If_[][]
*/
private function collectDuplicatedIfWithOnlyReturnByHash(FunctionLike $functionLike): array
{
$ifWithOnlyReturnsByHash = [];
$modifiedVariableNames = [];
foreach ((array) $functionLike->getStmts() as $stmt) {
if (! $this->ifManipulator->isIfWithOnly($stmt, Return_::class)) {
// variable modification
$modifiedVariableNames = array_merge(
$modifiedVariableNames,
$this->modifiedVariableNamesCollector->collectModifiedVariableNames($stmt)
);
continue;
}
if ($this->containsVariableNames($stmt, $modifiedVariableNames)) {
continue;
}
/** @var If_ $stmt */
$isFoundPropertyFetch = (bool) $this->betterNodeFinder->findFirst(
$stmt->cond,
fn (Node $node): bool => $this->propertyFetchAnalyzer->isPropertyFetch($node)
);
if ($isFoundPropertyFetch) {
continue;
}
$hash = $this->nodeComparator->printWithoutComments($stmt);
$ifWithOnlyReturnsByHash[$hash][] = $stmt;
}
return $ifWithOnlyReturnsByHash;
}
/**
* @param string[] $modifiedVariableNames
*/
private function containsVariableNames(Stmt $stmt, array $modifiedVariableNames): bool
{
if ($modifiedVariableNames === []) {
return false;
}
$containsVariableNames = false;
$this->traverseNodesWithCallable($stmt, function (Node $node) use (
$modifiedVariableNames,
&$containsVariableNames
): ?int {
if (! $node instanceof Variable) {
return null;
}
if (! $this->isNames($node, $modifiedVariableNames)) {
return null;
}
$containsVariableNames = true;
return NodeTraverser::STOP_TRAVERSAL;
});
return $containsVariableNames;
}
}