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:
Complex classes like EEH_Money often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EEH_Money, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class EEH_Money extends EEH_Base |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * This removes all localized money formatting from the incoming value |
||
| 24 | * Note: uses this site's currency settings for deciding what is considered a |
||
| 25 | * "thousands separator" (usually the character "," ) |
||
| 26 | * and what is a "decimal mark" (usually the character ".") |
||
| 27 | * |
||
| 28 | * @param int|float|string $money_value |
||
| 29 | * @param string $CNT_ISO |
||
| 30 | * @return float |
||
| 31 | * @throws EE_Error |
||
| 32 | * @throws InvalidArgumentException |
||
| 33 | * @throws ReflectionException |
||
| 34 | * @throws InvalidDataTypeException |
||
| 35 | * @throws InvalidInterfaceException |
||
| 36 | */ |
||
| 37 | public static function strip_localized_money_formatting($money_value, $CNT_ISO = '') |
||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * This converts an incoming localized money value into a standard float item (to three decimal places) |
||
| 62 | * Only use this if you know the $money_value follows your currency configuration's |
||
| 63 | * settings. Note: this uses this site's currency settings for deciding what is considered a |
||
| 64 | * "thousands separator" (usually the character "," ) |
||
| 65 | * and what is a "decimal mark" (usually the character ".") |
||
| 66 | * |
||
| 67 | * @param int|string $money_value |
||
| 68 | * @return float |
||
| 69 | * @throws EE_Error |
||
| 70 | * @throws InvalidArgumentException |
||
| 71 | * @throws ReflectionException |
||
| 72 | * @throws InvalidDataTypeException |
||
| 73 | * @throws InvalidInterfaceException |
||
| 74 | */ |
||
| 75 | public static function convert_to_float_from_localized_money($money_value) |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * For comparing floats. Default operator is '=', but see the $operator below for all options. |
||
| 84 | * This should be used to compare floats instead of normal '==' because floats |
||
| 85 | * are inherently imprecise, and so you can sometimes have two floats that appear to be identical |
||
| 86 | * but actually differ by 0.00000001. |
||
| 87 | * |
||
| 88 | * @see http://biostall.com/php-function-to-compare-floating-point-numbers |
||
| 89 | * @param float $float1 |
||
| 90 | * @param float $float2 |
||
| 91 | * @param string $operator The operator. Valid options are =, <=, <, >=, >, <>, eq, lt, lte, gt, gte, ne |
||
| 92 | * @return bool whether the equation is true or false |
||
| 93 | * @throws EE_Error |
||
| 94 | */ |
||
| 95 | public static function compare_floats($float1, $float2, $operator = '=') |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * This returns a localized format string suitable for jqPlot. |
||
| 172 | * |
||
| 173 | * @param string $CNT_ISO If this is provided, then will attempt to get the currency settings for the country. |
||
| 174 | * Otherwise will use currency settings for current active country on site. |
||
| 175 | * @return string |
||
| 176 | * @throws EE_Error |
||
| 177 | * @throws InvalidArgumentException |
||
| 178 | * @throws ReflectionException |
||
| 179 | * @throws InvalidDataTypeException |
||
| 180 | * @throws InvalidInterfaceException |
||
| 181 | */ |
||
| 182 | public static function get_format_for_jqplot($CNT_ISO = '') |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * This returns a localized format string suitable for usage with the Google Charts API format param. |
||
| 197 | * |
||
| 198 | * @param string $CNT_ISO If this is provided, then will attempt to get the currency settings for the country. |
||
| 199 | * Otherwise will use currency settings for current active country on site. |
||
| 200 | * Note: GoogleCharts uses ICU pattern set |
||
| 201 | * (@see http://icu-project.org/apiref/icu4c/classDecimalFormat.html#_details) |
||
| 202 | * @return array |
||
| 203 | * @throws EE_Error |
||
| 204 | * @throws InvalidArgumentException |
||
| 205 | * @throws ReflectionException |
||
| 206 | * @throws InvalidDataTypeException |
||
| 207 | * @throws InvalidInterfaceException |
||
| 208 | */ |
||
| 209 | public static function get_format_for_google_charts($CNT_ISO = '') |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * @param string $CNT_ISO |
||
| 239 | * @param bool $from_db whether to fetch currency data from the admin editable DB, or the immutable JSON file |
||
| 240 | * @return EE_Currency_Config |
||
| 241 | * @throws EE_Error |
||
| 242 | * @throws InvalidArgumentException |
||
| 243 | * @throws ReflectionException |
||
| 244 | * @throws InvalidDataTypeException |
||
| 245 | * @throws InvalidInterfaceException |
||
| 246 | */ |
||
| 247 | public static function get_currency_config($CNT_ISO = '', $from_db = true) |
||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * replacement for EEH_Template::format_currency |
||
| 271 | * This helper takes a raw float value and formats it according to the default config country currency settings, or |
||
| 272 | * the country currency settings from the supplied country ISO code |
||
| 273 | * |
||
| 274 | * @param float $amount raw money value |
||
| 275 | * @param boolean $return_raw whether to return the formatted float value only with no currency sign or code |
||
| 276 | * @param boolean $display_code whether to display the country code (USD). Default = TRUE |
||
| 277 | * @param string $CNT_ISO 2 letter ISO code for a country |
||
| 278 | * @param string $cur_code_span_class |
||
| 279 | * @param boolean $from_db whether to fetch configuration data from the database (which a site admin can change) |
||
| 280 | * or from our JSON file, which admins can't change. Setting to true is usually better |
||
| 281 | * if the value will be displayed to a user; but if communicating with another server, |
||
| 282 | * setting to false will return more reliable formatting. |
||
| 283 | * @since $VID:$ |
||
| 284 | * @return string the html output for the formatted money value |
||
| 285 | * @throws EE_Error |
||
| 286 | * @throws InvalidArgumentException |
||
| 287 | * @throws ReflectionException |
||
| 288 | * @throws InvalidDataTypeException |
||
| 289 | * @throws InvalidInterfaceException |
||
| 290 | */ |
||
| 291 | public static function format_currency( |
||
| 355 | } |
||
| 356 |