Skip to content

Commit

Permalink
feat: improve groupBy, keyBy generics (#52787)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw authored Sep 16, 2024
1 parent 1c52ec4 commit 4b47b4e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 32 deletions.
2 changes: 2 additions & 0 deletions phpstan.types.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ parameters:
level: max
paths:
- types
ignoreErrors:
- identifier: argument.templateType
12 changes: 8 additions & 4 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,11 @@ public function getOrPut($key, $value)
/**
* Group an associative array by a field or using a callback.
*
* @param (callable(TValue, TKey): array-key)|array|string $groupBy
* @template TGroupKey of array-key
*
* @param (callable(TValue, TKey): TGroupKey)|array|string $groupBy
* @param bool $preserveKeys
* @return static<array-key, static<array-key, TValue>>
* @return static<TGroupKey, static<($preserveKeys is true ? TKey : int), TValue>>
*/
public function groupBy($groupBy, $preserveKeys = false)
{
Expand Down Expand Up @@ -537,8 +539,10 @@ public function groupBy($groupBy, $preserveKeys = false)
/**
* Key an associative array by a field or using a callback.
*
* @param (callable(TValue, TKey): array-key)|array|string $keyBy
* @return static<array-key, TValue>
* @template TNewKey of array-key
*
* @param (callable(TValue, TKey): TNewKey)|array|string $keyBy
* @return static<TNewKey, TValue>
*/
public function keyBy($keyBy)
{
Expand Down
12 changes: 8 additions & 4 deletions src/Illuminate/Collections/Enumerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,17 +518,21 @@ public function get($key, $default = null);
/**
* Group an associative array by a field or using a callback.
*
* @param (callable(TValue, TKey): array-key)|array|string $groupBy
* @template TGroupKey of array-key
*
* @param (callable(TValue, TKey): TGroupKey)|array|string $groupBy
* @param bool $preserveKeys
* @return static<array-key, static<array-key, TValue>>
* @return static<TGroupKey, static<($preserveKeys is true ? TKey : int), TValue>>
*/
public function groupBy($groupBy, $preserveKeys = false);

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

Expand Down
12 changes: 8 additions & 4 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,11 @@ public function get($key, $default = null)
/**
* Group an associative array by a field or using a callback.
*
* @param (callable(TValue, TKey): array-key)|array|string $groupBy
* @template TGroupKey of array-key
*
* @param (callable(TValue, TKey): TGroupKey)|array|string $groupBy
* @param bool $preserveKeys
* @return static<array-key, static<array-key, TValue>>
* @return static<TGroupKey, static<($preserveKeys is true ? TKey : int), TValue>>
*/
public function groupBy($groupBy, $preserveKeys = false)
{
Expand All @@ -556,8 +558,10 @@ public function groupBy($groupBy, $preserveKeys = false)
/**
* Key an associative array by a field or using a callback.
*
* @param (callable(TValue, TKey): array-key)|array|string $keyBy
* @return static<array-key, TValue>
* @template TNewKey of array-key
*
* @param (callable(TValue, TKey): TNewKey)|array|string $keyBy
* @return static<TNewKey, TValue>
*/
public function keyBy($keyBy)
{
Expand Down
21 changes: 11 additions & 10 deletions types/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,22 +514,23 @@ function ($collection, $count) {

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

assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<(int|string), User>>', $collection->groupBy('name'));
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<(int|string), User>>', $collection->groupBy('name', true));
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<(int|string), User>>', $collection->groupBy(function ($user, $int) {
// assertType('User', $user);
// assertType('int', $int);
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<int, User>>', $collection->groupBy('name'));
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<int, User>>', $collection->groupBy('name', true));
assertType('Illuminate\Support\Collection<string, Illuminate\Support\Collection<int, User>>', $collection->groupBy(function ($user, $int) {
assertType('User', $user);
assertType('int', $int);

return 'foo';
}));
assertType('Illuminate\Support\Collection<(int|string), Illuminate\Support\Collection<(int|string), User>>', $collection->groupBy(function ($user) {

assertType('Illuminate\Support\Collection<string, Illuminate\Support\Collection<string, User>>', $collection->keyBy(fn () => '')->groupBy(function ($user) {
return 'foo';
}));
}, preserveKeys: true));

assertType('Illuminate\Support\Collection<(int|string), User>', $collection->keyBy('name'));
assertType('Illuminate\Support\Collection<(int|string), User>', $collection->keyBy(function ($user, $int) {
// assertType('User', $user);
// assertType('int', $int);
assertType('Illuminate\Support\Collection<string, User>', $collection->keyBy(function ($user, $int) {
assertType('User', $user);
assertType('int', $int);

return 'foo';
}));
Expand Down
20 changes: 10 additions & 10 deletions types/Support/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,22 +393,22 @@

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

assertType('Illuminate\Support\LazyCollection<(int|string), Illuminate\Support\LazyCollection<(int|string), User>>', $collection->groupBy('name'));
assertType('Illuminate\Support\LazyCollection<(int|string), Illuminate\Support\LazyCollection<(int|string), User>>', $collection->groupBy('name', true));
assertType('Illuminate\Support\LazyCollection<(int|string), Illuminate\Support\LazyCollection<(int|string), User>>', $collection->groupBy(function ($user, $int) {
// assertType('User', $user);
// assertType('int', $int);
assertType('Illuminate\Support\LazyCollection<(int|string), Illuminate\Support\LazyCollection<int, User>>', $collection->groupBy('name'));
assertType('Illuminate\Support\LazyCollection<(int|string), Illuminate\Support\LazyCollection<int, User>>', $collection->groupBy('name', true));
assertType('Illuminate\Support\LazyCollection<string, Illuminate\Support\LazyCollection<int, User>>', $collection->groupBy(function ($user, $int) {
assertType('User', $user);
assertType('int', $int);

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

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

return 'foo';
}));
Expand Down

0 comments on commit 4b47b4e

Please sign in to comment.