Floats   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 88
Duplicated Lines 13.64 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 22
lcom 0
cbo 0
dl 12
loc 88
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C filter() 12 60 22

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Defines the DominionEnterprises\Filter\Floats class.
4
 */
5
6
namespace DominionEnterprises\Filter;
7
8
/**
9
 * A collection of filters for floats.
10
 */
11
final class Floats
12
{
13
    /**
14
     * Filters $value to a float strictly.
15
     *
16
     * The return value is the float, as expected by the \DominionEnterprises\Filterer class.
17
     *
18
     * @param string|float $value the value to filter to a float
19
     * @param bool $allowNull Set to true if NULL values are allowed. The filtered result of a NULL value is NULL
20
     * @param int $minValue The minimum acceptable value
21
     * @param int $maxValue The maximum acceptable value
22
     * @param bool $castInts Flag to cast $value to float if it is an integer
23
     *
24
     * @return float|null The filtered value
25
     *
26
     * @see is_numeric
27
     * @throws \InvalidArgumentException if $allowNull is not a boolean
28
     * @throws \InvalidArgumentException if $minValue is not null and not a float
29
     * @throws \InvalidArgumentException if $maxValue is not null and not a float
30
     * @throws \InvalidArgumentException if $castInts is not a boolean
31
     * @throws \Exception if $value does not pass is_numeric
32
     * @throws \Exception if $value is hex format
33
     * @throws \Exception if $value is not a string or float
34
     * @throws \Exception if $value overflow or underflows
35
     * @throws \Exception if $value is less than $minValue
36
     * @throws \Exception if $value is greater than $maxValue
37
     */
38
    public static function filter($value, $allowNull = false, $minValue = null, $maxValue = null, $castInts = false)
39
    {
40 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...
41
            throw new \InvalidArgumentException('"' . var_export($allowNull, true) . '" $allowNull was not a bool');
42
        }
43
44 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...
45
            throw new \InvalidArgumentException('"' . var_export($minValue, true) . '" $minValue was not a float');
46
        }
47
48 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...
49
            throw new \InvalidArgumentException('"' . var_export($maxValue, true) . '" $maxValue was not a float');
50
        }
51
52 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...
53
            throw new \InvalidArgumentException('"' . var_export($castInts, true) . '" $castInts was not a bool');
54
        }
55
56
        if ($allowNull === true && $value === null) {
57
            return null;
58
        }
59
60
        $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...
61
        if (is_float($value)) {
62
            $valueFloat = $value;
63
        } elseif (is_int($value) && $castInts) {
64
            $valueFloat = (float)$value;
65
        } elseif (is_string($value)) {
66
            $value = trim($value);
67
68
            if (!is_numeric($value)) {
69
                throw new \Exception("{$value} does not pass is_numeric");
70
            }
71
72
            $value = strtolower($value);
73
74
            //This is the only case (that we know of) where is_numeric does not return correctly castable float
75
            if (strpos($value, 'x') !== false) {
76
                throw new \Exception("{$value} is hex format");
77
            }
78
79
            $valueFloat = (float)$value;
80
        } else {
81
            throw new \Exception('"' . var_export($value, true) . '" $value is not a string');
82
        }
83
84
        if (is_infinite($valueFloat)) {
85
            throw new \Exception("{$value} overflow");
86
        }
87
88
        if ($minValue !== null && $valueFloat < $minValue) {
89
            throw new \Exception("{$valueFloat} is less than {$minValue}");
90
        }
91
92
        if ($maxValue !== null && $valueFloat > $maxValue) {
93
            throw new \Exception("{$valueFloat} is greater than {$maxValue}");
94
        }
95
96
        return $valueFloat;
97
    }
98
}
99