Skip to content

Commit

Permalink
Deprecate reduceMany in favor of reduceSpread (#39201)
Browse files Browse the repository at this point in the history
* Deprecate `reduceMany`

* Add lazy tests for `reduce` and `reduceSpread`
  • Loading branch information
JosephSilber authored Oct 14, 2021
1 parent 02c5dc5 commit a738e0e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
30 changes: 16 additions & 14 deletions src/Illuminate/Collections/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,25 @@ public function reduce(callable $callback, $initial = null)
* @param mixed ...$initial
* @return array
*
* @deprecated Use "reduceSpread" instead
*
* @throws \UnexpectedValueException
*/
public function reduceMany(callable $callback, ...$initial)
{
return $this->reduceSpread($callback, ...$initial);
}

/**
* Reduce the collection to multiple aggregate values.
*
* @param callable $callback
* @param mixed ...$initial
* @return array
*
* @throws \UnexpectedValueException
*/
public function reduceSpread(callable $callback, ...$initial)
{
$result = $initial;

Expand All @@ -772,20 +788,6 @@ class_basename(static::class), gettype($result)
return $result;
}

/**
* Reduce the collection to multiple aggregate values.
*
* @param callable $callback
* @param mixed ...$initial
* @return array
*
* @throws \UnexpectedValueException
*/
public function reduceSpread(callable $callback, ...$initial)
{
return $this->reduceMany($callback, ...$initial);
}

/**
* Reduce an associative collection to a single value.
*
Expand Down
8 changes: 4 additions & 4 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3920,11 +3920,11 @@ public function testReduceWithKeys($collection)
/**
* @dataProvider collectionClassProvider
*/
public function testReduceMany($collection)
public function testReduceSpread($collection)
{
$data = new $collection([-1, 0, 1, 2, 3, 4, 5]);

[$sum, $max, $min] = $data->reduceMany(function ($sum, $max, $min, $value) {
[$sum, $max, $min] = $data->reduceSpread(function ($sum, $max, $min, $value) {
$sum += $value;
$max = max($max, $value);
$min = min($min, $value);
Expand All @@ -3940,13 +3940,13 @@ public function testReduceMany($collection)
/**
* @dataProvider collectionClassProvider
*/
public function testReduceManyThrowsAnExceptionIfReducerDoesNotReturnAnArray($collection)
public function testReduceSpreadThrowsAnExceptionIfReducerDoesNotReturnAnArray($collection)
{
$data = new $collection([1]);

$this->expectException(UnexpectedValueException::class);

$data->reduceMany(function () {
$data->reduceSpread(function () {
return false;
}, null);
}
Expand Down
37 changes: 36 additions & 1 deletion tests/Support/SupportLazyCollectionIsLazyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Support;

use Exception;
use Illuminate\Support\ItemNotFoundException;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\MultipleItemsFoundException;
Expand Down Expand Up @@ -817,15 +818,40 @@ public function testRangeIsLazy()
});
}

public function testReduceEnumeratesOnce()
public function testReduceIsLazy()
{
$this->assertEnumerates(1, function ($collection) {
$this->rescue(function () use ($collection) {
$collection->reduce(function ($total, $value) {
throw new Exception('Short-circuit');
}, 0);
});
});

$this->assertEnumeratesOnce(function ($collection) {
$collection->reduce(function ($total, $value) {
return $total + $value;
}, 0);
});
}

public function testReduceSpreadIsLazy()
{
$this->assertEnumerates(1, function ($collection) {
$this->rescue(function () use ($collection) {
$collection->reduceSpread(function ($one, $two, $value) {
throw new Exception('Short-circuit');
}, 0, 0);
});
});

$this->assertEnumeratesOnce(function ($collection) {
$collection->reduceSpread(function ($total, $max, $value) {
return [$total + $value, max($max, $value)];
}, 0, 0);
});
}

public function testRejectIsLazy()
{
$this->assertDoesNotEnumerate(function ($collection) {
Expand Down Expand Up @@ -1573,4 +1599,13 @@ protected function make($source)
{
return new LazyCollection($source);
}

protected function rescue($callback)
{
try {
$callback();
} catch (Exception $e) {
// Silence is golden
}
}
}

0 comments on commit a738e0e

Please sign in to comment.