Fix phpstan/phpstan#14019: Assignment in a key of array destructuring is ignored#5164
Merged
staabm merged 2 commits intophpstan:2.1.xfrom Mar 9, 2026
Merged
Conversation
- Propagate scope from key expression processing in list/array destructuring - Previously the scope result from processing key expressions was discarded, so assignments like `[($a = 'foo') => $b] = [...]` left `$a` undefined - New regression test in tests/PHPStan/Analyser/nsrt/bug-14019.php Closes phpstan/phpstan#14019
staabm
reviewed
Mar 9, 2026
| $throwPoints = array_merge($throwPoints, $keyResult->getThrowPoints()); | ||
| $impurePoints = array_merge($impurePoints, $keyResult->getImpurePoints()); | ||
| $isAlwaysTerminating = $isAlwaysTerminating || $keyResult->isAlwaysTerminating(); | ||
| // no need for $keyResult->getScope() |
staabm
approved these changes
Mar 9, 2026
VincentLanglet
approved these changes
Mar 9, 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.
Summary
When using an assignment expression as a key in array destructuring (e.g.,
[($a = 'foo') => $b] = ['foo' => 1]), PHPStan incorrectly reported$aas possibly undefined with typemixed. The variable should be recognized as assigned with its correct type.Changes
src/Analyser/ExprHandler/AssignHandler.phpto propagate the scope returned from processing key expressions in array destructuringtests/PHPStan/Analyser/nsrt/bug-14019.phpRoot cause
In
AssignHandler.php, when processing array destructuring (list()/[]), the code correctly calledprocessExprNode()on key expressions to collect side effects (yield, throw points, impure points), but explicitly discarded the resulting scope with the comment// no need for $keyResult->getScope(). This meant any assignments within key expressions (like$a = 'foo') were not reflected in the scope, causing the assigned variable to appear undefined.The fix replaces the discarded scope with
$scope = $keyResult->getScope()so that assignments in key expressions are properly tracked.Test
Added
tests/PHPStan/Analyser/nsrt/bug-14019.phpwhich verifies that$agets type'foo'and$bgets type1after[($a = 'foo') => $b] = ['foo' => 1].Fixes phpstan/phpstan#14019