Skip to content

Report wrong @return on template-bound groupBy() and keyBy() receivers - #2332

Merged
alies-dev merged 2 commits into
4.xfrom
2328-groupby-keyby-template-receiver
Nov 16, 2026
Merged

alies-dev merged 2 commits into
4.xfrom
2328-groupby-keyby-template-receiver

Conversation

@alies-dev

@alies-dev alies-dev commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Issue to Solve

Since #2291, CollectionGroupByKeyByHandler declines for a template-bound receiver (@template C of Collection<...>, @param C $rows). groupBy('col') and keyBy('col') then fall back to the stub's static return, which Psalm expands to the receiver template C itself, so the changed shape is lost and a wrong @return C is accepted:

@template C of Collection<int, CollectionGroupingModel>
return $rows->groupBy('foreign_id');   // before: no issue

After this PR Psalm reports InvalidReturnStatement (Collection<int, Collection<int, CollectionGroupingModel>> does not match C), and the same for keyBy('foreign_id') on C of Collection<string, CollectionGroupingModel> (the keys become int).

Related

Fixes #2328
Refs #2291

Solution Description

Checklist

  • Tests cover the change (type test in tests/Type/ and/or unit test in tests/Unit/)

@alies-dev
alies-dev merged commit cb4f5ab into 4.x Nov 16, 2026
22 checks passed
@alies-dev
alies-dev deleted the 2328-groupby-keyby-template-receiver branch November 16, 2026 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix for PRs only (used by release-drafter)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Template-bound groupBy() / keyBy() receivers accept a wrong @return C since #2291

1 participant