Conversation
- Changed SetOffsetValueTypeExpr to SetExistingOffsetValueTypeExpr in MutatingScope::enterForeach() for the no-key by-reference case - In foreach ($list as &$value), values are modified at existing offsets, not new ones, so setExistingOffsetValueType must be used to preserve AccessoryArrayListType - New regression test in tests/PHPStan/Analyser/nsrt/bug-13809.php Closes phpstan/phpstan#13809
This was referenced Feb 15, 2026
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 iterating a
list<mixed>withforeach ($list as &$value)(without a key variable) and modifying$value, PHPStan incorrectly degraded the type toarray<int<0, max>, mixed>, losing the list property. The same loop withforeach ($list as $key => &$value)correctly preserved the list type.Changes
SetOffsetValueTypeExprtoSetExistingOffsetValueTypeExprinMutatingScope::enterForeach()atsrc/Analyser/MutatingScope.php:3026for the no-key by-reference casetests/PHPStan/Analyser/nsrt/bug-13809.phpCLAUDE.mdwith documentation about the SetExistingOffsetValueTypeExpr vs SetOffsetValueTypeExpr distinction in foreach contextRoot cause
In
MutatingScope::enterForeach(), when creating theIntertwinedVariableByReferenceWithExprforforeach ($list as &$value)(no key variable), the code usedSetOffsetValueTypeExprto represent the array modification. This expression callssetOffsetValueType()on the array type, which inAccessoryArrayListTypereturnsErrorTypefor non-null/non-zero offset types (likeint<0, max>). TheErrorTyperesult destroys the list accessory type during intersection resolution.The fix uses
SetExistingOffsetValueTypeExprinstead, which callssetExistingOffsetValueType(). This is semantically correct because a foreach loop only modifies values at existing offsets, never adding new ones.AccessoryArrayListType::setExistingOffsetValueType()returns$this, correctly preserving the list type.Test
The regression test covers:
foreach ($list as &$value)without key — verifies list type is preserved (was the reported bug)foreach ($list as $key => &$value)with key — verifies list type is preserved (already worked)foreach ($list as &$value)with typed list (list<string>) — verifies typed list is preservedforeach ($list as &$value)with arithmetic modification — verifies typed list is preservedFixes phpstan/phpstan#13809
Closes phpstan/phpstan#1311
Closes phpstan/phpstan#13851
Closes phpstan/phpstan#14083
Closes phpstan/phpstan#14084