Error Identifier: return.nestedUnusedType
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 array<int|string>
*/
function doFoo(): array
{
return [1, 2, 3];
}
Why is it reported? #
The declared return type contains a nested type that is wider than necessary based on the actual return values. In the example above, the function is declared to return array<int|string>, but it only ever returns arrays containing int values. The string part of the nested union type is unused and can be narrowed.
How to fix it #
Narrow the return type to match the actual return values:
<?php declare(strict_types = 1);
/**
- * @return array<int|string>
+ * @return array<int>
*/
function doFoo(): array
{
return [1, 2, 3];
}
How to ignore this error #
You can use the identifier return.nestedUnusedType to ignore this error using a comment:
// @phpstan-ignore return.nestedUnusedType
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: return.nestedUnusedType
Rules that report this error #
- PHPStan\Rules\TooWideTypehints\TooWideArrowFunctionReturnTypehintRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideClosureReturnTypehintRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideFunctionParameterOutTypeRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideFunctionReturnTypehintRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideMethodParameterOutTypeRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideMethodReturnTypehintRule [1]
- PHPStan\Rules\TooWideTypehints\TooWidePropertyTypeRule [1]