diff --git a/src/Illuminate/Support/Number.php b/src/Illuminate/Support/Number.php index 6ea2430a98a0..707ae89372b4 100644 --- a/src/Illuminate/Support/Number.php +++ b/src/Illuminate/Support/Number.php @@ -5,6 +5,7 @@ use Illuminate\Support\Traits\Macroable; use NumberFormatter; use RuntimeException; +use ValueError; class Number { @@ -310,7 +311,13 @@ protected static function summarize(int|float $number, int $precision = 0, ?int */ public static function clamp(int|float $number, int|float $min, int|float $max) { - return min(max($number, $min), $max); + try { + return clamp($number, $min, $max); + } catch (ValueError $e) { + trigger_error($e->getMessage(), E_USER_DEPRECATED); + + return $max; + } } /** diff --git a/src/Illuminate/Support/composer.json b/src/Illuminate/Support/composer.json index eb8bd0c66b6e..663d3ac76156 100644 --- a/src/Illuminate/Support/composer.json +++ b/src/Illuminate/Support/composer.json @@ -26,6 +26,7 @@ "illuminate/reflection": "^13.0", "nesbot/carbon": "^3.8.4", "symfony/polyfill-php85": "^1.33", + "symfony/polyfill-php86": "^1.36", "voku/portable-ascii": "^2.0.2" }, "replace": { diff --git a/tests/Support/SupportNumberTest.php b/tests/Support/SupportNumberTest.php index 720e4744fbb4..e7376404e01d 100644 --- a/tests/Support/SupportNumberTest.php +++ b/tests/Support/SupportNumberTest.php @@ -3,6 +3,7 @@ namespace Illuminate\Tests\Support; use Illuminate\Support\Number; +use PHPUnit\Framework\Attributes\IgnoreDeprecations; use PHPUnit\Framework\Attributes\RequiresPhpExtension; use PHPUnit\Framework\TestCase; @@ -197,6 +198,18 @@ public function testClamp() $this->assertSame(1, Number::clamp(-10, 1, 5)); } + #[IgnoreDeprecations] + public function testClampIncorrectBounds() + { + // Incorrect bounds always return 'max' + $this->expectUserDeprecationMessage('clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)'); + $this->assertSame(1, Number::clamp(-10, 5, 1)); + $this->expectUserDeprecationMessage('clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)'); + $this->assertSame(1, Number::clamp(4, 5, 1)); + $this->expectUserDeprecationMessage('clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)'); + $this->assertSame(1, Number::clamp(10, 5, 1)); + } + public function testToHuman() { $this->assertSame('1', Number::forHumans(1));