Skip to content

Commit 46515d2

Browse files
[EarlyReturn] Split nested && operand into its own early return in ReturnBinaryOrToEarlyReturnRector (#8156)
* [EarlyReturn] Split nested && operand into its own early return in ReturnBinaryOrToEarlyReturnRector * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 1ccbb46 commit 46515d2

4 files changed

Lines changed: 86 additions & 64 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector\Fixture;
4+
5+
class AndInNext
6+
{
7+
public function accept()
8+
{
9+
return $this->something() || $this->somethingelse() && $this->anotherelse() || $this->last();
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector\Fixture;
18+
19+
class AndInNext
20+
{
21+
public function accept()
22+
{
23+
if ($this->something()) {
24+
return true;
25+
}
26+
if ($this->somethingelse() && $this->anotherelse()) {
27+
return true;
28+
}
29+
return (bool) $this->last();
30+
}
31+
}
32+
33+
?>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Rector\Tests\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector\Fixture;
4+
5+
class AndNestedDeep
6+
{
7+
public function hasStale(): bool
8+
{
9+
return $this->donation === null
10+
|| ($this->commission > 0 && $this->transaction === null)
11+
|| $this->somethingElse()
12+
|| $this->anotherThing();
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector\Fixture;
21+
22+
class AndNestedDeep
23+
{
24+
public function hasStale(): bool
25+
{
26+
if ($this->donation === null) {
27+
return true;
28+
}
29+
if ($this->commission > 0 && $this->transaction === null) {
30+
return true;
31+
}
32+
if ($this->somethingElse()) {
33+
return true;
34+
}
35+
return (bool) $this->anotherThing();
36+
}
37+
}
38+
39+
?>

‎rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryOrToEarlyReturnRector/Fixture/skip_and_in_next.php.inc‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎rules/EarlyReturn/Rector/Return_/ReturnBinaryOrToEarlyReturnRector.php‎

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
9-
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
109
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
1110
use PhpParser\Node\Stmt\If_;
1211
use PhpParser\Node\Stmt\Return_;
@@ -89,12 +88,15 @@ public function refactor(Node $node): ?Node
8988

9089
$booleanOr = $stmt->expr;
9190

91+
// the right side becomes the final return, the left operands become early returns
9292
$left = $booleanOr->left;
93-
$ifs = $this->createMultipleIfs($left, $stmt, []);
93+
$leftOperands = $left instanceof BooleanOr ? $this->flattenBooleanOr($left) : [$left];
9494

95-
// ensure ifs not removed by other rules
96-
if ($ifs === []) {
97-
continue;
95+
$ifs = [];
96+
foreach ($leftOperands as $leftOperand) {
97+
$ifs[] = new If_($leftOperand, [
98+
'stmts' => [new Return_($this->nodeFactory->createTrue())],
99+
]);
98100
}
99101

100102
if (! $this->callAnalyzer->doesIfHasObjectCall($ifs)) {
@@ -119,56 +121,17 @@ public function refactor(Node $node): ?Node
119121
}
120122

121123
/**
122-
* @param If_[] $ifs
123-
* @return If_[]
124-
*/
125-
private function createMultipleIfs(Expr $expr, Return_ $return, array $ifs): array
126-
{
127-
while ($expr instanceof BooleanOr) {
128-
$ifs = [...$ifs, ...$this->collectLeftBooleanOrToIfs($expr, $return, $ifs)];
129-
$ifs[] = new If_($expr->right, [
130-
'stmts' => [new Return_($this->nodeFactory->createTrue())],
131-
]);
132-
133-
$expr = $expr->right;
134-
if ($expr instanceof BooleanAnd) {
135-
return [];
136-
}
137-
138-
if (! $expr instanceof BooleanOr) {
139-
continue;
140-
}
141-
142-
return [];
143-
}
144-
145-
$lastIf = new If_($expr, [
146-
'stmts' => [new Return_($this->nodeFactory->createTrue())],
147-
]);
148-
149-
// if empty, fallback to last if
150-
if ($ifs === []) {
151-
return [$lastIf];
152-
}
153-
154-
return $ifs;
155-
}
156-
157-
/**
158-
* @param If_[] $ifs
159-
* @return If_[]
124+
* Flatten a left-associative "||" chain into its operands, in source order.
125+
*
126+
* @return Expr[]
160127
*/
161-
private function collectLeftBooleanOrToIfs(BooleanOr $booleanOr, Return_ $return, array $ifs): array
128+
private function flattenBooleanOr(BooleanOr $booleanOr): array
162129
{
163130
$left = $booleanOr->left;
164-
if (! $left instanceof BooleanOr) {
165-
$returnTrueIf = new If_($left, [
166-
'stmts' => [new Return_($this->nodeFactory->createTrue())],
167-
]);
168131

169-
return [$returnTrueIf];
170-
}
132+
$operands = $left instanceof BooleanOr ? $this->flattenBooleanOr($left) : [$left];
133+
$operands[] = $booleanOr->right;
171134

172-
return $this->createMultipleIfs($left, $return, $ifs);
135+
return $operands;
173136
}
174137
}

0 commit comments

Comments
 (0)