Fix phpstan/phpstan#13786: incorrect type-narrowing to *NEVER* inside array row#5263
Merged
staabm merged 2 commits intophpstan:2.1.xfrom Mar 21, 2026
Merged
Conversation
…nment - Added hasOffsetValueType check in AssignHandler to prevent using setExistingOffsetValueType when offset is known to not exist in the array - When the offset definitely doesn't exist (hasOffsetValueType returns no), fall through to setOffsetValueType which correctly widens the key type - Updated bug-13270b-php8 test expectation to reflect more precise type inference (hasOffsetValue instead of hasOffset) - New regression test in tests/PHPStan/Analyser/nsrt/bug-13786.php
staabm
approved these changes
Mar 21, 2026
VincentLanglet
approved these changes
Mar 21, 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
After a foreach loop that assigns to nested array dimensions with a union key type (e.g.,
$total[$id][$col] = '0'where$colis'a'|'b'|'c'), assigning a new key outside the union (e.g.,$total[$id]['d'] = '0') incorrectly narrowed the array type to*NEVER*.Changes
!$offsetValueType->hasOffsetValueType($offsetType)->no()condition insrc/Analyser/ExprHandler/AssignHandler.phpto prevent usingsetExistingOffsetValueTypewhen the offset is definitely not present in the arraytests/PHPStan/Analyser/nsrt/bug-13270b-php8.phpto reflect more precise type inference (hasOffsetValueinstead ofhasOffset)tests/PHPStan/Analyser/nsrt/bug-13786.phpRoot cause
When processing
$total[$id]['d'] = '0', the code checked whether the scope tracked the expression$total[$id]and, if so, usedsetExistingOffsetValueTypeto update it. However,setExistingOffsetValueTypeonArrayTypedoes not widen the key type (it keeps'a'|'b'|'c'). ThenHasOffsetValueType('d', '0')was intersected with the result, creating a contradiction (key'd'cannot exist in an array with key type'a'|'b'|'c'), which collapsed to*NEVER*.The fix adds a check: when the array is known to NOT have the offset being assigned (
hasOffsetValueTypereturnsno), the code falls through tosetOffsetValueTypeinstead, which correctly widens the key type to include the new offset.Test
Added
tests/PHPStan/Analyser/nsrt/bug-13786.phpwhich reproduces the original issue: a foreach loop assigns to$total[$id][$col]with$col: 'a'|'b'|'c', then$total[$id]['d'] = '0'is assigned outside the loop. The test verifies the type is correctlynon-empty-array<'a'|'b'|'c'|'d', '0'>instead of*NEVER*.Fixes phpstan/phpstan#13786