Use pre-args scope for value types in array_push/array_unshift#5579
Open
predictor2718 wants to merge 1 commit intophpstan:2.1.xfrom
Open
Use pre-args scope for value types in array_push/array_unshift#5579predictor2718 wants to merge 1 commit intophpstan:2.1.xfrom
predictor2718 wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
staabm
reviewed
May 1, 2026
| } | ||
|
|
||
| private function getArrayFunctionAppendingType(FunctionReflection $functionReflection, Scope $scope, FuncCall $expr): Type | ||
| private function getArrayFunctionAppendingType(FunctionReflection $functionReflection, Scope $scope, Scope $valuesScope, FuncCall $expr): Type |
Contributor
There was a problem hiding this comment.
please try whether we can just use a single Scope argument - meaning use scopeBeforeArgs for all calls in this method
Contributor
Author
There was a problem hiding this comment.
Done, simplified to pass $scopeBeforeArgs as the single scope argument.
…id false mutations from nested by-ref calls
staabm
approved these changes
May 1, 2026
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.
When
array_unshift($arr, array_pop($arr))is processed,processArgsevaluates the nestedarray_pop($arr)argument first, which mutates$arr's type in scope (e.g.non-empty-list<int>→list<int>). ThengetArrayFunctionAppendingTypere-evaluates the value argument expressions in the already-mutated scope, causingarray_pop($arr)to returnint|nullinstead ofint— and the resulting array type becomesnon-empty-list<int|null>instead ofnon-empty-list<int>.The fix captures the scope before
processArgsand passes it as a separate$valuesScopetogetArrayFunctionAppendingType. The array type (first arg) still uses the post-args scope (correctly reflecting the post-pop state), while the value types (remaining args) are evaluated using the pre-args scope (correctly reflecting what was actually passed).Closes phpstan/phpstan#13510