Fix phpstan/phpstan#14170: lost list in iterable type after iteration#5199
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Fix phpstan/phpstan#14170: lost list in iterable type after iteration#5199phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Override getOffsetValueType() on IterableType to return the item type instead of mixed - Skip generalOffsetAccessibleType intersection for iterable types in produceArrayDimFetchAssignValueToWrite to prevent decomposing iterable into array|ArrayAccess - Update existing iterable test to reflect correct offset value type - New regression test in tests/PHPStan/Analyser/nsrt/bug-14170.php
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 over an
iterable<string, list<string>>and modifying elements via nested array dim fetch assignment ($convert[$outerKey][$key] = ...), thelisttype was lost and the variable's type degraded toarray<string, mixed>|iterable<string, list<string>>instead of remainingiterable<string, list<string>>.Changes
getOffsetValueType()onIterableType(src/Type/IterableType.php) to return the actual item type instead ofmixed(fromMaybeOffsetAccessibleTypeTrait)generalOffsetAccessibleTypeintersection for iterable types inproduceArrayDimFetchAssignValueToWrite(src/Analyser/ExprHandler/AssignHandler.php) to prevent decomposingiterableintoarray|ArrayAccessuniontests/PHPStan/Analyser/nsrt/iterable.phpto reflect the now-correct offset value type for typed iterablesRoot cause
Two issues combined to cause the bug:
IterableType::getOffsetValueType()inherited fromMaybeOffsetAccessibleTypeTraitreturnedmixedinstead of the item type. When processing nested array dim fetch assignments like$convert[$outerKey][$key], the intermediate value type at$convert[$outerKey]becamemixed, losing thelist<string>information.In
produceArrayDimFetchAssignValueToWrite, the iterable type was intersected withgeneralOffsetAccessibleType(array|ArrayAccess|null), which decomposed theiterablecompound type into separatearrayandArrayAccessbranches, permanently losing the iterable identity.Test
Added regression test
tests/PHPStan/Analyser/nsrt/bug-14170.phpwith two test cases:example3a: Iterating with references (already worked correctly)example3b: Iterating with explicit key access and array dim assignment (was failing, now passes)Fixes phpstan/phpstan#14170