From 54feb0dcfaeaafe177d1268ca2117a7fb1a7da1a Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 24 Aug 2022 03:51:27 +0700 Subject: [PATCH] [EarlyReturn] Handle crash on assign in if else before on RemoveAlwaysElseRector (#2822) Co-authored-by: Shin <2082119+shinsenter@users.noreply.github.com> Co-authored-by: GitHub Action --- .../Fixture/assign_in_if_else_before.php.inc | 55 +++++++++++++++++++ .../Rector/If_/RemoveAlwaysElseRector.php | 4 ++ 2 files changed, 59 insertions(+) create mode 100644 rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/assign_in_if_else_before.php.inc diff --git a/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/assign_in_if_else_before.php.inc b/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/assign_in_if_else_before.php.inc new file mode 100644 index 00000000000..42fb9655102 --- /dev/null +++ b/rules-tests/EarlyReturn/Rector/If_/RemoveAlwaysElseRector/Fixture/assign_in_if_else_before.php.inc @@ -0,0 +1,55 @@ + +----- + diff --git a/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php b/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php index 52fbbc5521f..51aa5f531ff 100644 --- a/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php +++ b/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php @@ -132,6 +132,10 @@ private function doesLastStatementBreakFlow(If_ | ElseIf_ $node): bool { $lastStmt = end($node->stmts); + if ($lastStmt instanceof If_ && $lastStmt->else instanceof Else_) { + return $this->doesLastStatementBreakFlow($lastStmt); + } + return ! ($lastStmt instanceof Return_ || $lastStmt instanceof Throw_ || $lastStmt instanceof Continue_