-
-
Notifications
You must be signed in to change notification settings - Fork 333
/
ChangeNestedForeachIfsToEarlyContinueRector.php
207 lines (172 loc) · 5.63 KB
/
ChangeNestedForeachIfsToEarlyContinueRector.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
<?php
declare(strict_types=1);
namespace Rector\EarlyReturn\Rector\Foreach_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Stmt\Continue_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\EarlyReturn\NodeTransformer\ConditionInverter;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector\ChangeNestedForeachIfsToEarlyContinueRectorTest
*/
final class ChangeNestedForeachIfsToEarlyContinueRector extends AbstractRector
{
public function __construct(
private readonly ConditionInverter $conditionInverter,
private readonly IfManipulator $ifManipulator
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change nested ifs to foreach with continue', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$items = [];
foreach ($values as $value) {
if ($value === 5) {
if ($value2 === 10) {
$items[] = 'maybe';
}
}
}
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$items = [];
foreach ($values as $value) {
if ($value !== 5) {
continue;
}
if ($value2 !== 10) {
continue;
}
$items[] = 'maybe';
}
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Foreach_::class];
}
/**
* @param Foreach_ $node
*/
public function refactor(Node $node): ?Node
{
$nestedIfsWithOnlyNonReturn = $this->ifManipulator->collectNestedIfsWithNonBreaking($node);
if (count($nestedIfsWithOnlyNonReturn) < 2) {
return null;
}
return $this->processNestedIfsWithNonBreaking($node, $nestedIfsWithOnlyNonReturn);
}
/**
* @param If_[] $nestedIfsWithOnlyReturn
*/
private function processNestedIfsWithNonBreaking(Foreach_ $foreach, array $nestedIfsWithOnlyReturn): Foreach_
{
// add nested if openly after this
$nestedIfsWithOnlyReturnCount = count($nestedIfsWithOnlyReturn);
// clear
$foreach->stmts = [];
foreach ($nestedIfsWithOnlyReturn as $key => $nestedIfWithOnlyReturn) {
// last item → the return node
if ($nestedIfsWithOnlyReturnCount === $key + 1) {
$finalReturn = clone $nestedIfWithOnlyReturn;
$this->addInvertedIfStmtWithContinue($nestedIfWithOnlyReturn, $foreach);
// should skip for weak inversion
if ($this->isBooleanOrWithWeakComparison($nestedIfWithOnlyReturn->cond)) {
continue;
}
$foreach->stmts = array_merge($foreach->stmts, $finalReturn->stmts);
} else {
$this->addInvertedIfStmtWithContinue($nestedIfWithOnlyReturn, $foreach);
}
}
return $foreach;
}
private function addInvertedIfStmtWithContinue(If_ $nestedIfWithOnlyReturn, Foreach_ $foreach): void
{
$invertedCondExpr = $this->conditionInverter->createInvertedCondition($nestedIfWithOnlyReturn->cond);
// special case
if ($invertedCondExpr instanceof BooleanNot && $invertedCondExpr->expr instanceof BooleanAnd) {
$leftExpr = $this->negateOrDeNegate($invertedCondExpr->expr->left);
$foreach->stmts[] = $this->createIfContinue($leftExpr);
$rightExpr = $this->negateOrDeNegate($invertedCondExpr->expr->right);
$foreach->stmts[] = $this->createIfContinue($rightExpr);
return;
}
// should skip for weak inversion
if ($this->isBooleanOrWithWeakComparison($nestedIfWithOnlyReturn->cond)) {
$foreach->stmts[] = $nestedIfWithOnlyReturn;
return;
}
$nestedIfWithOnlyReturn->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$nestedIfWithOnlyReturn->cond = $invertedCondExpr;
$nestedIfWithOnlyReturn->stmts = [new Continue_()];
$foreach->stmts[] = $nestedIfWithOnlyReturn;
}
/**
* Matches:
* $a == 1 || $b == 1
*
* Skips:
* $a === 1 || $b === 2
*/
private function isBooleanOrWithWeakComparison(Expr $expr): bool
{
if (! $expr instanceof BooleanOr) {
return false;
}
if ($expr->left instanceof Equal) {
return true;
}
if ($expr->left instanceof NotEqual) {
return true;
}
if ($expr->right instanceof Equal) {
return true;
}
return $expr->right instanceof NotEqual;
}
private function negateOrDeNegate(Expr $expr): Expr
{
if ($expr instanceof BooleanNot) {
return $expr->expr;
}
return new BooleanNot($expr);
}
private function createIfContinue(Expr $expr): If_
{
return new If_($expr, [
'stmts' => [new Continue_()],
]);
}
}