From a738e0e72f169f54a54af59a5faa93d6ea923885 Mon Sep 17 00:00:00 2001 From: Joseph Silber Date: Thu, 14 Oct 2021 11:27:26 -0400 Subject: [PATCH] Deprecate `reduceMany` in favor of `reduceSpread` (#39201) * Deprecate `reduceMany` * Add lazy tests for `reduce` and `reduceSpread` --- .../Collections/Traits/EnumeratesValues.php | 30 ++++++++------- tests/Support/SupportCollectionTest.php | 8 ++-- .../SupportLazyCollectionIsLazyTest.php | 37 ++++++++++++++++++- 3 files changed, 56 insertions(+), 19 deletions(-) diff --git a/src/Illuminate/Collections/Traits/EnumeratesValues.php b/src/Illuminate/Collections/Traits/EnumeratesValues.php index 941ce3a4b89a..d2944ffeb4db 100644 --- a/src/Illuminate/Collections/Traits/EnumeratesValues.php +++ b/src/Illuminate/Collections/Traits/EnumeratesValues.php @@ -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; @@ -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. * diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index eb05841152d8..aefc69ab783e 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -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); @@ -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); } diff --git a/tests/Support/SupportLazyCollectionIsLazyTest.php b/tests/Support/SupportLazyCollectionIsLazyTest.php index 095ae3f5da41..14ff3f49278a 100644 --- a/tests/Support/SupportLazyCollectionIsLazyTest.php +++ b/tests/Support/SupportLazyCollectionIsLazyTest.php @@ -2,6 +2,7 @@ namespace Illuminate\Tests\Support; +use Exception; use Illuminate\Support\ItemNotFoundException; use Illuminate\Support\LazyCollection; use Illuminate\Support\MultipleItemsFoundException; @@ -817,8 +818,16 @@ 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; @@ -826,6 +835,23 @@ public function testReduceEnumeratesOnce() }); } + 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) { @@ -1573,4 +1599,13 @@ protected function make($source) { return new LazyCollection($source); } + + protected function rescue($callback) + { + try { + $callback(); + } catch (Exception $e) { + // Silence is golden + } + } }