Skip to content

Commit 0fd262e

Browse files
authored
Merge pull request #40813 from fdalcin/generic-reduce-key
[9.x] Improves `Support\Collection` reduce method type definition
2 parents 5d02e1b + b24ce99 commit 0fd262e

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/Illuminate/Collections/Enumerable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ public function random($number = null);
805805
* @template TReduceInitial
806806
* @template TReduceReturnType
807807
*
808-
* @param callable(TReduceInitial|TReduceReturnType, TValue): TReduceReturnType $callback
808+
* @param callable(TReduceInitial|TReduceReturnType, TValue, TKey): TReduceReturnType $callback
809809
* @param TReduceInitial $initial
810810
* @return TReduceReturnType
811811
*/

src/Illuminate/Collections/Traits/EnumeratesValues.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ function ($carry, $pipe) {
744744
* @template TReduceInitial
745745
* @template TReduceReturnType
746746
*
747-
* @param callable(TReduceInitial|TReduceReturnType, TValue): TReduceReturnType $callback
747+
* @param callable(TReduceInitial|TReduceReturnType, TValue, TKey): TReduceReturnType $callback
748748
* @param TReduceInitial $initial
749749
* @return TReduceReturnType
750750
*/
@@ -808,7 +808,7 @@ class_basename(static::class), gettype($result)
808808
* @template TReduceWithKeysInitial
809809
* @template TReduceWithKeysReturnType
810810
*
811-
* @param callable(TReduceWithKeysInitial|TReduceWithKeysReturnType, TValue): TReduceWithKeysReturnType $callback
811+
* @param callable(TReduceWithKeysInitial|TReduceWithKeysReturnType, TValue, TKey): TReduceWithKeysReturnType $callback
812812
* @param TReduceWithKeysInitial $initial
813813
* @return TReduceWithKeysReturnType
814814
*/

types/Support/Collection.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,14 @@
544544
assertType('User', $user);
545545
assertType('int', $int);
546546

547+
return 1;
548+
}, 0));
549+
assertType('int', $collection
550+
->reduce(function ($int, $user, $key) {
551+
assertType('User', $user);
552+
assertType('int', $int);
553+
assertType('int', $key);
554+
547555
return 1;
548556
}, 0));
549557

@@ -559,6 +567,14 @@
559567
assertType('User', $user);
560568
assertType('int', $int);
561569

570+
return 1;
571+
}, 0));
572+
assertType('int', $collection
573+
->reduceWithKeys(function ($int, $user, $key) {
574+
assertType('User', $user);
575+
assertType('int', $int);
576+
assertType('int', $key);
577+
562578
return 1;
563579
}, 0));
564580

0 commit comments

Comments
 (0)