Error Identifier: div.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 divide(string $label, int $denominator): void
{
$result = $label / $denominator;
}
Why is it reported? #
This error is reported by phpstan/phpstan-strict-rules.
The left operand of a division operator (/) is not a numeric type (int or float). Division is an arithmetic operation that only makes sense with numeric values. Using a non-numeric type like string, array, object, or null on the left side of a division indicates a logic error.
In the example above, $label is a string, which is not a valid numeric operand for division.
How to fix it #
Use a numeric value as the left operand:
<?php declare(strict_types = 1);
-function divide(string $label, int $denominator): void
+function divide(int $numerator, int $denominator): void
{
- $result = $label / $denominator;
+ $result = $numerator / $denominator;
}
Or convert the value to a numeric type before dividing:
<?php declare(strict_types = 1);
function divide(string $label, int $denominator): void
{
- $result = $label / $denominator;
+ $result = (int) $label / $denominator;
}
How to ignore this error #
You can use the identifier div.leftNonNumeric to ignore this error using a comment:
// @phpstan-ignore div.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: div.leftNonNumeric
Rules that report this error #
- PHPStan\Rules\Operators\OperandsInArithmeticDivisionRule [1] phpstan/phpstan-strict-rules