Fix phpstan/phpstan#10089: Multi-dimensional array shape wrongly inferred#5259
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#10089: Multi-dimensional array shape wrongly inferred#5259phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- When assigning to a nested array with a non-constant outer key like $matrix[$size - 1][8] = 3, the HasOffsetValueType(8, 3) from the inner dimension was incorrectly propagated to ALL entries of the outer array - Added logic in AssignHandler::produceArrayDimFetchAssignValueToWrite() to strip HasOffsetValueType from the inner value when the outer offset is non-constant and the inner dimension was not a tracked expression (i.e., not from a foreach binding) - New regression test in tests/PHPStan/Analyser/nsrt/bug-10089.php Fixes phpstan/phpstan#10089
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 assigning to a multi-dimensional array with a non-constant outer key (e.g.,
$matrix[$size - 1][8] = 3), PHPStan incorrectly appliedhasOffsetValue(8, 3)to ALL inner arrays instead of just the one at the specified index. This caused false positive errors like "Strict comparison using === between 3 and 0 will always evaluate to false" when accessing other rows of the matrix.Changes
src/Analyser/ExprHandler/AssignHandler.phpin theproduceArrayDimFetchAssignValueToWrite()method$previousIterationUsedExistingBranchflag to track whether the inner dimension's processing used the "existing expression" branch (which indicates the expression is tracked in scope, e.g., from a foreach binding)HasOffsetValueTypeaccessories are stripped from the inner value before applying it to the outer arrayhasOffsetValueto all array entries while preserving correct behavior for foreach loops where all entries are genuinely modifiedRoot cause
In
produceArrayDimFetchAssignValueToWrite(), nested array assignments are processed from innermost to outermost dimension. When the inner dimension has a constant offset (like[8]),HasOffsetValueType(8, 3)is correctly added to represent that specific inner array entry. However, when the outer dimension has a non-constant offset (like[$size - 1]),setOffsetValueTypewith$unionValues = falsereplaces ALL entries' value type with the inner result — including theHasOffsetValueTypethat should only apply to one specific entry.The fix strips
HasOffsetValueTypefrom the inner value before propagating to the outer dimension, but only when the inner expression is not tracked in scope (distinguishing single assignments from foreach loops where all entries are modified).Test
Added
tests/PHPStan/Analyser/nsrt/bug-10089.phpwhich reproduces the original issue: creating a matrix witharray_fill, assigning to one row, and verifying that the matrix type doesn't claim all rows have the modified offset value.Fixes phpstan/phpstan#10089