website/errors/impure.yield.md
<?php declare(strict_types = 1);
/**
* @phpstan-pure
* @return \Generator<int, string, void, void>
*/
function generateItems(): \Generator
{
yield 'a'; // ERROR: Impure yield in pure function generateItems().
yield 'b';
}
A function or method marked as @phpstan-pure must not have side effects and must always return the same result for the same inputs. Using yield inside a pure function is considered an impure operation because generators maintain internal state across multiple calls. Each time the generator is resumed, it can produce different values and interact with the caller through the send() method, making the behavior inherently stateful and impure.
Remove the @phpstan-pure annotation if the function needs to use yield:
<?php declare(strict_types = 1);
-/**
- * @phpstan-pure
- * @return \Generator<int, string, void, void>
- */
+/**
+ * @return \Generator<int, string, void, void>
+ */
function generateItems(): \Generator
{
yield 'a';
yield 'b';
}
Alternatively, if the function should remain pure, return an array instead of using a generator:
<?php declare(strict_types = 1);
/**
* @phpstan-pure
- * @return \Generator<int, string, void, void>
+ * @return list<string>
*/
-function generateItems(): \Generator
+function generateItems(): array
{
- yield 'a';
- yield 'b';
+ return ['a', 'b'];
}