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

[8.x] Deprecate reduceMany in favor of reduceSpread #39201

Merged
merged 2 commits into from
Oct 14, 2021
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
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
}
}
}