Error Identifier: booleanAnd.resultUnused
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(bool $a, bool $b): void
{
$a && $b;
}
Why is it reported? #
The result of the && (boolean AND) operator is computed but never used. The expression is evaluated as a standalone statement, meaning its result is discarded. This is usually a mistake – either the result should be assigned to a variable, returned, or used in a condition.
How to fix it #
Assign the result to a variable if it is needed:
<?php declare(strict_types = 1);
function doFoo(bool $a, bool $b): void
{
- $a && $b;
+ $result = $a && $b;
}
Or use the expression as a condition:
<?php declare(strict_types = 1);
function doFoo(bool $a, bool $b): void
{
- $a && $b;
+ if ($a && $b) {
+ // do something
+ }
}
If the intent was to call a function when $a is truthy, consider using an explicit if statement instead:
<?php declare(strict_types = 1);
-function doFoo(bool $a, bool $b): void
+function doFoo(bool $a): void
{
- $a && $b;
+ if ($a) {
+ doSomething();
+ }
}
How to ignore this error #
You can use the identifier booleanAnd.resultUnused to ignore this error using a comment:
// @phpstan-ignore booleanAnd.resultUnused
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.resultUnused
Rules that report this error #
- PHPStan\Rules\DeadCode\NoopRule [1]