Conditions | 24 |
Paths | 31 |
Total Lines | 64 |
Code Lines | 45 |
Lines | 28 |
Ratio | 43.75 % |
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 |
||
60 | public static function compare_floats( $float1, $float2, $operator='=' ) { |
||
61 | // Check numbers to 5 digits of precision |
||
62 | $epsilon = 0.00001; |
||
63 | |||
64 | $float1 = (float)$float1; |
||
65 | $float2 = (float)$float2; |
||
66 | |||
67 | switch ($operator) { |
||
68 | // equal |
||
69 | case "=": |
||
70 | case "eq": |
||
71 | if (abs($float1 - $float2) < $epsilon) { |
||
72 | return true; |
||
73 | } |
||
74 | break; |
||
75 | // less than |
||
76 | case "<": |
||
77 | View Code Duplication | case "lt": |
|
|
|||
78 | if (abs($float1 - $float2) < $epsilon) { |
||
79 | return false; |
||
80 | } else { |
||
81 | if ($float1 < $float2) { |
||
82 | return true; |
||
83 | } |
||
84 | } |
||
85 | break; |
||
86 | // less than or equal |
||
87 | case "<=": |
||
88 | View Code Duplication | case "lte": |
|
89 | if (self::compare_floats($float1, $float2, '<') || self::compare_floats($float1, $float2, '=')) { |
||
90 | return true; |
||
91 | } |
||
92 | break; |
||
93 | // greater than |
||
94 | case ">": |
||
95 | View Code Duplication | case "gt": |
|
96 | if (abs($float1 - $float2) < $epsilon) { |
||
97 | return false; |
||
98 | } else { |
||
99 | if ($float1 > $float2) { |
||
100 | return true; |
||
101 | } |
||
102 | } |
||
103 | break; |
||
104 | // greater than or equal |
||
105 | case ">=": |
||
106 | View Code Duplication | case "gte": |
|
107 | if (self::compare_floats($float1, $float2, '>') || self::compare_floats($float1, $float2, '=')) { |
||
108 | return true; |
||
109 | } |
||
110 | break; |
||
111 | case "<>": |
||
112 | case "!=": |
||
113 | case "ne": |
||
114 | if (abs($float1 - $float2) > $epsilon) { |
||
115 | return true; |
||
116 | } |
||
117 | break; |
||
118 | default: |
||
119 | throw new EE_Error(__( "Unknown operator '" . $operator . "' in EEH_Money::compare_floats()", 'event_espresso' ) ); |
||
120 | } |
||
121 | |||
122 | return false; |
||
123 | } |
||
124 | |||
182 |
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.