Error Identifier: possiblyImpure.yieldFrom
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
/**
* @phpstan-pure
* @return \Generator<int, string, void, void>
*/
function generateAll(): \Generator
{
yield from ['a', 'b']; // ERROR: Possibly impure yield from in pure function generateAll().
}
Why is it reported? #
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 from inside a pure function is considered a possibly impure operation because it delegates to another generator or iterable while maintaining stateful generator behavior. Generators interact with the caller through send() and throw() methods, and yield from forwards these interactions to the inner generator, making the operation inherently impure.
The “possibly impure” variant is reported when PHPStan cannot be fully certain the operation is impure but considers it suspicious enough to report.
How to fix it #
Remove the @phpstan-pure annotation if the function needs to use yield from:
<?php declare(strict_types = 1);
-/**
- * @phpstan-pure
- * @return \Generator<int, string, void, void>
- */
+/**
+ * @return \Generator<int, string, void, void>
+ */
function generateAll(): \Generator
{
yield from ['a', '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 generateAll(): \Generator
+function generateAll(): array
{
- yield from ['a', 'b'];
+ return ['a', 'b'];
}
How to ignore this error #
You can use the identifier possiblyImpure.yieldFrom to ignore this error using a comment:
// @phpstan-ignore possiblyImpure.yieldFrom
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: possiblyImpure.yieldFrom