Error Identifier: parameterByRef.tooWideBool
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 setFlag(bool &$flag): void
{
$flag = true;
}
Why is it reported? #
The function declares a by-reference parameter with type bool, but only ever assigns true to it. The false value is never assigned, making the bool type wider than necessary.
How to fix it #
If the parameter is always set to a specific boolean value, narrow the by-ref type using a @param-out PHPDoc tag:
<?php declare(strict_types = 1);
+/** @param-out true $flag */
function setFlag(bool &$flag): void
{
$flag = true;
}
Alternatively, if both true and false should be possible, ensure both values are actually assigned on different code paths:
<?php declare(strict_types = 1);
function setFlag(bool &$flag, bool $condition): void
{
if ($condition) {
$flag = true;
} else {
$flag = false;
}
}
How to ignore this error #
You can use the identifier parameterByRef.tooWideBool to ignore this error using a comment:
// @phpstan-ignore parameterByRef.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: parameterByRef.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]