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);
class Foo
{
/**
* @param array<int|string> &$items
*/
public function process(array &$items): void
{
$items = [1, 2, 3];
}
}
Why is it reported? #
The declared type of a by-reference parameter is wider than necessary in a nested part. PHPStan analyzed all code paths and determined that a narrower type would be more precise, because some union members in the nested type are never actually assigned to the parameter.
In the example above, the parameter type declares array<int|string>, but the function only ever assigns int values. The string part of the nested union is unused and the type could be narrowed to array<int>.
How to fix it #
Narrow the parameter type to match what the function actually assigns:
class Foo
{
/**
- * @param array<int|string> &$items
+ * @param array<int> &$items
*/
public function process(array &$items): void
{
$items = [1, 2, 3];
}
}
How to ignore this error #
You can use the identifier parameterByRef.nestedUnusedType to ignore this error using a comment:
// @phpstan-ignore parameterByRef.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: parameterByRef.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]