Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
23 | class EEH_Money extends EEH_Base { |
||
24 | |||
25 | |||
26 | /** |
||
27 | * This removes all localized money formatting from the incoming value |
||
28 | * |
||
29 | * @param int|float|string $money_value |
||
30 | * @param string $CNT_ISO |
||
31 | * @return float |
||
32 | * @throws EE_Error |
||
33 | */ |
||
34 | public static function strip_localized_money_formatting($money_value, $CNT_ISO = '') { |
||
54 | |||
55 | |||
56 | |||
57 | /** |
||
58 | * This converts an incoming localized money value into a standard float item (to three decimal places) |
||
59 | * |
||
60 | * @param int|string $money_value |
||
61 | * @return float |
||
62 | * @throws EE_Error |
||
63 | */ |
||
64 | public static function convert_to_float_from_localized_money($money_value ) { |
||
68 | |||
69 | |||
70 | |||
71 | /** |
||
72 | * For comparing floats. Default operator is '=', but see the $operator below for all options. |
||
73 | * This should be used to compare floats instead of normal '==' because floats |
||
74 | * are inherently imprecise, and so you can sometimes have two floats that appear to be identical |
||
75 | * but actually differ by 0.00000001. |
||
76 | * |
||
77 | * @see http://biostall.com/php-function-to-compare-floating-point-numbers |
||
78 | * @param float $float1 |
||
79 | * @param float $float2 |
||
80 | * @param string $operator The operator. Valid options are =, <=, <, >=, >, <>, eq, lt, lte, gt, gte, ne |
||
81 | * @return bool whether the equation is true or false |
||
82 | * @throws EE_Error |
||
83 | */ |
||
84 | |||
85 | public static function compare_floats( $float1, $float2, $operator='=' ) { |
||
151 | |||
152 | |||
153 | |||
154 | /** |
||
155 | * This returns a localized format string suitable for jQplot. |
||
156 | * |
||
157 | * @param string $CNT_ISO If this is provided, then will attempt to get the currency settings for the country. |
||
158 | * Otherwise will use currency settings for current active country on site. |
||
159 | * @return string |
||
160 | * @throws EE_Error |
||
161 | */ |
||
162 | public static function get_format_for_jqplot( $CNT_ISO = '') { |
||
172 | |||
173 | |||
174 | |||
175 | /** |
||
176 | * This returns a localized format string suitable for usage with the Google Charts API format param. |
||
177 | * |
||
178 | * @param string $CNT_ISO If this is provided, then will attempt to get the currency settings for the country. |
||
179 | * Otherwise will use currency settings for current active country on site. |
||
180 | * Note: GoogleCharts uses ICU pattern set |
||
181 | * (@see http://icu-project.org/apiref/icu4c/classDecimalFormat.html#_details) |
||
182 | * @return string |
||
183 | * @throws EE_Error |
||
184 | */ |
||
185 | public static function get_format_for_google_charts( $CNT_ISO = '' ) { |
||
208 | |||
209 | |||
210 | |||
211 | /** |
||
212 | * @param string $CNT_ISO |
||
213 | * @return EE_Currency_Config|null |
||
214 | * @throws EE_Error |
||
215 | */ |
||
216 | public static function get_currency_config($CNT_ISO = '') |
||
230 | |||
231 | } //end class EEH_Money |
||
232 |