Skip to content

Commit 68401d9

Browse files
committed
fix: groupBy/keyBy collection generics
1 parent 48647fb commit 68401d9

4 files changed

+40
-4
lines changed

e2e/filamentphp-filament.baseline.neon

+10
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ parameters:
110110
count: 1
111111
path: ../../e2e/packages/panels/src/Commands/MakeResourceCommand.php
112112

113+
-
114+
message: "#^Unable to resolve the template type TGroupKey in call to method Illuminate\\\\Support\\\\Collection\\<\\(int\\|string\\),Filament\\\\Navigation\\\\NavigationItem\\>\\:\\:groupBy\\(\\)$#"
115+
count: 1
116+
path: ../../e2e/packages/panels/src/Navigation/NavigationManager.php
117+
118+
-
119+
message: "#^Unable to resolve the template type TGroupKey in call to method Illuminate\\\\Support\\\\Collection\\<int,Filament\\\\Navigation\\\\NavigationItem\\>\\:\\:groupBy\\(\\)$#"
120+
count: 1
121+
path: ../../e2e/packages/panels/src/Navigation/NavigationManager.php
122+
113123
-
114124
message: "#^Unable to resolve the template type TMapValue in call to method Illuminate\\\\Support\\\\Collection\\<\\(int\\|string\\),string\\>\\:\\:map\\(\\)$#"
115125
count: 1

e2e/monicahq-monica.baseline.neon

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ parameters:
55
count: 1
66
path: ../../e2e/app/Domains/Contact/DavClient/Jobs/UpdateAddressBooks.php
77

8+
-
9+
message: "#^Parameter \\#1 \\$entry of method App\\\\Domains\\\\Contact\\\\DavClient\\\\Services\\\\Utils\\\\Dav\\\\ServiceUrlQuery\\:\\:getUri\\(\\) expects array, Illuminate\\\\Support\\\\Enumerable\\<int, mixed\\> given\\.$#"
10+
count: 1
11+
path: ../../e2e/app/Domains/Contact/DavClient/Services/Utils/Dav/ServiceUrlQuery.php
12+
813
-
914
message: "#^Parameter \\#1 \\$value of method Illuminate\\\\Support\\\\Collection\\<int,array\\<string, int\\|string\\>\\>\\:\\:prepend\\(\\) expects array\\{id\\: int, name\\: string\\}, array\\{id\\: null, name\\: ''\\} given\\.$#"
1015
count: 1

stubs/common/Enumerable.stub

+21
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,25 @@ interface Enumerable extends \Countable, \IteratorAggregate, \JsonSerializable
3838
* @return static<array-key, TCombineValue>
3939
*/
4040
public function combine($values);
41+
42+
/**
43+
* Key an associative array by a field or using a callback.
44+
*
45+
* @template TNewKey of array-key
46+
*
47+
* @param (callable(TValue, TKey=): TNewKey)|array<int, mixed>|string $keyBy
48+
* @return static<($keyBy is string ? array-key : ($keyBy is array<int, mixed> ? array-key : TNewKey)), TValue>
49+
*/
50+
public function keyBy($keyBy);
51+
52+
/**
53+
* Group an associative array by a field or using a callback.
54+
*
55+
* @template TGroupKey of array-key
56+
*
57+
* @param (callable(TValue, TKey=): TGroupKey)|array<int, mixed>|string $groupBy
58+
* @param bool $preserveKeys
59+
* @return static<($groupBy is string ? array-key : ($groupBy is array<int, mixed> ? array-key : TGroupKey)), static<($preserveKeys is true ? TKey : int), TValue>>
60+
*/
61+
public function groupBy($groupBy, $preserveKeys = false);
4162
}

tests/Type/data/collection-generic-static-methods.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ function test(
6161
assertType('Illuminate\Support\Collection<App\User, int>', $collection->flip());
6262
assertType('Illuminate\Support\Collection<int, string>', $items->flip());
6363

64-
assertType('Illuminate\Database\Eloquent\Collection<(int|string), Illuminate\Database\Eloquent\Collection<(int|string), App\User>>', $collection->groupBy('id'));
65-
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<(int|string), int>>', $items->groupBy('id'));
64+
assertType('Illuminate\Database\Eloquent\Collection<(int|string), Illuminate\Database\Eloquent\Collection<int, App\User>>', $collection->groupBy('id'));
65+
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<int, int>>', $items->groupBy('id'));
6666

67-
assertType('Illuminate\Database\Eloquent\Collection<(int|string), App\User>', $collection->keyBy(fn (User $user, int $key): string => $user->email));
67+
assertType('Illuminate\Database\Eloquent\Collection<string, App\User>', $collection->keyBy(fn (User $user, int $key): string => $user->email));
6868

6969
assertType('Illuminate\Support\Collection<int, int>', $collection->keys());
7070
assertType('Illuminate\Support\Collection<int, string>', $items->keys());
@@ -227,7 +227,7 @@ function test(
227227
LazyCollection::times(10, fn ($int) => 5)->flatMap(fn (int $i) => [$i * 2]),
228228
);
229229

230-
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<(int|string), array{id: int, type: string}>>', collect([
230+
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<int, array{id: int, type: string}>>', collect([
231231
[
232232
'id' => 1,
233233
'type' => 'A',

0 commit comments

Comments
 (0)