Repository navigation
Keep the terminated iterations' scope after an unrolled constant-array foreach when no iteration leaves the loop - #6810
Open
phpstan-bot wants to merge 1 commit into
Conversation
…y `foreach` when no iteration leaves the loop - ForeachHandler::tryProcessUnrolledConstantArrayForeach() collects the end scope of every non-optional iteration whose body never reaches the next iteration. When no chain scope and no break scope remain (the code after the loop is unreachable), these scopes become the end scope. Previously the end scope stayed null, and the caller fell back to the generic loop pass. That pass sees the loop variable as the union of all values, so the subject lost its narrowing to `never`. This was a regression from 003d601. - Ported the same change to the turbo-ext mirror (ForeachHandler.cpp). - Analogous constructs: while/for/do-while loops are not unrolled, and the generic foreach path already falls back to the body result scope (`$finalScope ??= $finalScopeResult->getScope()`), so only the unrolled path was affected.
ondrejmirtes
force-pushed
the
create-pull-request/patch-6s60t7w
branch
from
October 27, 2026 18:45
1105b2d to
f2e4370
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Take a
foreachover a constant array where every iteration returns or throws, for exampleforeach ([Status::A, Status::B, Status::C] as $s) { if ($status === $s) return; }. Since 2.3.0, PHPStan no longer narrows the subject toneverafter such a loop. This PR restores the 2.2.x behaviour.Changes
src/Analyser/StmtHandler/ForeachHandler.php:tryProcessUnrolledConstantArrayForeach()now records the scope of each non-optional iteration that ends the unrolled chain because its body never reaches the next iteration. If no chain end scope and nobreakscope exist, it merges these scopes and uses them as the loop's end scope.turbo-ext/src/ForeachHandler.cpp: the same change in the native mirror. I checked that the regression test fails with the extension loaded and without the C++ change.Root cause
Commit 003d601 ("Build a loop's back edge from its continue statements when the body end is unreachable") made the unrolled path set the chain scope to
nullonce an iteration always terminates. With nobreak, the unrolled end scope then stayednull, andForeachHandlerused the generic final pass's scope instead. That pass binds the loop variable to the union of all values (Status::A|Status::B|Status::C), so$statuswas not narrowed. Before the commit, the unrolled path used the last iteration's end scope, where$statusisnever. The fix keeps that scope, but only as a fallback when the code after the loop is unreachable. It does not affect the possibly-undefined-variable fix from #14418, which still drops dead scopes whenever a real exit exists.Analogous constructs I probed:
while,foranddo-whileare never unrolled. The genericforeachpath already falls back to the body result scope ($finalScope ??= $finalScopeResult->getScope()). The case where the terminating iteration ends in a barereturnaftercontinuealready behaved the same way before the regression, because there the body's dead scope is not narrowed tonever. So only the unrolled path needed the fix.Test
tests/PHPStan/Analyser/nsrt/bug-15390.phpcovers:in_array()against nested constant arrays with a key variablethrowand a constant integer unionStatus::Cmust remain after the loopFixes phpstan/phpstan#15390
🤖 Generated with Claude Code