{"record":{"id":"4bfdd2a046c2ea37","repo":"laravel/framework","slug":"s-reducespread-expects-reducer-to-return-an-arra","errorCode":null,"errorMessage":"%s::reduceSpread expects reducer to return an array, but got a '%s' instead.","messagePattern":"(.+?)::reduceSpread expects reducer to return an array, but got a '(.+?)' instead\\.","errorType":"exception","errorClass":"UnexpectedValueException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Collections/Traits/EnumeratesValues.php","lineNumber":889,"sourceCode":"\n    /**\n     * Reduce the collection to multiple aggregate values.\n     *\n     * @param  callable  $callback\n     * @param  mixed  ...$initial\n     * @return array\n     *\n     * @throws \\UnexpectedValueException\n     */\n    public function reduceSpread(callable $callback, ...$initial)\n    {\n        $result = $initial;\n\n        foreach ($this as $key => $value) {\n            $result = call_user_func_array($callback, array_merge($result, [$value, $key]));\n\n            if (! is_array($result)) {\n                throw new UnexpectedValueException(sprintf(\n                    \"%s::reduceSpread expects reducer to return an array, but got a '%s' instead.\",\n                    class_basename(static::class), gettype($result)\n                ));\n            }\n        }\n\n        return $result;\n    }\n\n    /**\n     * Reduce an associative collection to a single value.\n     *\n     * @template TReduceWithKeysInitial\n     * @template TReduceWithKeysReturnType\n     *\n     * @param  callable(TReduceWithKeysInitial|TReduceWithKeysReturnType, TValue, TKey): TReduceWithKeysReturnType  $callback\n     * @param  TReduceWithKeysInitial  $initial\n     * @return TReduceWithKeysInitial|TReduceWithKeysReturnType","sourceCodeStart":871,"sourceCodeEnd":907,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Collections/Traits/EnumeratesValues.php#L871-L907","documentation":"Thrown by reduceSpread(callable $callback, ...$initial) when the reducer callable returns a non-array value on any iteration. reduceSpread expects each step to return an array so it can spread the result as arguments into the next call (list-destructuring pattern); a non-array return breaks that contract and throws UnexpectedValueException with the class and actual type.","triggerScenarios":"Passing a reducer that returns a scalar/object/null on some iteration; forgetting to return the accumulator array; early-return inside the callback that yields null.","commonSituations":"Refactoring a reduce into reduceSpread for the spread-arg ergonomics; callback that conditionally returns and misses a branch; third-party reducer reused without adapting its return shape.","solutions":["Ensure the reducer always returns an array on every code path (including the final iteration).","Use plain reduce() if you only need a single accumulated value.","Add a return-type check / unit test asserting array output for each branch."],"exampleFix":"// before\n$result = $collection->reduceSpread(function ($carry, $item) {\n    if ($item === null) return null;\n    return [$carry[0] + $item];\n}, 0);\n// after\n$result = $collection->reduceSpread(function ($carry, $item) {\n    if ($item === null) return $carry;\n    return [$carry[0] + $item];\n}, 0);","handlingStrategy":"validation","validationCode":"// Verify the reducer returns array on a sample before relying on it\n$test = $callback(...array_merge($initial, [$collection->first(), $collection->keys()->first()]));\nif (! is_array($test)) {\n    throw new \\UnexpectedValueException('reducer must return array');\n}","typeGuard":"// Ensure every branch returns an array\n$callback = function (array $carry, $item): array {\n    // ...all paths return array\n    return $carry;\n};","tryCatchPattern":"try {\n    $result = $collection->reduceSpread($callback, ...$initial);\n} catch (\\UnexpectedValueException $e) {\n    // fall back to plain reduce\n    $result = $collection->reduce($callback, $initial);\n}","preventionTips":["Always type the reducer return as array in its signature.","Use reduce() instead of reduceSpread() if a single scalar suffices.","Unit-test each branch of the callback for array output."],"tags":["collections","callback-contract","laravel"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}