make clamp() more robust
This commit is contained in:
parent
e62cdb0979
commit
314528801b
2 changed files with 10 additions and 11 deletions
|
@ -228,10 +228,6 @@ if (!function_exists('http_parse_headers')) {
|
|||
*/
|
||||
function find_header(array $headers, string $name): ?string
|
||||
{
|
||||
if (!is_array($headers)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$header = null;
|
||||
|
||||
if (array_key_exists($name, $headers)) {
|
||||
|
@ -457,9 +453,9 @@ function page_number(string $input, ?int $max=null): int
|
|||
return $pageNumber;
|
||||
}
|
||||
|
||||
function clamp(?int $val, ?int $min=null, ?int $max=null): int
|
||||
function clamp(int $val, ?int $min=null, ?int $max=null): int
|
||||
{
|
||||
if (!is_numeric($val) || (!is_null($min) && $val < $min)) {
|
||||
if (!is_null($min) && $val < $min) {
|
||||
$val = $min;
|
||||
}
|
||||
if (!is_null($max) && $val > $max) {
|
||||
|
|
|
@ -73,11 +73,14 @@ class PolyfillsTest extends TestCase
|
|||
|
||||
public function test_clamp()
|
||||
{
|
||||
$this->assertEquals(5, clamp(0, 5, 10));
|
||||
$this->assertEquals(5, clamp(5, 5, 10));
|
||||
$this->assertEquals(7, clamp(7, 5, 10));
|
||||
$this->assertEquals(10, clamp(10, 5, 10));
|
||||
$this->assertEquals(10, clamp(15, 5, 10));
|
||||
$this->assertEquals(5, clamp(0, 5, 10)); // too small
|
||||
$this->assertEquals(5, clamp(5, 5, 10)); // lower limit
|
||||
$this->assertEquals(7, clamp(7, 5, 10)); // ok
|
||||
$this->assertEquals(10, clamp(10, 5, 10)); // upper limit
|
||||
$this->assertEquals(10, clamp(15, 5, 10)); // too large
|
||||
$this->assertEquals(0, clamp(0, null, 10)); // no lower limit
|
||||
$this->assertEquals(10, clamp(10, 0, null)); // no upper limit
|
||||
$this->assertEquals(42, clamp(42, null, null)); // no limit
|
||||
}
|
||||
|
||||
public function test_truncate()
|
||||
|
|
Reference in a new issue