Skip to content

Commit

Permalink
fix: groupBy/keyBy collection generics
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Sep 25, 2024
1 parent 48647fb commit 68401d9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
10 changes: 10 additions & 0 deletions e2e/filamentphp-filament.baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ parameters:
count: 1
path: ../../e2e/packages/panels/src/Commands/MakeResourceCommand.php

-
message: "#^Unable to resolve the template type TGroupKey in call to method Illuminate\\\\Support\\\\Collection\\<\\(int\\|string\\),Filament\\\\Navigation\\\\NavigationItem\\>\\:\\:groupBy\\(\\)$#"
count: 1
path: ../../e2e/packages/panels/src/Navigation/NavigationManager.php

-
message: "#^Unable to resolve the template type TGroupKey in call to method Illuminate\\\\Support\\\\Collection\\<int,Filament\\\\Navigation\\\\NavigationItem\\>\\:\\:groupBy\\(\\)$#"
count: 1
path: ../../e2e/packages/panels/src/Navigation/NavigationManager.php

-
message: "#^Unable to resolve the template type TMapValue in call to method Illuminate\\\\Support\\\\Collection\\<\\(int\\|string\\),string\\>\\:\\:map\\(\\)$#"
count: 1
Expand Down
5 changes: 5 additions & 0 deletions e2e/monicahq-monica.baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ parameters:
count: 1
path: ../../e2e/app/Domains/Contact/DavClient/Jobs/UpdateAddressBooks.php

-
message: "#^Parameter \\#1 \\$entry of method App\\\\Domains\\\\Contact\\\\DavClient\\\\Services\\\\Utils\\\\Dav\\\\ServiceUrlQuery\\:\\:getUri\\(\\) expects array, Illuminate\\\\Support\\\\Enumerable\\<int, mixed\\> given\\.$#"
count: 1
path: ../../e2e/app/Domains/Contact/DavClient/Services/Utils/Dav/ServiceUrlQuery.php

-
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\\.$#"
count: 1
Expand Down
21 changes: 21 additions & 0 deletions stubs/common/Enumerable.stub
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,25 @@ interface Enumerable extends \Countable, \IteratorAggregate, \JsonSerializable
* @return static<array-key, TCombineValue>
*/
public function combine($values);

/**
* Key an associative array by a field or using a callback.
*
* @template TNewKey of array-key
*
* @param (callable(TValue, TKey=): TNewKey)|array<int, mixed>|string $keyBy
* @return static<($keyBy is string ? array-key : ($keyBy is array<int, mixed> ? array-key : TNewKey)), TValue>
*/
public function keyBy($keyBy);

/**
* Group an associative array by a field or using a callback.
*
* @template TGroupKey of array-key
*
* @param (callable(TValue, TKey=): TGroupKey)|array<int, mixed>|string $groupBy
* @param bool $preserveKeys
* @return static<($groupBy is string ? array-key : ($groupBy is array<int, mixed> ? array-key : TGroupKey)), static<($preserveKeys is true ? TKey : int), TValue>>
*/
public function groupBy($groupBy, $preserveKeys = false);
}
8 changes: 4 additions & 4 deletions tests/Type/data/collection-generic-static-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ function test(
assertType('Illuminate\Support\Collection<App\User, int>', $collection->flip());
assertType('Illuminate\Support\Collection<int, string>', $items->flip());

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

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

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

assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<(int|string), array{id: int, type: string}>>', collect([
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<int, array{id: int, type: string}>>', collect([
[
'id' => 1,
'type' => 'A',
Expand Down

0 comments on commit 68401d9

Please sign in to comment.