Error Identifier: generator.valueType
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, mixed, void>
*/
function doFoo(): \Generator
{
yield 'foo' => 'bar';
}
Why is it reported? #
A yield or yield from expression provides a value whose type does not match the generator’s declared value type. The function declares it returns Generator<string, int, mixed, void>, meaning the values yielded must be of type int. However, the string 'bar' is yielded as the value, which is incompatible with int.
How to fix it #
Yield a value that matches the declared generator value type:
/**
* @return \Generator<string, int, mixed, void>
*/
function doFoo(): \Generator
{
- yield 'foo' => 'bar';
+ yield 'foo' => 42;
}
Or update the generator’s PHPDoc type to match the actual yielded values:
/**
- * @return \Generator<string, int, mixed, void>
+ * @return \Generator<string, string, mixed, void>
*/
function doFoo(): \Generator
{
yield 'foo' => 'bar';
}
How to ignore this error #
You can use the identifier generator.valueType to ignore this error using a comment:
// @phpstan-ignore generator.valueType
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.valueType