From ca9fdce43cd5d5f21277a68f37037060678241d6 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 11 Sep 2026 11:34:29 +0200 Subject: [PATCH] [Php84] Fix ForeachToArrayAnyRector dropping elseif/else branches array_any only takes a single condition, so a foreach whose if has elseif/else branches cannot be represented without losing logic. Bail out when the if carries elseifs or an else instead of keeping only the first condition. Fixes #9900 Claude-Session: https://claude.ai/code/session_01APZKnKT7DBy7AWZ7z3MGDa --- ...ly_return_skip_if_with_elseif_else.php.inc | 26 +++++++++++++++++++ .../Fixture/skip_boolean_if_with_else.php.inc | 20 ++++++++++++++ .../Foreach_/ForeachToArrayAnyRector.php | 9 +++++++ 3 files changed, 55 insertions(+) create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_if_with_elseif_else.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_boolean_if_with_else.php.inc diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_if_with_elseif_else.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_if_with_elseif_else.php.inc new file mode 100644 index 00000000000..93836864346 --- /dev/null +++ b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_if_with_elseif_else.php.inc @@ -0,0 +1,26 @@ +elseifs !== [] || $firstStmt->else instanceof Else_) { + return false; + } + $assignmentStmt = $firstStmt->stmts[0]; $breakStmt = $firstStmt->stmts[1]; @@ -315,6 +320,10 @@ private function isValidEarlyReturnForeachStructure(Foreach_ $foreach): bool $ifStmt = $foreach->stmts[0]; + if ($ifStmt->elseifs !== [] || $ifStmt->else instanceof Else_) { + return false; + } + if (count($ifStmt->stmts) !== 1) { return false; }