Failed Conditions
Pull Request — master (#75)
by
unknown
03:37
created

Floats::filter()   C

Complexity

Conditions 22
Paths 20

Size

Total Lines 60
Code Lines 33

Duplication

Lines 12
Ratio 20 %

Importance

Changes 0
Metric Value
dl 12
loc 60
rs 6.1742
c 0
b 0
f 0
cc 22
eloc 33
nc 20
nop 5

How to fix   Long Method    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 NULL
17
     * @param int $minValue The minimum acceptable value
18
     * @param int $maxValue The maximum acceptable value
19
     * @param bool $castInts Flag to cast $value to float if it is an integer
20
     *
21
     * @return float|null The filtered value
22
     *
23
     * @see is_numeric
24
     * @throws \InvalidArgumentException if $allowNull is not a boolean
25
     * @throws \InvalidArgumentException if $minValue is not null and not a float
26
     * @throws \InvalidArgumentException if $maxValue is not null and not a float
27
     * @throws \InvalidArgumentException if $castInts is not a boolean
28
     * @throws Exception if $value does not pass is_numeric
29
     * @throws Exception if $value is hex format
30
     * @throws Exception if $value is not a string or float
31
     * @throws Exception if $value overflow or underflows
32
     * @throws Exception if $value is less than $minValue
33
     * @throws Exception if $value is greater than $maxValue
34
     */
35
    public static function filter($value, $allowNull = false, $minValue = null, $maxValue = null, $castInts = false)
36
    {
37 View Code Duplication
        if ($allowNull !== false && $allowNull !== true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
            throw new \InvalidArgumentException('"' . var_export($allowNull, true) . '" $allowNull was not a bool');
39
        }
40
41 View Code Duplication
        if ($minValue !== null && !is_float($minValue)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
            throw new \InvalidArgumentException('"' . var_export($minValue, true) . '" $minValue was not a float');
43
        }
44
45 View Code Duplication
        if ($maxValue !== null && !is_float($maxValue)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
            throw new \InvalidArgumentException('"' . var_export($maxValue, true) . '" $maxValue was not a float');
47
        }
48
49 View Code Duplication
        if ($castInts !== false && $castInts !== true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            throw new \InvalidArgumentException('"' . var_export($castInts, true) . '" $castInts was not a bool');
51
        }
52
53
        if ($allowNull === true && $value === null) {
54
            return null;
55
        }
56
57
        $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...
58
        if (is_float($value)) {
59
            $valueFloat = $value;
60
        } elseif (is_int($value) && $castInts) {
61
            $valueFloat = (float)$value;
62
        } elseif (is_string($value)) {
63
            $value = trim($value);
64
65
            if (!is_numeric($value)) {
66
                throw new Exception("{$value} does not pass is_numeric");
67
            }
68
69
            $value = strtolower($value);
70
71
            //This is the only case (that we know of) where is_numeric does not return correctly castable float
72
            if (strpos($value, 'x') !== false) {
73
                throw new Exception("{$value} is hex format");
74
            }
75
76
            $valueFloat = (float)$value;
77
        } else {
78
            throw new Exception('"' . var_export($value, true) . '" $value is not a string');
79
        }
80
81
        if (is_infinite($valueFloat)) {
82
            throw new Exception("{$value} overflow");
83
        }
84
85
        if ($minValue !== null && $valueFloat < $minValue) {
86
            throw new Exception("{$valueFloat} is less than {$minValue}");
87
        }
88
89
        if ($maxValue !== null && $valueFloat > $maxValue) {
90
            throw new Exception("{$valueFloat} is greater than {$maxValue}");
91
        }
92
93
        return $valueFloat;
94
    }
95
}
96