Skip to content

Collection partion method not returning correct generic types #53283

@browner12

Description

@browner12

Laravel Version

11.29.0

PHP Version

8.3.12

Database Driver & Version

Sqlite 3.43.2

Description

The partition() method of the Illuminate\Database\Eloquent\Collection, as defined in the Illuminate\Support\Traits\EnumeratesValues class possibly does not have the correct generic return types.

https://github.com/laravel/framework/blob/11.x/src/Illuminate/Collections/Traits/EnumeratesValues.php#L501

The issue goes away when I disable and remove the cache of the LaravelIdea plugin. However, I'm not sure if it's an issue with Laravel's definition of the generics, or with how the plugin is parsing the generics. If it's deemed that Laravel is correct, I'll go open this issue in the plugin repo.

Steps To Reproduce

Given the following user table:

id sex birthday
1 male 2002-01-12
2 female 1991-05-05
3 male 2010-12-25

and the following code:

//get users
$users = User::all();

//partition by sex
[$men, $women] = $users->partition(fn ($user) => $user->sex === 'male');

//group men by age
$menGroupedByAge = $men->groupBy(fn ($item) => $item->birthday->age);

dd($users, $men, $women, $menGroupedByAge);

all 4 dumped variables will be of type Illuminate\Database\Eloquent\Collection. However, the IDE is currently thinking $men, $women, and $menGroupedByAge are all of type App\Models\User.

Both our Collection and User classes also have a groupBy() method available to them, but with different signatures. Because the IDE believes $men is a User, it's also warning that it is unacceptable to pass a closure to groupBy().

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions