{"record":{"id":"9c130638c61bb93d","repo":"laravel/framework","slug":"generators-should-not-be-passed-directly-to-lazyco","errorCode":null,"errorMessage":"Generators should not be passed directly to LazyCollection. Instead, pass a generator function.","messagePattern":"Generators should not be passed directly to LazyCollection\\. Instead, pass a generator function\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Collections/LazyCollection.php","lineNumber":54,"sourceCode":"     * @var (Closure(): \\Generator<TKey, TValue, mixed, void>)|static|array<TKey, TValue>\n     */\n    public $source;\n\n    /**\n     * Create a new lazy collection instance.\n     *\n     * @param  \\Illuminate\\Contracts\\Support\\Arrayable<TKey, TValue>|iterable<TKey, TValue>|(Closure(): \\Generator<TKey, TValue, mixed, void>)|self<TKey, TValue>|array<TKey, TValue>|null  $source\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function __construct($source = null)\n    {\n        if ($source instanceof Closure || $source instanceof self) {\n            $this->source = $source;\n        } elseif (is_null($source)) {\n            $this->source = static::empty();\n        } elseif ($source instanceof Generator) {\n            throw new InvalidArgumentException(\n                'Generators should not be passed directly to LazyCollection. Instead, pass a generator function.'\n            );\n        } else {\n            $this->source = $this->getArrayableItems($source);\n        }\n    }\n\n    /**\n     * Create a new instance of the collection.\n     *\n     * @param  \\Illuminate\\Contracts\\Support\\Arrayable<TKey, TValue>|iterable<TKey, TValue>|(Closure(): \\Generator<TKey, TValue, mixed, void>)|self<TKey, TValue>|array<TKey, TValue>|null  $items\n     * @return static\n     */\n    protected function newInstance($items = [])\n    {\n        return new static($items);\n    }\n","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Collections/LazyCollection.php#L36-L72","documentation":"Thrown by the LazyCollection constructor when a Generator instance is passed directly as the source. A Generator can only be iterated once and is consumed during operations, which breaks LazyCollection's laziness and re-iteration guarantees. The library requires a Closure that *produces* a Generator (a factory) so it can be re-created on each traversal.","triggerScenarios":"new LazyCollection($generator) where $generator is the return value of a generator function (e.g. new LazyCollection(myGeneratorFunc())). Also when wrapping a yield-based iterator from another library directly.","commonSituations":"Reading a file lazily via a generator and forgetting to wrap it in a closure; refactoring an eager collection into a lazy one and passing the old iterator; third-party stream/guzzle PSR-7 body generators.","solutions":["Pass a closure that returns the generator: new LazyCollection(fn () => myGeneratorFunc()).","If the source is already iterable, pass it directly (array, Arrayable) instead of a generator.","For a generator held in a variable, wrap its producer: new LazyCollection(function () use (...) { yield ...; })."],"exampleFix":"// before\n$lazy = new LazyCollection(readLines($file));\n// after\n$lazy = new LazyCollection(fn () => readLines($file));","handlingStrategy":"type-guard","validationCode":"if ($source instanceof \\Generator) {\n    throw new \\InvalidArgumentException('Wrap the generator in a closure.');\n}","typeGuard":"function lazySource($source) {\n    if ($source instanceof \\Generator) {\n        return fn () => $source; // not ideal; better to capture the factory\n    }\n    return $source;\n}\n// Preferred: always pass the generator-producing closure, not the generator.","tryCatchPattern":"try {\n    $lazy = new \\Illuminate\\Support\\LazyCollection($source);\n} catch (\\InvalidArgumentException $e) {\n    $lazy = new \\Illuminate\\Support\\LazyCollection(fn () => yield from $source);\n}","preventionTips":["Always pass fn () => generatorFunc() rather than generatorFunc().","Static-analyse with PHPStan/Larastan to catch Generator-vs-Closure mismatches early.","Document the source parameter contract clearly in your own wrappers."],"tags":["lazy-collection","generators","laravel","argument-error"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}