Fix phpstan/phpstan#13669: Offset -1|0|1|2 might not exist on non-empty-array<-1|0|1|2, int>.#5266
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Conversation
… loop
When a constant array shape (e.g. array{-1: 0, 0: 0, 2: 0, 1: 0}) was
assigned to a nested property dim fetch like $this->prop[$key], and then
an inner loop modified it via ++$this->prop[$key][$offset], the constant
shape was lost during loop processing. This caused false positive
"Offset might not exist" errors even though all offsets were explicitly
set.
Two changes fix this:
1. In AssignHandler's descending phase, when the scope tracks a more
precise type for an intermediate dim fetch and that type has all the
offsets needed by the next level, use the scope's tracked type instead
of recomputing from the parent's general array type.
2. In MutatingScope::generalizeVariableTypeHolders, when a parent
expression is generalized and both scopes track a child expression as
constant arrays with matching keys, generalize the child instead of
invalidating it. This preserves the constant array shape through loop
generalization.
Fixes phpstan/phpstan#13669
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
++$arr[$key][$offset]Test plan
testBug13669inNonexistentOffsetInArrayDimFetchRuleTestthat verifies no false positive is reportedpr-4390,bug-10438) continue to passFixes phpstan/phpstan#13669