Conversation
resolveTemplateTypes() swaps template types by name, so when K resolves to itself, a narrowed K got replaced by the declared one. For example, with K of string|int|object the key of array<K, V> is K of int|string. The PHPDoc return type then didn't fit the native array type and we fell back to plain array. This only started affecting conditional return types in 2.2.16. Before that ConditionalType::traverse() re-normalized its branches, which narrowed the key again and hid the problem.
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#15321
resolveTemplateTypes()replaces template types by name. When the class's own template map resolvesKto itself, a narrowed occurrence like theK of int|stringkey ofarray<K, V>gets replaced by the declaredK of string|int|object.TypehintHelper::decideType()then resolves that toarray<int|object|string, mixed>, which doesn't fit the nativearray, so we end up with plainarray.Plain
@return array<K, V>has been broken like this for a while. The conditional return type only broke in 2.2.16: since 8411207,ConditionalType::traverse()keeps the traversed branches instead of normalizing them again, and that re-normalization used to narrow the key back.The fix keeps the occurrence when the standin is the same template type (same name and scope, not an argument).
nsrt/bug-15321.phpfails on all 5 assertions without the change.