From 21fdb4f7aab8d92b9ec6e001417f12bdd905ab6b Mon Sep 17 00:00:00 2001 From: Jesper Noordsij Date: Mon, 27 Apr 2026 09:34:55 +0200 Subject: [PATCH] Use native clamp function inside util --- composer.json | 3 ++- src/Utils/Helpers.php | 6 +----- tests/Utils/Helpers.clamp().phpt | 4 ++-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 6eb4dd5a2..2c266dd9c 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ } ], "require": { - "php": "8.2 - 8.5" + "php": "8.2 - 8.5", + "symfony/polyfill-php86": "^1.36" }, "require-dev": { "nette/tester": "^2.5", diff --git a/src/Utils/Helpers.php b/src/Utils/Helpers.php index 73d77bba4..c506a4471 100644 --- a/src/Utils/Helpers.php +++ b/src/Utils/Helpers.php @@ -65,11 +65,7 @@ public static function falseToNull(mixed $value): mixed */ public static function clamp(int|float $value, int|float $min, int|float $max): int|float { - if ($min > $max) { - throw new Nette\InvalidArgumentException("Minimum ($min) is not less than maximum ($max)."); - } - - return min(max($value, $min), $max); + return clamp($value, $min, $max); } diff --git a/tests/Utils/Helpers.clamp().phpt b/tests/Utils/Helpers.clamp().phpt index 8277f6172..f4d3bcfc3 100644 --- a/tests/Utils/Helpers.clamp().phpt +++ b/tests/Utils/Helpers.clamp().phpt @@ -14,6 +14,6 @@ Assert::same(19.0, Helpers::clamp(20.0, 10.0, 19.0)); Assert::exception( fn() => Helpers::clamp(20, 30, 10), - InvalidArgumentException::class, - 'Minimum (30) is not less than maximum (10).', + ValueError::class, + 'clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)', );