Repository navigation
Expand file tree
/
Copy pathForeachToArrayFindFactory.php
More file actions
183 lines (146 loc) · 5.4 KB
/
Copy pathForeachToArrayFindFactory.php
File metadata and controls
183 lines (146 loc) · 5.4 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
<?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\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
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 ForeachToArrayFindRector and ForeachToArrayFindKeyRector.
*/
final readonly class ForeachToArrayFindFactory
{
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 createArrayFindAssign(Node $node, string $functionName, bool $compareKey): ?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->valueResolver->isNull($prevAssign->expr)) {
continue;
}
if (! $prevAssign->var instanceof Variable) {
continue;
}
$assignedVariable = $prevAssign->var;
if (! $this->isValidForeachStructure($foreach, $assignedVariable, $compareKey)) {
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' => $condition,
]);
$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;
}
private function isValidForeachStructure(Foreach_ $foreach, Variable $assignedVariable, bool $compareKey): bool
{
if (count($foreach->stmts) !== 1) {
return false;
}
$comparedExpr = $compareKey ? $foreach->keyVar : $foreach->valueVar;
if (! $comparedExpr instanceof Expr) {
return false;
}
$firstStmt = $foreach->stmts[0];
if (
! $firstStmt instanceof If_ ||
count($firstStmt->stmts) !== 2
) {
return false;
}
if ($this->exprModifiedInConditionalAnalyzer->isModified($firstStmt->cond)) {
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->nodeComparator->areNodesEqual($assignment->expr, $comparedExpr)) {
return false;
}
if (! $foreach->valueVar instanceof Variable) {
return false;
}
$type = $this->nodeTypeResolver->getNativeType($foreach->expr);
return $type->isArray()
->yes();
}
}