bounds()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 3
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Helpers;
4
5
/**
6
 * @param bool|float|int|null $value
7
 * @return bool
8
 */
9
function is_negative($value): bool
10
{
11 55
    if ($value === false) {
12 1
        $value = -1;
13
    }
14 55
    return ((float)$value < 0);
15
}
16
17
/**
18
 * Puts value in bounds
19
 *
20
 * @param float $value
21
 * @param float $min [optional] Default -1
22
 * @param float $max [optional] Default 1
23
 * @return float
24
 */
25
function bounds(float $value, float $min = -1.0, float $max = 1.0): float
26
{
27 85
    if ($value < $min) {
28 4
        $value = $min;
29 81
    } elseif ($value > $max) {
30 5
        $value = $max;
31
    }
32 85
    return $value;
33
}
34
35
/**
36
 * @param string $numeric
37
 * @return string
38
 */
39
function trim_zeros(string $numeric): string
40
{
41
    $numeric =
42 17
        \ltrim(
43 17
            false !== \strpos($numeric, '.') ? \rtrim(\rtrim($numeric, '0'), '.') : $numeric,
44 17
            '0, '
45
        );
46
    return
47 17
        '' === $numeric ? '0' : $numeric;
48
}
49