Error Identifier: smaller.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 $object, int $number): void
{
if ($object < $number) {
// ...
}
}
Why is it reported? #
The < (less than) comparison is used between types that cannot be meaningfully compared, resulting in a TypeError at runtime. Since PHP 8.0, comparing incompatible types with relational operators (<, >, <=, >=) throws a TypeError. In this example, comparing a \stdClass object with an int is not a valid operation.
How to fix it #
Compare values of compatible types. Extract a comparable value from the object first:
<?php declare(strict_types = 1);
function doFoo(\stdClass $object, int $number): void
{
- if ($object < $number) {
+ if ($object->value < $number) {
// ...
}
}
Or ensure both operands have comparable types:
<?php declare(strict_types = 1);
-function doFoo(\stdClass $object, int $number): void
+function doFoo(int $a, int $number): void
{
- if ($object < $number) {
+ if ($a < $number) {
// ...
}
}
How to ignore this error #
You can use the identifier smaller.invalid to ignore this error using a comment:
// @phpstan-ignore smaller.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: smaller.invalid
Rules that report this error #
- PHPStan\Rules\Operators\InvalidComparisonOperationRule [1]