Skip to content

[13.x] Add initial value type to return of reduce and reduceWithKeys#60178

Merged
taylorotwell merged 1 commit into
laravel:13.xfrom
jnoordsij:fix-reduce-type
May 19, 2026
Merged

[13.x] Add initial value type to return of reduce and reduceWithKeys#60178
taylorotwell merged 1 commit into
laravel:13.xfrom
jnoordsij:fix-reduce-type

Conversation

@jnoordsij
Copy link
Copy Markdown
Contributor

On empty collections, the reduce and reduceWithKeys methods will simply return the initial value:

> collect([])->reduce(static fn(): string => 'foo', 'bar');

= "bar"

Hence this should be part of the return typehint.

In real-world scenarios, this may appear when one does something like $collection->filter(<function where nothing passes>)->reduce(...) or $model->emptyRelation->reduce(...).

Note that the Enumerable interface already (correctly) prescribes this:

/**
* Reduce the collection to a single value.
*
* @template TReduceInitial
* @template TReduceReturnType
*
* @param callable(TReduceInitial|TReduceReturnType, TValue, TKey): TReduceReturnType $callback
* @param TReduceInitial $initial
* @return TReduceInitial|TReduceReturnType
*/
public function reduce(callable $callback, $initial = null);

@jnoordsij jnoordsij changed the title [13.x] Add initial value type to return of reduce and reduceWithKeys [13.x] Add initial value type to return of reduce and reduceWithKeys May 19, 2026
@taylorotwell taylorotwell merged commit f05ef24 into laravel:13.x May 19, 2026
48 of 52 checks passed
@jnoordsij jnoordsij deleted the fix-reduce-type branch May 19, 2026 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants