Skip to content

Commit 3cf1f51

Browse files
authored
[EarlyReturn] Handle If, elseIf, else all returned on RemoveAlwaysElseRector (#7659)
* [EarlyReturn] Handle If, elseIf, else all returned on RemoveAlwaysElseRector * Fix method name as it verify not break flow
1 parent b8da95b commit 3cf1f51

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Rector\Tests\EarlyReturn\Rector\If_\RemoveAlwaysElseRector\Fixture;
4+
5+
final class IfElseIfElse
6+
{
7+
public function run($value, $formatForEmail, $time_context, $direction)
8+
{
9+
if ($value == 0) {
10+
return $value;
11+
} elseif ($formatForEmail) {
12+
$value .= '|' . 1;
13+
14+
return $time_context . '&nbsp;&nbsp;//&nbsp;&nbsp;' . $direction . $value . ' minute(s)';
15+
} else {
16+
return $value;
17+
}
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\Tests\EarlyReturn\Rector\If_\RemoveAlwaysElseRector\Fixture;
26+
27+
final class IfElseIfElse
28+
{
29+
public function run($value, $formatForEmail, $time_context, $direction)
30+
{
31+
if ($value == 0) {
32+
return $value;
33+
}
34+
if ($formatForEmail) {
35+
$value .= '|' . 1;
36+
return $time_context . '&nbsp;&nbsp;//&nbsp;&nbsp;' . $direction . $value . ' minute(s)';
37+
}
38+
return $value;
39+
}
40+
}
41+
42+
?>

rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/nested_if_with_terminating_elseif_and_else.php.inc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ class NestedIfWithTerminatingElseIfAndElse
3737
if ($cond3) {
3838
return 'bar';
3939
}
40-
else {
41-
return 'baz';
42-
}
40+
return 'baz';
4341
}
4442
foo();
4543
}

rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/process_empty_return_last.php.inc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ class ProcessEmptyReturnLast
4747
$value = 55;
4848
return 10;
4949
}
50-
else {
51-
return;
52-
}
50+
return;
5351
}
5452

5553
public function secondRun($value)

rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function getNodeTypes(): array
7474
*/
7575
public function refactor(Node $node): ?array
7676
{
77-
if ($this->doesLastStatementBreakFlow($node)) {
77+
if ($this->doesNotLastStatementBreakFlow($node)) {
7878
return null;
7979
}
8080

@@ -108,7 +108,7 @@ private function handleElseIfs(If_ $if): array
108108
$currentElseIf = array_shift($if->elseifs);
109109

110110
// If the last statement in the `elseif` breaks flow, merge it into the original `if` and stop processing
111-
if ($this->doesLastStatementBreakFlow($currentElseIf)) {
111+
if ($this->doesNotLastStatementBreakFlow($currentElseIf)) {
112112
$this->updateIfWithElseIf($if, $currentElseIf);
113113
$nodesToReturn = [...$nodesToReturn, $if, ...$this->getStatementsElseIfs($if)];
114114

@@ -130,7 +130,18 @@ private function handleElseIfs(If_ $if): array
130130
}
131131

132132
if ($originalIf->else instanceof Else_) {
133-
$nodesToReturn[] = $originalIf->else;
133+
$mergeStmts = true;
134+
foreach ($nodesToReturn as $nodeToReturn) {
135+
if ($this->doesNotLastStatementBreakFlow($nodeToReturn)) {
136+
$nodesToReturn[] = $originalIf->else;
137+
$mergeStmts = false;
138+
break;
139+
}
140+
}
141+
142+
if ($mergeStmts) {
143+
$nodesToReturn = array_merge($nodesToReturn, $originalIf->else->stmts);
144+
}
134145
}
135146

136147
return $nodesToReturn;
@@ -143,7 +154,7 @@ private function getStatementsElseIfs(If_ $if): array
143154
{
144155
$statements = [];
145156
foreach ($if->elseifs as $key => $elseif) {
146-
if ($this->doesLastStatementBreakFlow($elseif) && $elseif->stmts !== []) {
157+
if ($this->doesNotLastStatementBreakFlow($elseif) && $elseif->stmts !== []) {
147158
continue;
148159
}
149160

@@ -154,17 +165,17 @@ private function getStatementsElseIfs(If_ $if): array
154165
return $statements;
155166
}
156167

157-
private function doesLastStatementBreakFlow(If_ | ElseIf_ | Else_ $node): bool
168+
private function doesNotLastStatementBreakFlow(If_ | ElseIf_ | Else_ $node): bool
158169
{
159170
$lastStmt = end($node->stmts);
160171

161172
if ($lastStmt instanceof If_ && $lastStmt->else instanceof Else_) {
162-
if ($this->doesLastStatementBreakFlow($lastStmt) || $this->doesLastStatementBreakFlow($lastStmt->else)) {
173+
if ($this->doesNotLastStatementBreakFlow($lastStmt) || $this->doesNotLastStatementBreakFlow($lastStmt->else)) {
163174
return true;
164175
}
165176

166177
foreach ($lastStmt->elseifs as $elseIf) {
167-
if ($this->doesLastStatementBreakFlow($elseIf)) {
178+
if ($this->doesNotLastStatementBreakFlow($elseIf)) {
168179
return true;
169180
}
170181
}

0 commit comments

Comments
 (0)