Completed
Pull Request — master (#76)
by Chad
04:20
created

Floats::filter()   C

Complexity

Conditions 14
Paths 16

Size

Total Lines 49
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 5.1329
c 0
b 0
f 0
cc 14
eloc 30
nc 16
nop 5

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace TraderInteractive\Filter;
4
5
/**
6
 * A collection of filters for floats.
7
 */
8
final class Floats
9
{
10
    /**
11
     * Filters $value to a float strictly.
12
     *
13
     * The return value is the float, as expected by the \TraderInteractive\Filterer class.
14
     *
15
     * @param string|float $value     the value to filter to a float
16
     * @param bool         $allowNull Set to true if NULL values are allowed. The filtered result of a NULL value is
17
     *                                NULL
18
     * @param float        $minValue  The minimum acceptable value
19
     * @param float        $maxValue  The maximum acceptable value
20
     * @param bool         $castInts  Flag to cast $value to float if it is an integer
21
     *
22
     * @return float|null The filtered value
23
     *
24
     * @throws Exception if $value is greater than $maxValue
25
     * @see is_numeric
26
     */
27
    public static function filter(
28
        $value,
29
        bool $allowNull = false,
30
        float $minValue = null,
31
        float $maxValue = null,
32
        bool $castInts = false
33
    ) {
34
        if ($allowNull === true && $value === null) {
35
            return null;
36
        }
37
38
        $valueFloat = null;
0 ignored issues
show
Unused Code introduced by
$valueFloat is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
39
        if (is_float($value)) {
40
            $valueFloat = $value;
41
        } elseif (is_int($value) && $castInts) {
42
            $valueFloat = (float)$value;
43
        } elseif (is_string($value)) {
44
            $value = trim($value);
45
46
            if (!is_numeric($value)) {
47
                throw new Exception("{$value} does not pass is_numeric");
48
            }
49
50
            $value = strtolower($value);
51
52
            //This is the only case (that we know of) where is_numeric does not return correctly cast-able float
53
            if (strpos($value, 'x') !== false) {
54
                throw new Exception("{$value} is hex format");
55
            }
56
57
            $valueFloat = (float)$value;
58
        } else {
59
            throw new Exception('"' . var_export($value, true) . '" $value is not a string');
60
        }
61
62
        if (is_infinite($valueFloat)) {
63
            throw new Exception("{$value} overflow");
64
        }
65
66
        if ($minValue !== null && $valueFloat < $minValue) {
67
            throw new Exception("{$valueFloat} is less than {$minValue}");
68
        }
69
70
        if ($maxValue !== null && $valueFloat > $maxValue) {
71
            throw new Exception("{$valueFloat} is greater than {$maxValue}");
72
        }
73
74
        return $valueFloat;
75
    }
76
}
77