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-out bool $result
*/
function validate(mixed $input, bool &$result): void
{
$result = true;
}
Why is it reported? #
The @param-out type declares bool for a by-reference parameter, but PHPStan has determined that the parameter is only ever assigned true (or only ever assigned false). The declared output type is wider than necessary because one of the boolean values is never produced.
This indicates that the @param-out annotation is more permissive than what the function actually assigns.
How to fix it #
Narrow the @param-out type to match what the function actually produces:
<?php declare(strict_types = 1);
/**
- * @param-out bool $result
+ * @param-out true $result
*/
function validate(mixed $input, bool &$result): void
{
$result = true;
}
Or if the function should also produce false in some cases, add that code path:
<?php declare(strict_types = 1);
/**
* @param-out bool $result
*/
function validate(mixed $input, bool &$result): void
{
- $result = true;
+ $result = $input !== null;
}
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 value 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 bool only when neither the method nor anything overriding it assigns the other value to the parameter.
How to ignore this error #
You can use the identifier paramOut.tooWideBool to ignore this error using a comment:
// @phpstan-ignore paramOut.tooWideBool
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: paramOut.tooWideBool
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]