Conditions | 21 |
Paths | 21 |
Total Lines | 68 |
Code Lines | 37 |
Lines | 6 |
Ratio | 8.82 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
40 | public static function filter($value, $allowNull = false, $minValue = null, $maxValue = PHP_INT_MAX) |
||
41 | { |
||
42 | View Code Duplication | if ($allowNull !== false && $allowNull !== true) { |
|
|
|||
43 | throw new \InvalidArgumentException('"' . var_export($allowNull, true) . '" $allowNull was not a bool'); |
||
44 | } |
||
45 | |||
46 | View Code Duplication | if ($minValue !== null && !is_int($minValue)) { |
|
47 | throw new \InvalidArgumentException('"' . var_export($minValue, true) . '" $minValue was not an int'); |
||
48 | } |
||
49 | |||
50 | if (!is_int($maxValue)) { |
||
51 | throw new \InvalidArgumentException('"' . var_export($maxValue, true) . '" $maxValue was not an int'); |
||
52 | } |
||
53 | |||
54 | if ($allowNull === true && $value === null) { |
||
55 | return null; |
||
56 | } |
||
57 | |||
58 | $valueInt = null; |
||
59 | if (is_int($value)) { |
||
60 | $valueInt = $value; |
||
61 | } elseif (is_string($value)) { |
||
62 | $value = trim($value); |
||
63 | |||
64 | if (strlen($value) === 0) { |
||
65 | throw new \Exception('$value string length is zero'); |
||
66 | } |
||
67 | |||
68 | $stringToCheckDigits = $value; |
||
69 | |||
70 | if ($value[0] === '-' || $value[0] === '+') { |
||
71 | $stringToCheckDigits = substr($value, 1); |
||
72 | } |
||
73 | |||
74 | if (!ctype_digit($stringToCheckDigits)) { |
||
75 | throw new \Exception( |
||
76 | "{$value} does not contain all digits, optionally prepended by a '+' or '-' and optionally " |
||
77 | . "surrounded by whitespace" |
||
78 | ); |
||
79 | } |
||
80 | |||
81 | $phpIntMin = ~PHP_INT_MAX; |
||
82 | |||
83 | $casted = (int)$value; |
||
84 | |||
85 | if ($casted === PHP_INT_MAX && $value !== (string)PHP_INT_MAX) { |
||
86 | throw new \Exception("{$value} was greater than a max int of " . PHP_INT_MAX); |
||
87 | } |
||
88 | |||
89 | if ($casted === $phpIntMin && $value !== (string)$phpIntMin) { |
||
90 | throw new \Exception("{$value} was less than a min int of {$phpIntMin}"); |
||
91 | } |
||
92 | |||
93 | $valueInt = $casted; |
||
94 | } else { |
||
95 | throw new \Exception('"' . var_export($value, true) . '" $value is not a string'); |
||
96 | } |
||
97 | |||
98 | if ($minValue !== null && $valueInt < $minValue) { |
||
99 | throw new \Exception("{$valueInt} is less than {$minValue}"); |
||
100 | } |
||
101 | |||
102 | if ($valueInt > $maxValue) { |
||
103 | throw new \Exception("{$valueInt} is greater than {$maxValue}"); |
||
104 | } |
||
105 | |||
106 | return $valueInt; |
||
107 | } |
||
108 | } |
||
109 |
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.