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 |
||
38 | $valueFloat = null; |
||
|
|||
39 | if (is_float($value)) { |
||
40 | $valueFloat = $value; |
||
41 | } elseif (is_int($value) && $castInts) { |
||
42 | $valueFloat = (float)$value; |
||
43 | } elseif (is_string($value)) { |
||
44 | $value = trim($value); |
||
45 | |||
46 | if (!is_numeric($value)) { |
||
47 | throw new Exception("{$value} does not pass is_numeric"); |
||
48 | } |
||
49 | |||
50 | $value = strtolower($value); |
||
51 | |||
52 | //This is the only case (that we know of) where is_numeric does not return correctly cast-able float |
||
53 | if (strpos($value, 'x') !== false) { |
||
54 | throw new Exception("{$value} is hex format"); |
||
55 | } |
||
56 | |||
57 | $valueFloat = (float)$value; |
||
58 | } else { |
||
59 | throw new Exception('"' . var_export($value, true) . '" $value is not a string'); |
||
60 | } |
||
61 | |||
62 | if (is_infinite($valueFloat)) { |
||
63 | throw new Exception("{$value} overflow"); |
||
64 | } |
||
65 | |||
66 | if ($minValue !== null && $valueFloat < $minValue) { |
||
67 | throw new Exception("{$valueFloat} is less than {$minValue}"); |
||
68 | } |
||
69 | |||
70 | if ($maxValue !== null && $valueFloat > $maxValue) { |
||
71 | throw new Exception("{$valueFloat} is greater than {$maxValue}"); |
||
72 | } |
||
73 | |||
74 | return $valueFloat; |
||
75 | } |
||
76 | } |
||
77 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.