Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Traits\Macroable;
use NumberFormatter;
use RuntimeException;
use ValueError;

class Number
{
Expand Down Expand Up @@ -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;
Comment thread
jnoordsij marked this conversation as resolved.
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
13 changes: 13 additions & 0 deletions tests/Support/SupportNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
Expand Down
Loading