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 |
||
15 | class EEH_Money extends EEH_Base |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * This removes all localized money formatting from the incoming value |
||
20 | * Note: uses this site's currency settings for deciding what is considered a |
||
21 | * "thousands separator" (usually the character "," ) |
||
22 | * and what is a "decimal mark" (usually the character ".") |
||
23 | * |
||
24 | * @param int|float|string $money_value |
||
25 | * @param string $CNT_ISO |
||
26 | * @return float |
||
27 | * @throws EE_Error |
||
28 | */ |
||
29 | public static function strip_localized_money_formatting($money_value, $CNT_ISO = '') |
||
50 | |||
51 | |||
52 | /** |
||
53 | * This converts an incoming localized money value into a standard float item (to three decimal places) |
||
54 | * Only use this if you know the $money_value follows your currency configuration's |
||
55 | * settings. Note: this uses this site's currency settings for deciding what is considered a |
||
56 | * "thousands separator" (usually the character "," ) |
||
57 | * and what is a "decimal mark" (usually the character ".") |
||
58 | * |
||
59 | * @param int|string $money_value |
||
60 | * @return float |
||
61 | * @throws EE_Error |
||
62 | */ |
||
63 | public static function convert_to_float_from_localized_money($money_value) |
||
68 | |||
69 | |||
70 | /** |
||
71 | * For comparing floats. Default operator is '=', but see the $operator below for all options. |
||
72 | * This should be used to compare floats instead of normal '==' because floats |
||
73 | * are inherently imprecise, and so you can sometimes have two floats that appear to be identical |
||
74 | * but actually differ by 0.00000001. |
||
75 | * |
||
76 | * @see http://biostall.com/php-function-to-compare-floating-point-numbers |
||
77 | * @param float $float1 |
||
78 | * @param float $float2 |
||
79 | * @param string $operator The operator. Valid options are =, <=, <, >=, >, <>, eq, lt, lte, gt, gte, ne |
||
80 | * @return bool whether the equation is true or false |
||
81 | * @throws EE_Error |
||
82 | */ |
||
83 | public static function compare_floats($float1, $float2, $operator = '=') |
||
148 | |||
149 | |||
150 | /** |
||
151 | * This returns a localized format string suitable for jQplot. |
||
152 | * |
||
153 | * @param string $CNT_ISO If this is provided, then will attempt to get the currency settings for the country. |
||
154 | * Otherwise will use currency settings for current active country on site. |
||
155 | * @return string |
||
156 | * @throws EE_Error |
||
157 | */ |
||
158 | public static function get_format_for_jqplot($CNT_ISO = '') |
||
169 | |||
170 | |||
171 | /** |
||
172 | * This returns a localized format string suitable for usage with the Google Charts API format param. |
||
173 | * |
||
174 | * @param string $CNT_ISO If this is provided, then will attempt to get the currency settings for the country. |
||
175 | * Otherwise will use currency settings for current active country on site. |
||
176 | * Note: GoogleCharts uses ICU pattern set |
||
177 | * (@see http://icu-project.org/apiref/icu4c/classDecimalFormat.html#_details) |
||
178 | * @return string |
||
179 | * @throws EE_Error |
||
180 | */ |
||
181 | public static function get_format_for_google_charts($CNT_ISO = '') |
||
207 | |||
208 | |||
209 | /** |
||
210 | * @param string $CNT_ISO |
||
211 | * @return EE_Currency_Config|null |
||
212 | * @throws EE_Error |
||
213 | */ |
||
214 | public static function get_currency_config($CNT_ISO = '') |
||
228 | } //end class EEH_Money |
||
229 |