Error Identifier: greaterOrEqual.invalid
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(\stdClass $obj, int $n): void
{
$result = $obj >= $n;
}
Why is it reported? #
The >= (greater than or equal) comparison operator is being used between two types that cannot be meaningfully compared, resulting in a PHP error. Not all types support comparison operations – for example, comparing an object to an integer is not a valid operation.
In the example above, a stdClass object is compared with an integer using >=, which PHP cannot perform.
How to fix it #
Compare values of compatible types, or extract a comparable value from the object first:
<?php declare(strict_types = 1);
function doFoo(\stdClass $obj, int $n): void
{
- $result = $obj >= $n;
+ $result = $obj->value >= $n;
}
How to ignore this error #
You can use the identifier greaterOrEqual.invalid to ignore this error using a comment:
// @phpstan-ignore greaterOrEqual.invalid
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: greaterOrEqual.invalid
Rules that report this error #
- PHPStan\Rules\Operators\InvalidComparisonOperationRule [1]