Skip to content

MIR0229

DivisionByZero

A division operation uses a divisor that is statically known to be zero, which will cause a runtime error.

<?php
function calculate(int $value): float {
return $value / 0; // Division by zero
}

Ensure the divisor is not zero, or add a check before division:

<?php
function calculate(int $value, int $divisor): float {
if ($divisor === 0) {
throw new InvalidArgumentException('Divisor cannot be zero');
}
return $value / $divisor;
}