Error Identifier: parameterByRef.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);
/**
* @param array<mixed> $a
* @param-out array<array{int, bool}> $a
*/
function doFoo(array &$a): void
{
$a = [
[1, false],
[2, false],
];
}
Why is it reported? #
The declared @param-out type for a by-reference parameter is wider than necessary. The nested type contains a union member or component that is never actually assigned to the parameter. PHPStan analyzed all code paths and determined that a narrower type would be more precise.
In the example above, if the bool in array{int, bool} is always false, the type could be narrowed to array{int, false}.
How to fix it #
Narrow the @param-out type to match what the function actually assigns:
/**
* @param array<mixed> $a
- * @param-out array<array{int, bool}> $a
+ * @param-out array<array{int, false}> $a
*/
function doFoo(array &$a): void
{
$a = [
[1, false],
[2, false],
];
}
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]