Repository navigation
Expand file tree
/
Copy pathForeachToArrayAnyAllFactory.php
More file actions
345 lines (275 loc) · 9.91 KB
/
Copy pathForeachToArrayAnyAllFactory.php
File metadata and controls
345 lines (275 loc) · 9.91 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
declare(strict_types=1);
namespace Rector\Php84\NodeFactory;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\NodeManipulator\StmtsManipulator;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\Php84\NodeAnalyzer\ExprModifiedInConditionalAnalyzer;
use Rector\Php84\NodeAnalyzer\ForeachKeyUsedInConditionalAnalyzer;
use Rector\PhpParser\Comparing\NodeComparator;
use Rector\PhpParser\Node\NodeFactory;
use Rector\PhpParser\Node\Value\ValueResolver;
/**
* Shared logic for ForeachToArrayAnyRector and ForeachToArrayAllRector.
*/
final readonly class ForeachToArrayAnyAllFactory
{
public function __construct(
private ValueResolver $valueResolver,
private StmtsManipulator $stmtsManipulator,
private ForeachKeyUsedInConditionalAnalyzer $foreachKeyUsedInConditionalAnalyzer,
private NodeComparator $nodeComparator,
private NodeTypeResolver $nodeTypeResolver,
private NodeFactory $nodeFactory,
private NodeNameResolver $nodeNameResolver,
private ExprModifiedInConditionalAnalyzer $exprModifiedInConditionalAnalyzer
) {
}
/**
* @param StmtsAware $node
*/
public function refactorToArrayAnyAll(
Node $node,
string $functionName,
bool $negateCondition,
bool $initialBool,
bool $rejectElseBranches
): ?Node {
return $this->refactorBooleanAssignmentPattern($node, $functionName, $negateCondition, $initialBool, $rejectElseBranches)
?? $this->refactorEarlyReturnPattern($node, $functionName, $negateCondition, $initialBool, $rejectElseBranches);
}
/**
* @param StmtsAware $node
*/
private function refactorBooleanAssignmentPattern(
Node $node,
string $functionName,
bool $negateCondition,
bool $initialBool,
bool $rejectElseBranches
): ?Node {
if ($node->stmts === null) {
return null;
}
foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof Foreach_) {
continue;
}
$prevStmt = $node->stmts[$key - 1] ?? null;
if (! $prevStmt instanceof Expression) {
continue;
}
if (! $prevStmt->expr instanceof Assign) {
continue;
}
$foreach = $stmt;
$prevAssign = $prevStmt->expr;
if (! $this->isExpectedBool($prevAssign->expr, $initialBool)) {
continue;
}
if (! $prevAssign->var instanceof Variable) {
continue;
}
$assignedVariable = $prevAssign->var;
if (! $this->isValidBooleanAssignmentForeachStructure($foreach, $assignedVariable, $initialBool, $rejectElseBranches)) {
continue;
}
if ($this->stmtsManipulator->isVariableUsedInNextStmt(
$node,
$key + 1,
(string) $this->nodeNameResolver->getName($foreach->valueVar)
)) {
continue;
}
/** @var If_ $firstNodeInsideForeach */
$firstNodeInsideForeach = $foreach->stmts[0];
$condition = $firstNodeInsideForeach->cond;
$valueParam = $foreach->valueVar;
if (! $valueParam instanceof Variable) {
continue;
}
$params = [new Param($valueParam)];
if ($foreach->keyVar instanceof Variable && $this->foreachKeyUsedInConditionalAnalyzer->isUsed(
$foreach->keyVar,
$condition
)) {
$params[] = new Param(new Variable((string) $this->nodeNameResolver->getName($foreach->keyVar)));
}
$arrowFunction = new ArrowFunction([
'params' => $params,
'expr' => $this->applyNegation($condition, $negateCondition),
]);
$funcCall = $this->nodeFactory->createFuncCall($functionName, [$foreach->expr, $arrowFunction]);
$newAssign = new Assign($assignedVariable, $funcCall);
$newExpression = new Expression($newAssign);
unset($node->stmts[$key - 1]);
$node->stmts[$key] = $newExpression;
$node->stmts = array_values($node->stmts);
return $node;
}
return null;
}
/**
* @param StmtsAware $node
*/
private function refactorEarlyReturnPattern(
Node $node,
string $functionName,
bool $negateCondition,
bool $initialBool,
bool $rejectElseBranches
): ?Node {
if ($node->stmts === null) {
return null;
}
foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof Foreach_) {
continue;
}
$foreach = $stmt;
$nextStmt = $node->stmts[$key + 1] ?? null;
if (! $nextStmt instanceof Return_) {
continue;
}
if (! $nextStmt->expr instanceof Expr) {
continue;
}
if (! $this->isExpectedBool($nextStmt->expr, $initialBool)) {
continue;
}
if (! $this->isValidEarlyReturnForeachStructure($foreach, $initialBool, $rejectElseBranches)) {
continue;
}
/** @var If_ $firstNodeInsideForeach */
$firstNodeInsideForeach = $foreach->stmts[0];
$condition = $firstNodeInsideForeach->cond;
$params = [];
if ($foreach->valueVar instanceof Variable) {
$params[] = new Param($foreach->valueVar);
}
if (
$foreach->keyVar instanceof Variable &&
$this->foreachKeyUsedInConditionalAnalyzer->isUsed($foreach->keyVar, $condition)
) {
$params[] = new Param(new Variable((string) $this->nodeNameResolver->getName($foreach->keyVar)));
}
$arrowFunction = new ArrowFunction([
'params' => $params,
'expr' => $this->applyNegation($condition, $negateCondition),
]);
$funcCall = $this->nodeFactory->createFuncCall($functionName, [$foreach->expr, $arrowFunction]);
$node->stmts[$key] = new Return_($funcCall);
unset($node->stmts[$key + 1]);
$node->stmts = array_values($node->stmts);
return $node;
}
return null;
}
private function isValidBooleanAssignmentForeachStructure(
Foreach_ $foreach,
Variable $assignedVariable,
bool $initialBool,
bool $rejectElseBranches
): bool {
if (count($foreach->stmts) !== 1) {
return false;
}
$firstStmt = $foreach->stmts[0];
if (
! $firstStmt instanceof If_ ||
count($firstStmt->stmts) !== 2
) {
return false;
}
if ($this->exprModifiedInConditionalAnalyzer->isModified($firstStmt->cond)) {
return false;
}
if ($rejectElseBranches && ($firstStmt->elseifs !== [] || $firstStmt->else instanceof Else_)) {
return false;
}
$assignmentStmt = $firstStmt->stmts[0];
$breakStmt = $firstStmt->stmts[1];
if (
! $assignmentStmt instanceof Expression ||
! $assignmentStmt->expr instanceof Assign ||
! $breakStmt instanceof Break_
) {
return false;
}
$assignment = $assignmentStmt->expr;
if (! $this->nodeComparator->areNodesEqual($assignment->var, $assignedVariable)) {
return false;
}
if (! $this->isExpectedBool($assignment->expr, ! $initialBool)) {
return false;
}
$type = $this->nodeTypeResolver->getNativeType($foreach->expr);
return $type->isArray()
->yes();
}
private function isValidEarlyReturnForeachStructure(
Foreach_ $foreach,
bool $initialBool,
bool $rejectElseBranches
): bool {
if (count($foreach->stmts) !== 1) {
return false;
}
if (! $foreach->stmts[0] instanceof If_) {
return false;
}
$ifStmt = $foreach->stmts[0];
if ($this->exprModifiedInConditionalAnalyzer->isModified($ifStmt->cond)) {
return false;
}
if ($rejectElseBranches && ($ifStmt->elseifs !== [] || $ifStmt->else instanceof Else_)) {
return false;
}
if (count($ifStmt->stmts) !== 1) {
return false;
}
if (! $ifStmt->stmts[0] instanceof Return_) {
return false;
}
$returnStmt = $ifStmt->stmts[0];
if (! $returnStmt->expr instanceof Expr) {
return false;
}
if (! $this->isExpectedBool($returnStmt->expr, ! $initialBool)) {
return false;
}
if (! $foreach->valueVar instanceof Variable) {
return false;
}
$type = $this->nodeTypeResolver->getNativeType($foreach->expr);
return $type->isArray()
->yes();
}
private function isExpectedBool(Expr $expr, bool $expected): bool
{
if ($expected) {
return $this->valueResolver->isTrue($expr);
}
return $this->valueResolver->isFalse($expr);
}
private function applyNegation(Expr $expr, bool $negate): Expr
{
if (! $negate) {
return $expr;
}
return $expr instanceof BooleanNot ? $expr->expr : new BooleanNot($expr);
}
}