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

Ints::filter()   C

Complexity

Conditions 21
Paths 21

Size

Total Lines 68
Code Lines 37

Duplication

Lines 6
Ratio 8.82 %

Importance

Changes 0
Metric Value
dl 6
loc 68
rs 5.6559
c 0
b 0
f 0
cc 21
eloc 37
nc 21
nop 4

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 integers.
7
 */
8
final class Ints
9
{
10
    /**
11
     * Filters $value to an integer strictly.
12
     *
13
     * $value must be an int or contain all digits, optionally prepended by a '+' or '-' and optionally surrounded by
14
     * whitespace to pass the filter.
15
     *
16
     * The return value is the int, as expected by the \TraderInteractive\Filterer class.
17
     *
18
     * @param string|int $value the value to make an integer
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
     *
23
     * @return int|null The filtered value
24
     *
25
     * @throws \InvalidArgumentException if $allowNull is not a boolean
26
     * @throws \InvalidArgumentException if $minValue is not null and not an int
27
     * @throws \InvalidArgumentException if $maxValue is not an int
28
     * @throws Exception if $value string length is zero
29
     * @throws Exception if $value does not contain all digits, optionally prepended by a '+' or '-' and optionally
30
     *                    surrounded by whitespace
31
     * @throws Exception if $value was greater than a max int of PHP_INT_MAX
32
     * @throws Exception if $value was less than a min int of ~PHP_INT_MAX
33
     * @throws Exception if $value is not a string
34
     * @throws Exception if $value is less than $minValue
35
     * @throws Exception if $value is greater than $maxValue
36
     */
37
    public static function filter($value, $allowNull = false, $minValue = null, $maxValue = PHP_INT_MAX)
38
    {
39 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...
40
            throw new \InvalidArgumentException('"' . var_export($allowNull, true) . '" $allowNull was not a bool');
41
        }
42
43 View Code Duplication
        if ($minValue !== null && !is_int($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...
44
            throw new \InvalidArgumentException('"' . var_export($minValue, true) . '" $minValue was not an int');
45
        }
46
47
        if (!is_int($maxValue)) {
48
            throw new \InvalidArgumentException('"' . var_export($maxValue, true) . '" $maxValue was not an int');
49
        }
50
51
        if ($allowNull === true && $value === null) {
52
            return null;
53
        }
54
55
        $valueInt = null;
0 ignored issues
show
Unused Code introduced by
$valueInt 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...
56
        if (is_int($value)) {
57
            $valueInt = $value;
58
        } elseif (is_string($value)) {
59
            $value = trim($value);
60
61
            if (strlen($value) === 0) {
62
                throw new Exception('$value string length is zero');
63
            }
64
65
            $stringToCheckDigits = $value;
66
67
            if ($value[0] === '-' || $value[0] === '+') {
68
                $stringToCheckDigits = substr($value, 1);
69
            }
70
71
            if (!ctype_digit($stringToCheckDigits)) {
72
                throw new Exception(
73
                    "{$value} does not contain all digits, optionally prepended by a '+' or '-' and optionally "
74
                    . "surrounded by whitespace"
75
                );
76
            }
77
78
            $phpIntMin = ~PHP_INT_MAX;
79
80
            $casted = (int)$value;
81
82
            if ($casted === PHP_INT_MAX && $value !== (string)PHP_INT_MAX) {
83
                throw new Exception("{$value} was greater than a max int of " . PHP_INT_MAX);
84
            }
85
86
            if ($casted === $phpIntMin && $value !== (string)$phpIntMin) {
87
                throw new Exception("{$value} was less than a min int of {$phpIntMin}");
88
            }
89
90
            $valueInt = $casted;
91
        } else {
92
            throw new Exception('"' . var_export($value, true) . '" $value is not a string');
93
        }
94
95
        if ($minValue !== null && $valueInt < $minValue) {
96
            throw new Exception("{$valueInt} is less than {$minValue}");
97
        }
98
99
        if ($valueInt > $maxValue) {
100
            throw new Exception("{$valueInt} is greater than {$maxValue}");
101
        }
102
103
        return $valueInt;
104
    }
105
}
106