Skip to content

Commit 10ab29e

Browse files
authored
[9.x] Remove reduceWithKeys and merge the functionality with reduce (#35901)
* remove reduce from LazyCollection * remove reduce from Collection * add reduce to EnumeratesValues * move tap method a bit down to its alphabetically correct place * add test for reduce with keys * Remove testReduceWithKeys we don't have the method anymore so we're not testing it
1 parent 107e49f commit 10ab29e

File tree

4 files changed

+26
-80
lines changed

4 files changed

+26
-80
lines changed

src/Illuminate/Collections/Collection.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -872,36 +872,6 @@ public function random($number = null)
872872
return new static(Arr::random($this->items, $number));
873873
}
874874

875-
/**
876-
* Reduce the collection to a single value.
877-
*
878-
* @param callable $callback
879-
* @param mixed $initial
880-
* @return mixed
881-
*/
882-
public function reduce(callable $callback, $initial = null)
883-
{
884-
return array_reduce($this->items, $callback, $initial);
885-
}
886-
887-
/**
888-
* Reduce an associative collection to a single value.
889-
*
890-
* @param callable $callback
891-
* @param mixed $initial
892-
* @return mixed
893-
*/
894-
public function reduceWithKeys(callable $callback, $initial = null)
895-
{
896-
$result = $initial;
897-
898-
foreach ($this->items as $key => $value) {
899-
$result = $callback($result, $value, $key);
900-
}
901-
902-
return $result;
903-
}
904-
905875
/**
906876
* Replace the collection items with the given items.
907877
*

src/Illuminate/Collections/LazyCollection.php

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -827,42 +827,6 @@ public function random($number = null)
827827
return is_null($number) ? $result : new static($result);
828828
}
829829

830-
/**
831-
* Reduce the collection to a single value.
832-
*
833-
* @param callable $callback
834-
* @param mixed $initial
835-
* @return mixed
836-
*/
837-
public function reduce(callable $callback, $initial = null)
838-
{
839-
$result = $initial;
840-
841-
foreach ($this as $value) {
842-
$result = $callback($result, $value);
843-
}
844-
845-
return $result;
846-
}
847-
848-
/**
849-
* Reduce an associative collection to a single value.
850-
*
851-
* @param callable $callback
852-
* @param mixed $initial
853-
* @return mixed
854-
*/
855-
public function reduceWithKeys(callable $callback, $initial = null)
856-
{
857-
$result = $initial;
858-
859-
foreach ($this as $key => $value) {
860-
$result = $callback($result, $value, $key);
861-
}
862-
863-
return $result;
864-
}
865-
866830
/**
867831
* Replace the collection items with the given items.
868832
*

src/Illuminate/Collections/Traits/EnumeratesValues.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -704,16 +704,21 @@ public function pipeInto($class)
704704
}
705705

706706
/**
707-
* Pass the collection to the given callback and then return it.
707+
* Reduce the collection to a single value.
708708
*
709709
* @param callable $callback
710-
* @return $this
710+
* @param mixed $initial
711+
* @return mixed
711712
*/
712-
public function tap(callable $callback)
713+
public function reduce(callable $callback, $initial = null)
713714
{
714-
$callback(clone $this);
715+
$result = $initial;
715716

716-
return $this;
717+
foreach ($this as $key => $value) {
718+
$result = $callback($result, $value, $key);
719+
}
720+
721+
return $result;
717722
}
718723

719724
/**
@@ -733,6 +738,19 @@ public function reject($callback = true)
733738
});
734739
}
735740

741+
/**
742+
* Pass the collection to the given callback and then return it.
743+
*
744+
* @param callable $callback
745+
* @return $this
746+
*/
747+
public function tap(callable $callback)
748+
{
749+
$callback(clone $this);
750+
751+
return $this;
752+
}
753+
736754
/**
737755
* Return only unique items from the collection array.
738756
*

tests/Support/SupportCollectionTest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,18 +3590,12 @@ public function testReduce($collection)
35903590
$this->assertEquals(6, $data->reduce(function ($carry, $element) {
35913591
return $carry += $element;
35923592
}));
3593-
}
35943593

3595-
/**
3596-
* @dataProvider collectionClassProvider
3597-
*/
3598-
public function testReduceWithKeys($collection)
3599-
{
36003594
$data = new $collection([
3601-
'foo' => 'bar',
3602-
'baz' => 'qux',
3595+
'foo' => 'bar', 'foo' => 'bar',
3596+
'baz' => 'qux', 'baz' => 'qux',
36033597
]);
3604-
$this->assertEquals('foobarbazqux', $data->reduceWithKeys(function ($carry, $element, $key) {
3598+
$this->assertEquals('foobarbazqux', $data->reduce(function ($carry, $element, $key) {
36053599
return $carry .= $key.$element;
36063600
}));
36073601
}

0 commit comments

Comments
 (0)