Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

groupBy('column')->map->count() makes int keys #2091

Open
rudiedirkx opened this issue Oct 25, 2024 · 3 comments
Open

groupBy('column')->map->count() makes int keys #2091

rudiedirkx opened this issue Oct 25, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@rudiedirkx
Copy link

  • Larastan Version: v2.9.9
  • Laravel Version: v11.23.5
  • Phpstan Version: 1.12.6

Description

groupBy('status') returns int|string for TKey. I think that's acceptable, because Larastan doesn't know what status is. It probably could, but that's not the point. After that, I want to count how many records per group: ->map->count() so TValue should be int, and Larastan does that. But it also makes TKey int! Why would it go from int|string to int? The keys haven't changed from the higher-order-map.

Laravel code where the issue was found

$users = User::all();
\PHPStan\dumpType($users); // 🟩 App\Base\ModelCollection<int, App\Models\User>

$groups = $users->groupBy('status');
\PHPStan\dumpType($groups); // 🟩 App\Base\ModelCollection<(int|string), App\Base\ModelCollection<int, App\Models\User>>

$counts = $groups->map->count();
\PHPStan\dumpType($counts); // 🟥 Illuminate\Support\Collection<int, int>

The inner collection of $groups is int key 👍 because of the groupBy(). Maybe that's why the outer collection becomes int after map->count()? I'm just guessing.

@rudiedirkx rudiedirkx added the bug Something isn't working label Oct 25, 2024
@rudiedirkx

This comment was marked as off-topic.

@calebdw
Copy link
Contributor

calebdw commented Oct 25, 2024

groupBy('status') returns int|string for TKey. I think that's acceptable, because Larastan doesn't know what status is.

it would if you provided a closure:

$groups = $users->groupBy(fn ($i) => $i->status);
\PHPStan\dumpType($groups); // 🟩 App\Base\ModelCollection<int, App\Base\ModelCollection<int, App\Models\User>>

@rudiedirkx
Copy link
Author

Okay, cool, but that wasn't the point. I'm okay with the array-key guess. Not with the int later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants