Repository navigation
Report wrong @return on template-bound groupBy() and keyBy() receivers - #2332
Merged
Merged
Conversation
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.
Issue to Solve
Since #2291,
CollectionGroupByKeyByHandlerdeclines for a template-bound receiver (@template C of Collection<...>,@param C $rows).groupBy('col')andkeyBy('col')then fall back to the stub'sstaticreturn, which Psalm expands to the receiver templateCitself, so the changed shape is lost and a wrong@return Cis accepted:After this PR Psalm reports
InvalidReturnStatement(Collection<int, Collection<int, CollectionGroupingModel>>does not matchC), and the same forkeyBy('foreign_id')onC of Collection<string, CollectionGroupingModel>(the keys becomeint).Related
Fixes #2328
Refs #2291
Solution Description
keyBy()whose rebuilt key and value equal the receiver's template arguments. That keeps the Keep template-bound receivers in groupBy()/keyBy() narrowing #2005 case (keyBy()returningC) working.groupBy()always answers the rebuilt nested type, because it can never be the receiver's own shape.Issue2005KeyByTemplateReceiverTest.phpt. They reported nothing on4.xand now report theInvalidReturnTypeandInvalidReturnStatementpairs above. The existing positive Keep template-bound receivers in groupBy()/keyBy() narrowing #2005 case andIssue2005KeyByForeignTemplateListKnownLimitation.phptare unchanged and pass. Type tests, Psalm self-analysis, Rector, cs and StructArmed are clean.@return static<...>on a template-typed receiver collapses to the template and drops the new type arguments vimeo/psalm#13997 (psalm.dev). Psalm itself collapses@return static<NewK, NewV>on a template receiver (C of Base<int, string>) toCand drops the new arguments, so this is a plugin workaround, marked@todo-by https://github.com/vimeo/psalm/issues/13997inCollectionGroupByKeyByHandler. Once Psalm keeps the arguments, the handler declines for every template-bound receiver and the stub'sstatic<...>answers. Related:@return static<...>arguments are applied by position to the called subclass instead of projected through@extendsvimeo/psalm#13988 (samestatic<...>family, positional arguments on a reshaped subclass).Checklist
tests/Type/and/or unit test intests/Unit/)