Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] feat: improve Collection groupBy, keyBy generics #52787

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: improve groupBy, keyBy generics
  • Loading branch information
calebdw committed Sep 13, 2024
commit 42e29d0bda13eebc4aeb7a60936eff2943713889
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