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 $flag): bool
{
$f = false;
return $flag xor $f;
}
Why is it reported? #
The right side of the xor operator always evaluates to false. This makes the xor expression equivalent to just the left side, because $left xor false always equals $left. The right operand is redundant and likely indicates a logic error.
In the example above, $f is always false, so $flag xor $f is equivalent to $flag.
How to fix it #
Simplify the expression by removing the redundant xor:
<?php declare(strict_types = 1);
function doFoo(bool $flag): bool
{
- $f = false;
- return $flag xor $f;
+ return $flag;
}
Or fix the right-side condition so that it can actually evaluate to true:
<?php declare(strict_types = 1);
-function doFoo(bool $flag): bool
+function doFoo(bool $flag, bool $other): bool
{
- $f = false;
- return $flag xor $f;
+ return $flag xor $other;
}
How to ignore this error #
You can use the identifier logicalXor.rightAlwaysFalse to ignore this error using a comment:
// @phpstan-ignore logicalXor.rightAlwaysFalse
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: logicalXor.rightAlwaysFalse
Rules that report this error #
- PHPStan\Rules\Comparison\LogicalXorConstantConditionRule [1]