Skip to content

Commit 0f8e518

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 af7b1ea commit 0f8e518

File tree

3 files changed

+23
-71
lines changed

3 files changed

+23
-71
lines changed

Collection.php

-30
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
*

LazyCollection.php

-36
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
*

Traits/EnumeratesValues.php

+23-5
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
*

0 commit comments

Comments
 (0)