Menu

← Back to logicalXor.*

Error Identifier: logicalXor.leftAlwaysTrue

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 (1 xor $b) {
		// ...
	}
}

Why is it reported? #

The left side of the xor expression always evaluates to true. This means the result of the xor is equivalent to the negation of the right side, making the xor operator misleading.

The logical xor operator returns true when exactly one of its operands is true. If the left side is always true, the result is always the opposite of the right side.

How to fix it #

Replace the xor with a negation of the right operand:

-if (1 xor $b) {
+if (!$b) {
 	// ...
 }

If the constant value is unintentional, fix the left operand so it can actually be true or false:

-if (1 xor $b) {
+if ($a xor $b) {
 	// ...
 }

How to ignore this error #

You can use the identifier logicalXor.leftAlwaysTrue to ignore this error using a comment:

// @phpstan-ignore logicalXor.leftAlwaysTrue
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.leftAlwaysTrue

Rules that report this error #

  • PHPStan\Rules\Comparison\LogicalXorConstantConditionRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.