Unroll foreach over union of constant arrays in tryProcessUnrolledConstantArrayForeach#5558
Merged
ondrejmirtes merged 1 commit intophpstan:2.1.xfrom Apr 28, 2026
Conversation
…nstantArrayForeach`
- Support iterating over a union of multiple ConstantArrayType values
(e.g. `list{'a','b'}|list{'x','y'}`) in the foreach unrolling logic
- Previously, unrolling was restricted to exactly one constant array
(`count($constantArrays) !== 1`), causing unions to fall back to
the imprecise iterative processing that merged all possible keys
- Now each constant array in the union is unrolled independently with
its own chain scope, and the results are merged across arrays
- This preserves per-array type precision when building arrays inside
foreach loops over constant array unions
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
When building an array inside a
foreachloop that iterates over a union of constant arrays (e.g.list{'username', 'password'}|list{'app_id', 'app_key'}), PHPStan previously merged all possible keys from all union members into a single array with optional keys, rather than preserving per-array shapes as a union type.This caused false positives when the built array was passed to a function expecting a union of shaped arrays.
Changes
tryProcessUnrolledConstantArrayForeach()insrc/Analyser/NodeScopeResolver.phpto handle unions of multipleConstantArrayTypevaluescount($constantArrays) !== 1guard that previously rejected unionsFOREACH_UNROLL_LIMIT$originalScopeRoot cause
The
tryProcessUnrolledConstantArrayForeach()method only supported a single constant array (count($constantArrays) !== 1early return). When the iteratee was a union of constant arrays — which commonly occurs when the outer foreach over a constant array assigns different array values to the loop variable — the method bailed out and the standard iterative foreach processing ran instead. The iterative processing sees the union's combined iterable value type (e.g.'username'|'password'|'app_id'|'app_key') and produces a single array with all keys as optional, losing the per-array shape information.The fix processes each constant array in the union independently through the existing unrolling logic, preserving per-array type precision, then merges the results.
Analogous cases probed:
@param list{'a','b'}|list{'x','y'}) — also fixed by this changeTest
Added
tests/PHPStan/Analyser/nsrt/bug-7978.phpwith three test cases:@param list{'username','password'}|list{'app_id','app_key'}@param list{'a','b','c'}|list{'x'}All verify the inferred type is a union of the correct shaped arrays rather than a single array with all optional keys.
Fixes phpstan/phpstan#7978