Error Identifier: mod.leftNonNumeric
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 remainder(string $a, int $b): int
{
return $a % $b;
}
Why is it reported? #
The left operand of the modulo operator (%) is not a numeric type. PHP will attempt to coerce non-numeric values to numbers at runtime, which can lead to unexpected results or TypeError exceptions in strict mode. Only int and float types should be used in arithmetic operations.
This rule is provided by the phpstan-strict-rules package.
How to fix it #
Ensure the left operand is a numeric type before performing the modulo operation:
-function remainder(string $a, int $b): int
+function remainder(int $a, int $b): int
{
return $a % $b;
}
If the value is a numeric string, cast it explicitly:
function remainder(string $a, int $b): int
{
- return $a % $b;
+ return (int) $a % $b;
}
How to ignore this error #
You can use the identifier mod.leftNonNumeric to ignore this error using a comment:
// @phpstan-ignore mod.leftNonNumeric
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: mod.leftNonNumeric
Rules that report this error #
- PHPStan\Rules\Operators\OperandsInArithmeticModuloRule [1] phpstan/phpstan-strict-rules