Skip to content

Commit ae347cb

Browse files
authored
[12.x] Types: EnumeratesValues Sum (#55044)
* add failing type test * add failing type test for lazy collection * add conditional return type * make tests more specific
1 parent 84c6055 commit ae347cb

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

src/Illuminate/Collections/Traits/EnumeratesValues.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,10 @@ public function percentage(callable $callback, int $precision = 2)
535535
/**
536536
* Get the sum of the given values.
537537
*
538-
* @param (callable(TValue): mixed)|string|null $callback
539-
* @return mixed
538+
* @template TReturnType
539+
*
540+
* @param (callable(TValue): TReturnType)|string|null $callback
541+
* @return ($callback is callable ? TReturnType : mixed)
540542
*/
541543
public function sum($callback = null)
542544
{

types/Support/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -873,10 +873,10 @@ function ($collection, $count) {
873873
assertType('Illuminate\Support\Collection<string, string>', $collection->make(['string' => 'string'])->sortKeysDesc(1));
874874

875875
assertType('mixed', $collection->make([1])->sum('string'));
876-
assertType('mixed', $collection->make(['string'])->sum(function ($string) {
876+
assertType('int<1, 2>', $collection->make(['string'])->sum(function ($string) {
877877
assertType('string', $string);
878878

879-
return 1;
879+
return rand(1, 2);
880880
}));
881881

882882
assertType('Illuminate\Support\Collection<int, int>', $collection->make([1])->take(1));

types/Support/LazyCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,10 +732,10 @@ public function toArray(): array
732732
assertType('Illuminate\Support\LazyCollection<string, string>', $collection->make(['string' => 'string'])->sortKeysDesc(1));
733733

734734
assertType('mixed', $collection->make([1])->sum('string'));
735-
assertType('mixed', $collection->make(['string'])->sum(function ($string) {
735+
assertType('int<1, 2>', $collection->make(['string'])->sum(function ($string) {
736736
assertType('string', $string);
737737

738-
return 1;
738+
return rand(1, 2);
739739
}));
740740

741741
assertType('Illuminate\Support\LazyCollection<int, int>', $collection->make([1])->take(1));

0 commit comments

Comments
 (0)