-
-
Notifications
You must be signed in to change notification settings - Fork 381
/
Copy pathIfManipulator.php
230 lines (183 loc) · 5.68 KB
/
IfManipulator.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
<?php
declare(strict_types=1);
namespace Rector\Core\NodeManipulator;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\Exit_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
final class IfManipulator
{
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly StmtsManipulator $stmtsManipulator,
private readonly ValueResolver $valueResolver,
private readonly NodeComparator $nodeComparator
) {
}
/**
* Matches:
*
* if (<$value> !== null) {
* return $value;
* }
*/
public function matchIfNotNullReturnValue(If_ $if): ?Expr
{
if (count($if->stmts) !== 1) {
return null;
}
$insideIfNode = $if->stmts[0];
if (! $insideIfNode instanceof Return_) {
return null;
}
if (! $if->cond instanceof NotIdentical) {
return null;
}
return $this->matchComparedAndReturnedNode($if->cond, $insideIfNode);
}
/**
* @return If_[]
*/
public function collectNestedIfsWithOnlyReturn(If_ $if): array
{
$ifs = [];
$currentIf = $if;
while ($this->isIfWithOnlyStmtIf($currentIf)) {
$ifs[] = $currentIf;
/** @var If_ $currentIf */
$currentIf = $currentIf->stmts[0];
}
if ($ifs === []) {
return [];
}
if (! $this->hasOnlyStmtOfType($currentIf, Return_::class)) {
return [];
}
// last if is with the return value
$ifs[] = $currentIf;
return $ifs;
}
public function isIfAndElseWithSameVariableAssignAsLastStmts(If_ $if, Expr $desiredExpr): bool
{
if (! $if->else instanceof Else_) {
return false;
}
if ((bool) $if->elseifs) {
return false;
}
$lastIfNode = $this->stmtsManipulator->getUnwrappedLastStmt($if->stmts);
if (! $lastIfNode instanceof Assign) {
return false;
}
$lastElseNode = $this->stmtsManipulator->getUnwrappedLastStmt($if->else->stmts);
if (! $lastElseNode instanceof Assign) {
return false;
}
if (! $lastIfNode->var instanceof Variable) {
return false;
}
if (! $this->nodeComparator->areNodesEqual($lastIfNode->var, $lastElseNode->var)) {
return false;
}
return $this->nodeComparator->areNodesEqual($desiredExpr, $lastElseNode->var);
}
/**
* @return If_[]
*/
public function collectNestedIfsWithNonBreaking(Foreach_ $foreach): array
{
if (count($foreach->stmts) !== 1) {
return [];
}
$onlyForeachStmt = $foreach->stmts[0];
if (! $onlyForeachStmt instanceof If_) {
return [];
}
$ifs = [];
$currentIf = $onlyForeachStmt;
while ($this->isIfWithOnlyStmtIf($currentIf)) {
$ifs[] = $currentIf;
/** @var If_ $currentIf */
$currentIf = $currentIf->stmts[0];
}
// IfManipulator is not build to handle elseif and else
if (! $this->isIfWithoutElseAndElseIfs($currentIf)) {
return [];
}
$return = $this->betterNodeFinder->findFirstInstanceOf($currentIf->stmts, Return_::class);
if ($return instanceof Return_) {
return [];
}
$exit = $this->betterNodeFinder->findFirstInstanceOf($currentIf->stmts, Exit_::class);
if ($exit instanceof Exit_) {
return [];
}
// last if is with the expression
$ifs[] = $currentIf;
return $ifs;
}
/**
* @param class-string<Stmt> $stmtClass
*/
public function isIfWithOnly(If_ $if, string $stmtClass): bool
{
if (! $this->isIfWithoutElseAndElseIfs($if)) {
return false;
}
return $this->hasOnlyStmtOfType($if, $stmtClass);
}
public function isIfWithOnlyOneStmt(If_ $if): bool
{
return count($if->stmts) === 1;
}
public function isIfWithoutElseAndElseIfs(If_ $if): bool
{
if ($if->else instanceof Else_) {
return false;
}
return $if->elseifs === [];
}
private function matchComparedAndReturnedNode(NotIdentical $notIdentical, Return_ $return): ?Expr
{
if ($this->nodeComparator->areNodesEqual(
$notIdentical->left,
$return->expr
) && $this->valueResolver->isNull($notIdentical->right)) {
return $notIdentical->left;
}
if (! $this->nodeComparator->areNodesEqual($notIdentical->right, $return->expr)) {
return null;
}
if ($this->valueResolver->isNull($notIdentical->left)) {
return $notIdentical->right;
}
return null;
}
private function isIfWithOnlyStmtIf(If_ $if): bool
{
if (! $this->isIfWithoutElseAndElseIfs($if)) {
return false;
}
return $this->hasOnlyStmtOfType($if, If_::class);
}
/**
* @param class-string<Stmt> $stmtClass
*/
private function hasOnlyStmtOfType(If_ $if, string $stmtClass): bool
{
$stmts = $if->stmts;
if (count($stmts) !== 1) {
return false;
}
return $stmts[0] instanceof $stmtClass;
}
}