diff --git a/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/if_elseif_else.php.inc b/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/if_elseif_else.php.inc new file mode 100644 index 00000000000..0f2a926d295 --- /dev/null +++ b/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/if_elseif_else.php.inc @@ -0,0 +1,42 @@ + +----- + diff --git a/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/nested_if_with_terminating_elseif_and_else.php.inc b/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/nested_if_with_terminating_elseif_and_else.php.inc index ad03daa7cb3..be7b8010571 100644 --- a/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/nested_if_with_terminating_elseif_and_else.php.inc +++ b/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/nested_if_with_terminating_elseif_and_else.php.inc @@ -37,9 +37,7 @@ class NestedIfWithTerminatingElseIfAndElse if ($cond3) { return 'bar'; } - else { - return 'baz'; - } + return 'baz'; } foo(); } diff --git a/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/process_empty_return_last.php.inc b/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/process_empty_return_last.php.inc index ca6d518b0b9..d3989cd29e5 100644 --- a/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/process_empty_return_last.php.inc +++ b/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/process_empty_return_last.php.inc @@ -47,9 +47,7 @@ class ProcessEmptyReturnLast $value = 55; return 10; } - else { - return; - } + return; } public function secondRun($value) diff --git a/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php b/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php index 44da436638c..b977853751e 100644 --- a/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php +++ b/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php @@ -74,7 +74,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?array { - if ($this->doesLastStatementBreakFlow($node)) { + if ($this->doesNotLastStatementBreakFlow($node)) { return null; } @@ -108,7 +108,7 @@ private function handleElseIfs(If_ $if): array $currentElseIf = array_shift($if->elseifs); // If the last statement in the `elseif` breaks flow, merge it into the original `if` and stop processing - if ($this->doesLastStatementBreakFlow($currentElseIf)) { + if ($this->doesNotLastStatementBreakFlow($currentElseIf)) { $this->updateIfWithElseIf($if, $currentElseIf); $nodesToReturn = [...$nodesToReturn, $if, ...$this->getStatementsElseIfs($if)]; @@ -130,7 +130,18 @@ private function handleElseIfs(If_ $if): array } if ($originalIf->else instanceof Else_) { - $nodesToReturn[] = $originalIf->else; + $mergeStmts = true; + foreach ($nodesToReturn as $nodeToReturn) { + if ($this->doesNotLastStatementBreakFlow($nodeToReturn)) { + $nodesToReturn[] = $originalIf->else; + $mergeStmts = false; + break; + } + } + + if ($mergeStmts) { + $nodesToReturn = array_merge($nodesToReturn, $originalIf->else->stmts); + } } return $nodesToReturn; @@ -143,7 +154,7 @@ private function getStatementsElseIfs(If_ $if): array { $statements = []; foreach ($if->elseifs as $key => $elseif) { - if ($this->doesLastStatementBreakFlow($elseif) && $elseif->stmts !== []) { + if ($this->doesNotLastStatementBreakFlow($elseif) && $elseif->stmts !== []) { continue; } @@ -154,17 +165,17 @@ private function getStatementsElseIfs(If_ $if): array return $statements; } - private function doesLastStatementBreakFlow(If_ | ElseIf_ | Else_ $node): bool + private function doesNotLastStatementBreakFlow(If_ | ElseIf_ | Else_ $node): bool { $lastStmt = end($node->stmts); if ($lastStmt instanceof If_ && $lastStmt->else instanceof Else_) { - if ($this->doesLastStatementBreakFlow($lastStmt) || $this->doesLastStatementBreakFlow($lastStmt->else)) { + if ($this->doesNotLastStatementBreakFlow($lastStmt) || $this->doesNotLastStatementBreakFlow($lastStmt->else)) { return true; } foreach ($lastStmt->elseifs as $elseIf) { - if ($this->doesLastStatementBreakFlow($elseIf)) { + if ($this->doesNotLastStatementBreakFlow($elseIf)) { return true; } }