From 68401d92fc7eca59e27ee94fe118863f40bebffb Mon Sep 17 00:00:00 2001 From: Caleb White Date: Tue, 24 Sep 2024 21:16:56 -0500 Subject: [PATCH] fix: groupBy/keyBy collection generics --- e2e/filamentphp-filament.baseline.neon | 10 +++++++++ e2e/monicahq-monica.baseline.neon | 5 +++++ stubs/common/Enumerable.stub | 21 +++++++++++++++++++ .../collection-generic-static-methods.php | 8 +++---- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/e2e/filamentphp-filament.baseline.neon b/e2e/filamentphp-filament.baseline.neon index 4d24e5406..c2c7d1ef4 100644 --- a/e2e/filamentphp-filament.baseline.neon +++ b/e2e/filamentphp-filament.baseline.neon @@ -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\\\\:\\: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 diff --git a/e2e/monicahq-monica.baseline.neon b/e2e/monicahq-monica.baseline.neon index a76b87550..c0891b87d 100644 --- a/e2e/monicahq-monica.baseline.neon +++ b/e2e/monicahq-monica.baseline.neon @@ -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\\ given\\.$#" + count: 1 + path: ../../e2e/app/Domains/Contact/DavClient/Services/Utils/Dav/ServiceUrlQuery.php + - message: "#^Parameter \\#1 \\$value of method Illuminate\\\\Support\\\\Collection\\\\>\\:\\:prepend\\(\\) expects array\\{id\\: int, name\\: string\\}, array\\{id\\: null, name\\: ''\\} given\\.$#" count: 1 diff --git a/stubs/common/Enumerable.stub b/stubs/common/Enumerable.stub index 987905733..4fa001791 100644 --- a/stubs/common/Enumerable.stub +++ b/stubs/common/Enumerable.stub @@ -38,4 +38,25 @@ interface Enumerable extends \Countable, \IteratorAggregate, \JsonSerializable * @return static */ 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|string $keyBy + * @return static<($keyBy is string ? array-key : ($keyBy is array ? 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|string $groupBy + * @param bool $preserveKeys + * @return static<($groupBy is string ? array-key : ($groupBy is array ? array-key : TGroupKey)), static<($preserveKeys is true ? TKey : int), TValue>> + */ + public function groupBy($groupBy, $preserveKeys = false); } diff --git a/tests/Type/data/collection-generic-static-methods.php b/tests/Type/data/collection-generic-static-methods.php index ad5e6607a..c25f485f6 100644 --- a/tests/Type/data/collection-generic-static-methods.php +++ b/tests/Type/data/collection-generic-static-methods.php @@ -61,10 +61,10 @@ function test( assertType('Illuminate\Support\Collection', $collection->flip()); assertType('Illuminate\Support\Collection', $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>', $collection->groupBy('id')); + assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection>', $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', $collection->keyBy(fn (User $user, int $key): string => $user->email)); assertType('Illuminate\Support\Collection', $collection->keys()); assertType('Illuminate\Support\Collection', $items->keys()); @@ -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>', collect([ [ 'id' => 1, 'type' => 'A',