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 MinMaxModifier 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 MinMaxModifier, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class MinMaxModifier extends OrderModifier |
||
|
|
|||
| 10 | { |
||
| 11 | |||
| 12 | //--------------------------------------------------------------------*** static variables |
||
| 13 | |||
| 14 | private static $db = array( |
||
| 15 | "Adjustments" => "HTMLText" |
||
| 16 | ); |
||
| 17 | |||
| 18 | private static $singular_name = "Stock Adjustment"; |
||
| 19 | public function i18n_singular_name() |
||
| 23 | |||
| 24 | private static $plural_name = "Stock Adjustments"; |
||
| 25 | public function i18n_plural_name() |
||
| 29 | |||
| 30 | private static $title = "MinMaxModifier"; |
||
| 31 | |||
| 32 | private static $default_min_quantity = 1; |
||
| 33 | |||
| 34 | private static $default_max_quantity = 9999; |
||
| 35 | |||
| 36 | private static $min_field = "MinQuantity"; |
||
| 37 | |||
| 38 | private static $max_field = "MaxQuantity"; |
||
| 39 | |||
| 40 | private static $adjustment_message = "Based on stock availability, quantities have been adjusted as follows: "; |
||
| 41 | |||
| 42 | private static $sorry_message = "Sorry, your selected value not is available."; |
||
| 43 | |||
| 44 | private static $use_stock_quantities = true; |
||
| 45 | |||
| 46 | private static $ids_of_items_adjusted = array(); |
||
| 47 | |||
| 48 | //-------------------------------------------------------------------- *** static functions |
||
| 49 | |||
| 50 | public static function show_form() |
||
| 55 | |||
| 56 | public static function get_form($controller) |
||
| 60 | |||
| 61 | //-------------------------------------------------------------------- *** cms fuctions |
||
| 62 | |||
| 63 | public function getCMSFields() |
||
| 70 | //-------------------------------------------------------------------- *** display functions |
||
| 71 | public function CanBeRemoved() |
||
| 75 | |||
| 76 | public function ShowInTable() |
||
| 80 | |||
| 81 | |||
| 82 | //--------------------------------------------------------------------*** table values |
||
| 83 | public function LiveCalculatedTotal() |
||
| 88 | |||
| 89 | public function LiveTableValue() |
||
| 93 | |||
| 94 | |||
| 95 | //--------------------------------------------------------------------*** table titles |
||
| 96 | public function LiveName() |
||
| 100 | |||
| 101 | //-------------------------------------------------------------------- *** calculations |
||
| 102 | public static function apply_min_max() |
||
| 208 | |||
| 209 | public function updateForAjax(array $js) |
||
| 223 | |||
| 224 | |||
| 225 | //--------------------------------------------------------------------*** database functions |
||
| 226 | } |
||
| 227 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.