Skip to content

Commit 512f824

Browse files
committed
[PHP 8.6] Allow max to be null
1 parent c9033e5 commit 512f824

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/Php86/Php86.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@ public static function clamp($value, $min, $max)
3939
self::throwValueErrorIfAvailable('clamp(): Argument #3 ($max) cannot be NAN');
4040
}
4141

42-
if ($max < $min) {
43-
self::throwValueErrorIfAvailable('clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)');
42+
if ($max !== null) {
43+
if ($max < $min) {
44+
self::throwValueErrorIfAvailable('clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)');
45+
}
46+
47+
if ($value > $max) {
48+
return $max;
49+
}
4450
}
4551

4652
if ($value < $min) {
4753
return $min;
4854
}
4955

50-
if ($value > $max) {
51-
return $max;
52-
}
53-
5456
return $value;
5557
}
5658

tests/Php86/Php86Test.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public function testClamp(): void
3333
$this->assertSame('2025-08-20', clamp('2025-08-20', '2025-08-15', '2025-09-15'));
3434
$this->assertSame('2025-08-15', clamp(new \DateTimeImmutable('2025-08-01'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d'));
3535
$this->assertSame('2025-08-20', clamp(new \DateTimeImmutable('2025-08-20'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d'));
36+
$this->assertSame(-1, clamp(null, -1, 1));
37+
$this->assertSame(1, clamp(null, 1, 3));
38+
$this->assertSame(-3, clamp(null, -3, -1));
39+
$this->assertSame(-9999, clamp(-9999, null, 10));
40+
$this->assertSame(10, clamp(12, null, 10));
41+
$this->assertSame(5, clamp(-9999, 5, null));
42+
$this->assertSame(12, clamp(12, 5, null));
3643

3744
$errorMessage = null;
3845

0 commit comments

Comments
 (0)