Skip to content

Commit 4b47b4e

Browse files
authored
feat: improve groupBy, keyBy generics (#52787)
1 parent 1c52ec4 commit 4b47b4e

File tree

6 files changed

+47
-32
lines changed

6 files changed

+47
-32
lines changed

phpstan.types.neon.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ parameters:
22
level: max
33
paths:
44
- types
5+
ignoreErrors:
6+
- identifier: argument.templateType

src/Illuminate/Collections/Collection.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,11 @@ public function getOrPut($key, $value)
486486
/**
487487
* Group an associative array by a field or using a callback.
488488
*
489-
* @param (callable(TValue, TKey): array-key)|array|string $groupBy
489+
* @template TGroupKey of array-key
490+
*
491+
* @param (callable(TValue, TKey): TGroupKey)|array|string $groupBy
490492
* @param bool $preserveKeys
491-
* @return static<array-key, static<array-key, TValue>>
493+
* @return static<TGroupKey, static<($preserveKeys is true ? TKey : int), TValue>>
492494
*/
493495
public function groupBy($groupBy, $preserveKeys = false)
494496
{
@@ -537,8 +539,10 @@ public function groupBy($groupBy, $preserveKeys = false)
537539
/**
538540
* Key an associative array by a field or using a callback.
539541
*
540-
* @param (callable(TValue, TKey): array-key)|array|string $keyBy
541-
* @return static<array-key, TValue>
542+
* @template TNewKey of array-key
543+
*
544+
* @param (callable(TValue, TKey): TNewKey)|array|string $keyBy
545+
* @return static<TNewKey, TValue>
542546
*/
543547
public function keyBy($keyBy)
544548
{

src/Illuminate/Collections/Enumerable.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,17 +518,21 @@ public function get($key, $default = null);
518518
/**
519519
* Group an associative array by a field or using a callback.
520520
*
521-
* @param (callable(TValue, TKey): array-key)|array|string $groupBy
521+
* @template TGroupKey of array-key
522+
*
523+
* @param (callable(TValue, TKey): TGroupKey)|array|string $groupBy
522524
* @param bool $preserveKeys
523-
* @return static<array-key, static<array-key, TValue>>
525+
* @return static<TGroupKey, static<($preserveKeys is true ? TKey : int), TValue>>
524526
*/
525527
public function groupBy($groupBy, $preserveKeys = false);
526528

527529
/**
528530
* Key an associative array by a field or using a callback.
529531
*
530-
* @param (callable(TValue, TKey): array-key)|array|string $keyBy
531-
* @return static<array-key, TValue>
532+
* @template TNewKey of array-key
533+
*
534+
* @param (callable(TValue, TKey): TNewKey)|array|string $keyBy
535+
* @return static<TNewKey, TValue>
532536
*/
533537
public function keyBy($keyBy);
534538

src/Illuminate/Collections/LazyCollection.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,11 @@ public function get($key, $default = null)
544544
/**
545545
* Group an associative array by a field or using a callback.
546546
*
547-
* @param (callable(TValue, TKey): array-key)|array|string $groupBy
547+
* @template TGroupKey of array-key
548+
*
549+
* @param (callable(TValue, TKey): TGroupKey)|array|string $groupBy
548550
* @param bool $preserveKeys
549-
* @return static<array-key, static<array-key, TValue>>
551+
* @return static<TGroupKey, static<($preserveKeys is true ? TKey : int), TValue>>
550552
*/
551553
public function groupBy($groupBy, $preserveKeys = false)
552554
{
@@ -556,8 +558,10 @@ public function groupBy($groupBy, $preserveKeys = false)
556558
/**
557559
* Key an associative array by a field or using a callback.
558560
*
559-
* @param (callable(TValue, TKey): array-key)|array|string $keyBy
560-
* @return static<array-key, TValue>
561+
* @template TNewKey of array-key
562+
*
563+
* @param (callable(TValue, TKey): TNewKey)|array|string $keyBy
564+
* @return static<TNewKey, TValue>
561565
*/
562566
public function keyBy($keyBy)
563567
{

types/Support/Collection.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -514,22 +514,23 @@ function ($collection, $count) {
514514

515515
assertType('Illuminate\Support\Collection<string, int>', $collection::make(['string'])->flip());
516516

517-
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<(int|string), User>>', $collection->groupBy('name'));
518-
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<(int|string), User>>', $collection->groupBy('name', true));
519-
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<(int|string), User>>', $collection->groupBy(function ($user, $int) {
520-
// assertType('User', $user);
521-
// assertType('int', $int);
517+
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<int, User>>', $collection->groupBy('name'));
518+
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<int, User>>', $collection->groupBy('name', true));
519+
assertType('Illuminate\Support\Collection<string, Illuminate\Support\Collection<int, User>>', $collection->groupBy(function ($user, $int) {
520+
assertType('User', $user);
521+
assertType('int', $int);
522522

523523
return 'foo';
524524
}));
525-
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<(int|string), User>>', $collection->groupBy(function ($user) {
525+
526+
assertType('Illuminate\Support\Collection<string, Illuminate\Support\Collection<string, User>>', $collection->keyBy(fn () => '')->groupBy(function ($user) {
526527
return 'foo';
527-
}));
528+
}, preserveKeys: true));
528529

529530
assertType('Illuminate\Support\Collection<(int|string), User>', $collection->keyBy('name'));
530-
assertType('Illuminate\Support\Collection<(int|string), User>', $collection->keyBy(function ($user, $int) {
531-
// assertType('User', $user);
532-
// assertType('int', $int);
531+
assertType('Illuminate\Support\Collection<string, User>', $collection->keyBy(function ($user, $int) {
532+
assertType('User', $user);
533+
assertType('int', $int);
533534

534535
return 'foo';
535536
}));

types/Support/LazyCollection.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -393,22 +393,22 @@
393393

394394
assertType('Illuminate\Support\LazyCollection<string, int>', $collection::make(['string'])->flip());
395395

396-
assertType('Illuminate\Support\LazyCollection<(int|string), Illuminate\Support\LazyCollection<(int|string), User>>', $collection->groupBy('name'));
397-
assertType('Illuminate\Support\LazyCollection<(int|string), Illuminate\Support\LazyCollection<(int|string), User>>', $collection->groupBy('name', true));
398-
assertType('Illuminate\Support\LazyCollection<(int|string), Illuminate\Support\LazyCollection<(int|string), User>>', $collection->groupBy(function ($user, $int) {
399-
// assertType('User', $user);
400-
// assertType('int', $int);
396+
assertType('Illuminate\Support\LazyCollection<(int|string), Illuminate\Support\LazyCollection<int, User>>', $collection->groupBy('name'));
397+
assertType('Illuminate\Support\LazyCollection<(int|string), Illuminate\Support\LazyCollection<int, User>>', $collection->groupBy('name', true));
398+
assertType('Illuminate\Support\LazyCollection<string, Illuminate\Support\LazyCollection<int, User>>', $collection->groupBy(function ($user, $int) {
399+
assertType('User', $user);
400+
assertType('int', $int);
401401

402402
return 'foo';
403403
}));
404-
assertType('Illuminate\Support\LazyCollection<(int|string), Illuminate\Support\LazyCollection<(int|string), User>>', $collection->groupBy(function ($user) {
404+
assertType('Illuminate\Support\LazyCollection<string, Illuminate\Support\LazyCollection<string, User>>', $collection->keyBy(fn () => '')->groupBy(function ($user) {
405405
return 'foo';
406-
}));
406+
}, true));
407407

408408
assertType('Illuminate\Support\LazyCollection<(int|string), User>', $collection->keyBy('name'));
409-
assertType('Illuminate\Support\LazyCollection<(int|string), User>', $collection->keyBy(function ($user, $int) {
410-
// assertType('User', $user);
411-
// assertType('int', $int);
409+
assertType('Illuminate\Support\LazyCollection<string, User>', $collection->keyBy(function ($user, $int) {
410+
assertType('User', $user);
411+
assertType('int', $int);
412412

413413
return 'foo';
414414
}));

0 commit comments

Comments
 (0)