Error Identifier: booleanNot.alwaysTrue
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 $i): void
{
$zero = 0;
if (!$zero) {
// always entered
}
}
Why is it reported? #
The negated boolean expression (!$zero) always evaluates to true because the operand is always falsy. In this example, the variable $zero is always 0, which is falsy in PHP, so !$zero is always true. This means the condition will always be entered, which usually indicates a logic error or a redundant check.
How to fix it #
Remove the redundant condition if it is always evaluating to the same value:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
$zero = 0;
// Execute the code unconditionally instead of wrapping in if (!$zero)
}
Or fix the logic to check the correct variable:
<?php declare(strict_types = 1);
function doFoo(int $i): void
{
- $zero = 0;
- if (!$zero) {
+ if (!$i) {
// now depends on the actual input
}
}
How to ignore this error #
You can use the identifier booleanNot.alwaysTrue to ignore this error using a comment:
// @phpstan-ignore booleanNot.alwaysTrue
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: booleanNot.alwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanNotConstantConditionRule [1]