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);
function doFoo(int|string &$value): void
{
$value = 42;
}
Why is it reported? #
A by-reference parameter has a union type, but the function never assigns one of the union members to it. In the example above, $value is typed as int|string, but the function only ever assigns an int. The string part of the union is never used and can be removed.
How to fix it #
Narrow the type to only include what is actually assigned:
<?php declare(strict_types = 1);
-function doFoo(int|string &$value): void
+function doFoo(int &$value): void
{
$value = 42;
}
You can also narrow the parameter out type with a @param-out PHPDoc tag if you cannot change the native type.
Public and protected methods that can be overridden depend on the projectType configuration parameter. With the default open, classes outside the analysed paths might override them and assign the other type to the parameter, so they are not reported unless checkTooWideParameterOutInProtectedAndPublicMethods is enabled. With closed, PHPStan checks them against all the methods that override them, and reports a type only when neither the method nor anything overriding it assigns that type to the parameter.
How to ignore this error #
You can use the identifier parameterByRef.unusedType to ignore this error using a comment:
// @phpstan-ignore parameterByRef.unusedType
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.unusedType
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]
- PHPStan\Rules\TooWideTypehints\TooWideTypeInOverridableMemberRule [1]