Menu

← Back to generator.*

Error Identifier: generator.keyType

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);

/** @return \Generator<string, int> */
function generateItems(): \Generator
{
	yield 1 => 'foo';
}

Why is it reported? #

The key type yielded by the generator does not match the key type declared in the @return PHPDoc tag. The Generator generic type is Generator<TKey, TValue, TSend, TReturn>, where the first type parameter specifies the expected key type.

In the example above, the function declares it returns Generator<string, int> (string keys, int values), but yield 1 => 'foo' yields an integer key 1 instead of a string key.

How to fix it #

Either fix the yielded key to match the declared type:

 <?php declare(strict_types = 1);
 
 /** @return \Generator<string, int> */
 function generateItems(): \Generator
 {
-	yield 1 => 'foo';
+	yield 'item' => 42;
 }

Or update the PHPDoc to match the actual yielded types:

 <?php declare(strict_types = 1);
 
-/** @return \Generator<string, int> */
+/** @return \Generator<int, string> */
 function generateItems(): \Generator
 {
 	yield 1 => 'foo';
 }

How to ignore this error #

You can use the identifier generator.keyType to ignore this error using a comment:

// @phpstan-ignore generator.keyType
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: generator.keyType

Rules that report this error #

  • PHPStan\Rules\Generators\YieldFromTypeRule [1]
  • PHPStan\Rules\Generators\YieldTypeRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.