Skip to content

Commit

Permalink
Make sure result of division is always a float
Browse files Browse the repository at this point in the history
  • Loading branch information
bertramakers authored Oct 8, 2024
1 parent 15f65a2 commit 761c47a
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Schema/Type/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ public function validate(mixed $value, callable $fail): void

// Divide the value by multipleOf instead of using the modulo operator to avoid bugs when using a multipleOf
// that has decimal places. (Since the modulo operator converts the multipleOf to int)
// Note that dividing two integers returns another integer if the result is a whole number. So to make the
// comparison work at all times we need to cast the result to float. Casting both to integer will not work
// as intended since then the result of the division would also be rounded.
if (
$this->multipleOf !== null &&
$value / $this->multipleOf !== round($value / $this->multipleOf)
(float) ($value / $this->multipleOf) !== round($value / $this->multipleOf)
) {
$fail(sprintf('must be a multiple of %d', $this->multipleOf));
}
Expand Down

0 comments on commit 761c47a

Please sign in to comment.