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 positive-int $i */
function doFoo(int $i): void
{
if ($i > 0 && is_int($i)) {
echo 'always';
}
}
Why is it reported? #
The result of the && (boolean AND) expression always evaluates to true. This happens when both sides of the operator are always truthy, making the condition constant. In this example, $i is a positive-int, so $i > 0 is always true and is_int($i) is also always true. This usually indicates a logic error, a redundant check, or dead code.
How to fix it #
Remove the redundant condition if the check is unnecessary:
<?php declare(strict_types = 1);
/** @param positive-int $i */
function doFoo(int $i): void
{
- if ($i > 0 && is_int($i)) {
- echo 'always';
- }
+ echo 'always';
}
Or fix the logic to use conditions that are meaningful:
<?php declare(strict_types = 1);
/** @param positive-int $i */
function doFoo(int $i): void
{
- if ($i > 0 && is_int($i)) {
+ if ($i > 0 && $i < 100) {
echo 'always';
}
}
How to ignore this error #
You can use the identifier booleanAnd.alwaysTrue to ignore this error using a comment:
// @phpstan-ignore booleanAnd.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: booleanAnd.alwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\BooleanAndConstantConditionRule [1]