Conditions | 22 |
Paths | 20 |
Total Lines | 60 |
Code Lines | 33 |
Lines | 12 |
Ratio | 20 % |
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 |
||
35 | public static function filter($value, $allowNull = false, $minValue = null, $maxValue = null, $castInts = false) |
||
36 | { |
||
37 | View Code Duplication | if ($allowNull !== false && $allowNull !== true) { |
|
|
|||
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)) { |
|
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)) { |
|
46 | throw new \InvalidArgumentException('"' . var_export($maxValue, true) . '" $maxValue was not a float'); |
||
47 | } |
||
48 | |||
49 | View Code Duplication | if ($castInts !== false && $castInts !== true) { |
|
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; |
||
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 |
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.