diff --git a/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/nested_if_with_else_and_non_terminating_elseif.php.inc b/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/nested_if_with_else_and_non_terminating_elseif.php.inc new file mode 100644 index 00000000000..1f8aa07aaa6 --- /dev/null +++ b/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/nested_if_with_else_and_non_terminating_elseif.php.inc @@ -0,0 +1,49 @@ + +----- + diff --git a/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/nested_if_with_non_terminating_else.php.inc b/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/nested_if_with_non_terminating_else.php.inc new file mode 100644 index 00000000000..5f556ddf12c --- /dev/null +++ b/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/nested_if_with_non_terminating_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 new file mode 100644 index 00000000000..ad03daa7cb3 --- /dev/null +++ b/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/nested_if_with_terminating_elseif_and_else.php.inc @@ -0,0 +1,48 @@ + +----- + diff --git a/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php b/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php index caed85fc566..5b7cede4f00 100644 --- a/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php +++ b/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php @@ -128,12 +128,22 @@ private function getStatementsElseIfs(If_ $if): array return $statements; } - private function doesLastStatementBreakFlow(If_ | ElseIf_ $node): bool + private function doesLastStatementBreakFlow(If_ | ElseIf_ | Else_ $node): bool { $lastStmt = end($node->stmts); if ($lastStmt instanceof If_ && $lastStmt->else instanceof Else_) { - return $this->doesLastStatementBreakFlow($lastStmt); + if ($this->doesLastStatementBreakFlow($lastStmt) || $this->doesLastStatementBreakFlow($lastStmt->else)) { + return true; + } + + foreach ($lastStmt->elseifs as $elseIf) { + if ($this->doesLastStatementBreakFlow($elseIf)) { + return true; + } + } + + return false; } return ! ($lastStmt instanceof Return_