Error Identifier: logicalXor.leftAlwaysFalse
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 $b): void
{
if (0 xor $b) {
// ...
}
}
Why is it reported? #
The left side of the xor expression always evaluates to false. This means the result of the xor is equivalent to just the right side’s boolean value, making the xor operator redundant.
The logical xor operator returns true when exactly one of its operands is true. If the left side is always false, the result is always equal to the right side.
How to fix it #
Remove the xor and use the right operand directly:
-if (0 xor $b) {
+if ($b) {
// ...
}
If the constant value is unintentional, fix the left operand so it can actually be true or false:
-if (0 xor $b) {
+if ($a xor $b) {
// ...
}
How to ignore this error #
You can use the identifier logicalXor.leftAlwaysFalse to ignore this error using a comment:
// @phpstan-ignore logicalXor.leftAlwaysFalse
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.leftAlwaysFalse
Rules that report this error #
- PHPStan\Rules\Comparison\LogicalXorConstantConditionRule [1]