Menu

← Back to impure.*

Error Identifier: impure.yield

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 generateItems(): \Generator
{
	yield 'a'; // ERROR: Impure yield in pure function generateItems().
	yield 'b';
}

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 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.

How to fix it #

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'];
 }

How to ignore this error #

You can use the identifier impure.yield to ignore this error using a comment:

// @phpstan-ignore impure.yield
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: impure.yield

Rules that report this error #

  • PHPStan\Rules\Pure\PureFunctionRule [1]
  • PHPStan\Rules\Pure\PureMethodRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.