-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
SimplifyIfReturnBoolRector.php
262 lines (210 loc) · 7.51 KB
/
SimplifyIfReturnBoolRector.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
use Rector\CodeQuality\NodeManipulator\ExprBoolCaster;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\PhpParser\Printer\BetterStandardPrinter;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector\SimplifyIfReturnBoolRectorTest
*/
final class SimplifyIfReturnBoolRector extends AbstractRector
{
public function __construct(
private readonly CommentsMerger $commentsMerger,
private readonly ExprBoolCaster $exprBoolCaster,
private readonly BetterStandardPrinter $betterStandardPrinter,
private readonly ValueResolver $valueResolver
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Shortens if return false/true to direct return',
[
new CodeSample(
<<<'CODE_SAMPLE'
if (strpos($docToken->getContent(), "\n") === false) {
return true;
}
return false;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
return strpos($docToken->getContent(), "\n") === false;
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StmtsAwareInterface::class];
}
/**
* @param StmtsAwareInterface $node
*/
public function refactor(Node $node): ?Node
{
if ($node->stmts === null) {
return null;
}
foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof Return_) {
continue;
}
$previousStmt = $node->stmts[$key - 1] ?? null;
if (! $previousStmt instanceof If_) {
continue;
}
$if = $previousStmt;
if ($this->shouldSkipIfAndReturn($previousStmt, $stmt)) {
continue;
}
$return = $stmt;
/** @var Return_ $ifInnerNode */
$ifInnerNode = $if->stmts[0];
$innerIfInnerNode = $ifInnerNode->expr;
if (! $innerIfInnerNode instanceof Expr) {
continue;
}
$newReturn = $this->resolveReturn($innerIfInnerNode, $if, $return);
if (! $newReturn instanceof Return_) {
continue;
}
$this->commentsMerger->keepComments($newReturn, [$if, $return, $ifInnerNode]);
// remove previous IF
unset($node->stmts[$key - 1]);
$node->stmts[$key] = $newReturn;
return $node;
}
return null;
}
private function shouldSkipIfAndReturn(If_ $if, Return_ $return): bool
{
if ($if->elseifs !== []) {
return true;
}
if ($this->isElseSeparatedThenIf($if)) {
return true;
}
if (! $this->isIfWithSingleReturnExpr($if)) {
return true;
}
/** @var Return_ $ifInnerNode */
$ifInnerNode = $if->stmts[0];
/** @var Expr $returnedExpr */
$returnedExpr = $ifInnerNode->expr;
if (! $this->valueResolver->isTrueOrFalse($returnedExpr)) {
return true;
}
if (! $return->expr instanceof Expr) {
return true;
}
// negate + negate → skip for now
if (! $this->valueResolver->isFalse($returnedExpr)) {
return ! $this->valueResolver->isTrueOrFalse($return->expr);
}
$condString = $this->betterStandardPrinter->print($if->cond);
if (! \str_contains($condString, '!=')) {
return ! $this->valueResolver->isTrueOrFalse($return->expr);
}
return ! $if->cond instanceof NotIdentical && ! $if->cond instanceof NotEqual;
}
private function processReturnTrue(If_ $if, Return_ $nextReturn): Return_
{
if ($if->cond instanceof BooleanNot && $nextReturn->expr instanceof Expr && $this->valueResolver->isTrue(
$nextReturn->expr
)) {
return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded($if->cond->expr));
}
return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded($if->cond));
}
private function processReturnFalse(If_ $if, Return_ $nextReturn): ?Return_
{
if ($if->cond instanceof Identical) {
$notIdentical = new NotIdentical($if->cond->left, $if->cond->right);
return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded($notIdentical));
}
if ($if->cond instanceof Equal) {
$notIdentical = new NotEqual($if->cond->left, $if->cond->right);
return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded($notIdentical));
}
if (! $nextReturn->expr instanceof Expr) {
return null;
}
if (! $this->valueResolver->isTrue($nextReturn->expr)) {
return null;
}
if ($if->cond instanceof BooleanNot) {
return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded($if->cond->expr));
}
return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded(new BooleanNot($if->cond)));
}
/**
* Matches: "else if"
*/
private function isElseSeparatedThenIf(If_ $if): bool
{
if (! $if->else instanceof Else_) {
return false;
}
if (count($if->else->stmts) !== 1) {
return false;
}
$onlyStmt = $if->else->stmts[0];
return $onlyStmt instanceof If_;
}
private function isIfWithSingleReturnExpr(If_ $if): bool
{
if (count($if->stmts) !== 1) {
return false;
}
if ($if->elseifs !== []) {
return false;
}
$ifInnerNode = $if->stmts[0];
if (! $ifInnerNode instanceof Return_) {
return false;
}
// return must have value
return $ifInnerNode->expr instanceof Expr;
}
private function resolveReturn(Expr $innerExpr, If_ $if, Return_ $return): ?Return_
{
if ($this->valueResolver->isTrue($innerExpr)) {
return $this->processReturnTrue($if, $return);
}
if ($this->valueResolver->isFalse($innerExpr)) {
/** @var Expr $expr */
$expr = $return->expr;
if ($if->cond instanceof NotIdentical && $this->valueResolver->isTrue($expr)) {
$if->cond = new Identical($if->cond->left, $if->cond->right);
return $this->processReturnTrue($if, $return);
}
if ($if->cond instanceof NotEqual && $this->valueResolver->isTrue($expr)) {
$if->cond = new Equal($if->cond->left, $if->cond->right);
return $this->processReturnTrue($if, $return);
}
return $this->processReturnFalse($if, $return);
}
return null;
}
}