Respect original variable type when using extract on optional keys#4450
Merged
VincentLanglet merged 5 commits intophpstan:2.1.xfrom Feb 11, 2026
Merged
Respect original variable type when using extract on optional keys#4450VincentLanglet merged 5 commits intophpstan:2.1.xfrom
VincentLanglet merged 5 commits intophpstan:2.1.xfrom
Conversation
64793f6 to
9f4771a
Compare
9f4771a to
a3eb7f9
Compare
ondrejmirtes
requested changes
Feb 10, 2026
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** @return array{x: string, y?: string} */ |
Member
There was a problem hiding this comment.
Please add z?: string here and test it without assigning first.
I'd say that if $scope->canAnyVariableExist() is true and variable certainty is maybe, $z has to be mixed after extract.
The script can be included from somewhere where $z is already defined and is of a different type.
staabm
reviewed
Feb 11, 2026
Comment on lines
+13
to
+18
| assertType('null', $x); | ||
| assertType('null', $y); | ||
| extract(foo()); | ||
| assertType('string', $x); | ||
| assertType('string|null', $y); // <-- should be: null|string | ||
| assertType('mixed', $z); |
Contributor
There was a problem hiding this comment.
I think it would also make sense to assertVariableCertainty on the 3 vars
ondrejmirtes
approved these changes
Feb 11, 2026
fac1c51 to
1e30645
Compare
1e30645 to
90d84cb
Compare
staabm
reviewed
Feb 11, 2026
staabm
approved these changes
Feb 11, 2026
Co-authored-by: Markus Staab <[email protected]>
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.
Closes phpstan/phpstan#12364