Fix phpstan/phpstan#8270: False positive on array item modification#5189
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#8270: False positive on array item modification#5189phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- When modifying a sub-key of an array element at a non-constant offset (e.g. $list[$k]['test'] = true), PHPStan incorrectly replaced the item type for ALL elements instead of unioning with the original item type - Added shouldUnionExistingItemType() check in AssignHandler to detect when the composed value changes existing constant-array key values, forcing a union to preserve unmodified elements' types - Updated test expectations in bug-11679 and slevomat-foreach tests to reflect the more correct union behavior - New regression test in tests/PHPStan/Analyser/nsrt/bug-8270.php Closes phpstan/phpstan#8270
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 modifying a sub-key of an array element at a non-constant offset (e.g.,
$list[$k]['test'] = true), PHPStan incorrectly concluded that ALL array items had the modified value. This caused false positives like "If condition is always true" when iterating over the array and checking the modified key.Changes
shouldUnionExistingItemType()method insrc/Analyser/ExprHandler/AssignHandler.phpthat checks whether a composed value changes existing constant-array key valuesproduceArrayDimFetchAssignValueToWrite()to forceunionValues=trueat the outermost dimension when the composed value has conflicting key values with the existing item typetests/PHPStan/Rules/Arrays/data/bug-11679.phpandtests/PHPStan/Rules/Arrays/data/slevomat-foreach-array-key-exists-bug.phpto match the more correct union behaviortests/PHPStan/Analyser/nsrt/bug-8270.phpRoot cause
In
produceArrayDimFetchAssignValueToWrite(), nested array dimension assignments like$list[$k]['test'] = trueare processed bottom-up. The inner dimension correctly computes the modified element value (e.g.,array{test: true, value: int}). However, at the outer dimension,setOffsetValueType()was called withunionValues=false, which replaced the entire item type with the composed value. Since the offset key is non-constant (could match any element), this incorrectly assumed ALL elements had the new value.The fix detects when the composed value changes existing key values (e.g.,
test: false→test: true) by comparing key-by-key. When a conflict is found, it forcesunionValues=trueso the item type becomes the union of old and new (e.g.,test: bool), correctly representing that some elements retain their original values.The check is limited to the outermost dimension to avoid issues with deeply nested array building patterns (like bug-1388).
Test
Added
tests/PHPStan/Analyser/nsrt/bug-8270.phpwith two test functions:@var-annotatednon-empty-listand constant offset$list[0]['test'] = truearray_key_first(), modifying one element, then iteratingBoth verify that after modifying one element's sub-key, the foreach item type correctly shows
bool(nottrue) for the modified key.Fixes phpstan/phpstan#8270