From f25e5f16bcf8698f65ec421c60141c967404efe0 Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Fri, 15 Jan 2021 09:33:53 +0330 Subject: [PATCH 1/6] remove reduce from LazyCollection --- src/Illuminate/Collections/LazyCollection.php | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 2384948f951a..650213133ced 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -827,24 +827,6 @@ public function random($number = null) return is_null($number) ? $result : new static($result); } - /** - * Reduce the collection to a single value. - * - * @param callable $callback - * @param mixed $initial - * @return mixed - */ - public function reduce(callable $callback, $initial = null) - { - $result = $initial; - - foreach ($this as $value) { - $result = $callback($result, $value); - } - - return $result; - } - /** * Replace the collection items with the given items. * From ce581d38399515efbe4befa5ed9475c1fbfc37e2 Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Fri, 15 Jan 2021 09:34:22 +0330 Subject: [PATCH 2/6] remove reduce from Collection --- src/Illuminate/Collections/Collection.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index f4c7b4007a91..e4549d03ed11 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -872,18 +872,6 @@ public function random($number = null) return new static(Arr::random($this->items, $number)); } - /** - * Reduce the collection to a single value. - * - * @param callable $callback - * @param mixed $initial - * @return mixed - */ - public function reduce(callable $callback, $initial = null) - { - return array_reduce($this->items, $callback, $initial); - } - /** * Replace the collection items with the given items. * From 8ff015df389b356826500f61a435f4e8d03a5d19 Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Fri, 15 Jan 2021 09:36:58 +0330 Subject: [PATCH 3/6] add reduce to EnumeratesValues --- .../Collections/Traits/EnumeratesValues.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Illuminate/Collections/Traits/EnumeratesValues.php b/src/Illuminate/Collections/Traits/EnumeratesValues.php index 3355a9b2da29..45124450e822 100644 --- a/src/Illuminate/Collections/Traits/EnumeratesValues.php +++ b/src/Illuminate/Collections/Traits/EnumeratesValues.php @@ -703,6 +703,24 @@ public function pipeInto($class) return new $class($this); } + /** + * Reduce the collection to a single value. + * + * @param callable $callback + * @param mixed $initial + * @return mixed + */ + public function reduce(callable $callback, $initial = null) + { + $result = $initial; + + foreach ($this as $key => $value) { + $result = $callback($result, $value, $key); + } + + return $result; + } + /** * Pass the collection to the given callback and then return it. * From 9413573ed82d722ed42e7d374dd40a87f1faa4fd Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Fri, 15 Jan 2021 09:38:18 +0330 Subject: [PATCH 4/6] move tap method a bit down to its alphabetically correct place --- .../Collections/Traits/EnumeratesValues.php | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Illuminate/Collections/Traits/EnumeratesValues.php b/src/Illuminate/Collections/Traits/EnumeratesValues.php index 45124450e822..61257390e004 100644 --- a/src/Illuminate/Collections/Traits/EnumeratesValues.php +++ b/src/Illuminate/Collections/Traits/EnumeratesValues.php @@ -721,19 +721,6 @@ public function reduce(callable $callback, $initial = null) return $result; } - /** - * Pass the collection to the given callback and then return it. - * - * @param callable $callback - * @return $this - */ - public function tap(callable $callback) - { - $callback(clone $this); - - return $this; - } - /** * Create a collection of all elements that do not pass a given truth test. * @@ -751,6 +738,19 @@ public function reject($callback = true) }); } + /** + * Pass the collection to the given callback and then return it. + * + * @param callable $callback + * @return $this + */ + public function tap(callable $callback) + { + $callback(clone $this); + + return $this; + } + /** * Return only unique items from the collection array. * From cc0a1974815301217d27159e8107d3eb81ad780a Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Fri, 15 Jan 2021 09:43:38 +0330 Subject: [PATCH 5/6] add test for reduce with keys --- tests/Support/SupportCollectionTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 6faf65a1a3c2..602805781bda 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -3590,6 +3590,14 @@ public function testReduce($collection) $this->assertEquals(6, $data->reduce(function ($carry, $element) { return $carry += $element; })); + + $data = new $collection([ + 'foo' => 'bar', 'foo' => 'bar', + 'baz' => 'qux', 'baz' => 'qux', + ]); + $this->assertEquals('foobarbazqux', $data->reduce(function ($carry, $element, $key) { + return $carry .= $key.$element; + })); } /** From ed1e29825e0ff0c54d585788529703ba98f49cc1 Mon Sep 17 00:00:00 2001 From: Mo Khosh Date: Fri, 15 Jan 2021 10:03:56 +0330 Subject: [PATCH 6/6] Remove testReduceWithKeys we don't have the method anymore so we're not testing it --- tests/Support/SupportCollectionTest.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 6c2e9e2a73c9..602805781bda 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -3600,20 +3600,6 @@ public function testReduce($collection) })); } - /** - * @dataProvider collectionClassProvider - */ - public function testReduceWithKeys($collection) - { - $data = new $collection([ - 'foo' => 'bar', - 'baz' => 'qux', - ]); - $this->assertEquals('foobarbazqux', $data->reduceWithKeys(function ($carry, $element, $key) { - return $carry .= $key.$element; - })); - } - /** * @dataProvider collectionClassProvider */