Passed
Push — develop ( d9e5d3...c6c5bb )
by Alec
02:46
created

bc_bounds()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 4
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 4
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 53
    if ($value === false) {
12 1
        $value = -1;
13
    }
14 53
    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 86
    if ($value < $min) {
28 4
        $value = $min;
29 82
    } elseif ($value > $max) {
30 5
        $value = $max;
31
    }
32 86
    return $value;
33
}
34
35
/**
36
 * @param string $numeric
37
 * @return string
38
 */
39
function trim_zeros(string $numeric): string
40
{
41 7
    return false !== \strpos($numeric, '.') ? \rtrim(\rtrim($numeric, '0'), '.') : $numeric;
42
}
43