website/errors/div.leftNonNumeric.md
<?php declare(strict_types = 1);
function divide(bool $flag, int $denominator): float
{
return $flag / $denominator;
}
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 bool, array, object, or null on the left side of a division indicates a logic error.
In the example above, $flag is a bool, which is not a valid numeric operand for division.
Use a numeric value as the left operand:
<?php declare(strict_types = 1);
-function divide(bool $flag, int $denominator): float
+function divide(int $numerator, int $denominator): float
{
- return $flag / $denominator;
+ return $numerator / $denominator;
}
Or convert the value to a numeric type before dividing:
<?php declare(strict_types = 1);
function divide(bool $flag, int $denominator): float
{
- return $flag / $denominator;
+ return (int) $flag / $denominator;
}