Skip to content

Commit

Permalink
Merge pull request #2535 from mjansenDatabay/bugfix/27785
Browse files Browse the repository at this point in the history
QuestionPool/Math: Restored PHP <= 7.0.x behaviour if one operand is …
  • Loading branch information
mbecker-databay committed Mar 4, 2020
1 parent 0ceddc6 commit ad930ee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
15 changes: 13 additions & 2 deletions Services/Math/classes/class.ilMathPhpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,20 @@ public function div($left_operand, $right_operand, $scale = null)
throw new ilMathDivisionByZeroException(sprintf("Division of %s by %s not possible!", $left_operand, $right_operand));
}

$res = $this->normalize($left_operand) / $this->normalize($right_operand);
// This ensures the old PHP <= 7.0.x behaviour, see: #27785 / #26361
try {
$res = $this->normalize($left_operand) / $this->normalize($right_operand);

$division = $this->applyScale($res, $this->normalize($scale));
} catch (Throwable $e) {
if (strpos($e->getMessage(), 'A non-numeric value encountered') !== false) {
$division = 0;
} else {
throw $e;
}
}

return $this->applyScale($res, $this->normalize($scale));
return $division;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Services/Math/test/ilMathBaseAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ public function mulData()
public function divData()
{
return [
['1', '2', '0.5', self::DEFAULT_SCALE]
['1', '2', '0.5', self::DEFAULT_SCALE],
['', '2', '0', self::DEFAULT_SCALE],
];
}

Expand Down

0 comments on commit ad930ee

Please sign in to comment.