Fix phpstan/phpstan#9691: Preserve per-key array types during loop generalization when values mix arrays and scalars#5473
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…neralization when values mix arrays and scalars When an array has different keys holding structurally different value types (e.g., one key holds an int, another holds an array), loop type generalization would collapse all per-key type information into a general array<K, V>, causing false "Cannot access offset" errors when accessing keys that are known to hold array values. Add a new branch in MutatingScope::generalizeType() that detects when the wider array (B) has keys that are a superset of the narrower (A) and the value types span both array and non-array types. In this case, perform per-key value type merging to preserve structural type info.
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
Fixes phpstan/phpstan#9691
When an array inside a loop has different keys holding structurally different value types (e.g., key
0holds anintand key1holds anarray{abc: 'def'}), the loop type generalization inMutatingScope::generalizeType()would collapse all per-key type information into a generalarray<K, V>type. This caused false positive "Cannot access offset 'abc' on0|array{abc: 'def'}" errors when accessing keys that are known to hold array values.Root cause
In
generalizeType(), when constant arrays A and B have different key sets (B has more keys than A), the code always fell through to the generalArrayTypefallback. This merged all value types into a single union, losing per-key precision. For the reported bug:array{1: array{abc: 'def'}}(from first loop iteration)array{0?: 0, 1: array{abc: 'def'}}(from second iteration, conditional assignment added key 0)array<0|1, 0|array{abc: 'def'}>— accessing key 1 now gives0|array{abc: 'def'}instead ofarray{abc: 'def'}Fix
Added a new
elseifbranch in the constant array generalization section that activates when:When all three conditions are met, the code performs per-key value type merging using B's key set, preserving the structural type information for each key. Keys present only in one side are handled correctly (with optional markers for keys not present in both).
The structural mixing check (
hasStructurallyMixedValueTypes) prevents this branch from activating for growing-list patterns (where all values are the same scalar type), which must still be generalized tolist<T>.Test plan
tests/PHPStan/Analyser/nsrt/bug-9691.phpthat asserts$issues[1]has typearray{abc: 'def'}inside the loop