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 PickUpOrDeliveryModifierOptions 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 PickUpOrDeliveryModifierOptions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class PickUpOrDeliveryModifierOptions extends DataObject |
||
|
|
|||
| 8 | { |
||
| 9 | private static $db = array( |
||
| 10 | "IsDefault" => "Boolean", |
||
| 11 | "Code" => "Varchar(25)", |
||
| 12 | "Name" => "Varchar(175)", |
||
| 13 | "Percentage" => "Double", |
||
| 14 | "FixedCost" => "Currency", |
||
| 15 | "WeightMultiplier" => "Double", |
||
| 16 | "WeightUnit" => "Double", |
||
| 17 | "MinimumDeliveryCharge" => "Currency", |
||
| 18 | "MaximumDeliveryCharge" => "Currency", |
||
| 19 | "MinimumOrderAmountForZeroRate" => "Currency", |
||
| 20 | "FreeShippingUpToThisOrderAmount" => "Currency", |
||
| 21 | "Sort" => "Int" |
||
| 22 | ); |
||
| 23 | |||
| 24 | private static $has_one = array( |
||
| 25 | "ExplanationPage" => "SiteTree" |
||
| 26 | ); |
||
| 27 | |||
| 28 | private static $many_many = array( |
||
| 29 | "AvailableInCountries" => "EcommerceCountry", |
||
| 30 | "AvailableInRegions" => "EcommerceRegion", |
||
| 31 | "WeightBrackets" => "PickUpOrDeliveryModifierOptions_WeightBracket", |
||
| 32 | "SubtotalBrackets" => "PickUpOrDeliveryModifierOptions_SubTotalBracket", |
||
| 33 | "ExcludedProducts" => 'Product' |
||
| 34 | ); |
||
| 35 | |||
| 36 | private static $belongs_many_many = array( |
||
| 37 | "ExcludeFromCountries" => "EcommerceCountry", |
||
| 38 | ); |
||
| 39 | |||
| 40 | private static $indexes = array( |
||
| 41 | "IsDefault" => true, |
||
| 42 | "Code" => true |
||
| 43 | ); |
||
| 44 | |||
| 45 | private static $searchable_fields = array( |
||
| 46 | "Code", |
||
| 47 | "Name" => "PartialMatchFilter" |
||
| 48 | ); |
||
| 49 | |||
| 50 | private static $field_labels = array( |
||
| 51 | "IsDefaultNice" => "Default option", |
||
| 52 | "IsDefault" => "Default delivery option?", |
||
| 53 | "Code" => "Code", |
||
| 54 | "Name" => "Long Name", |
||
| 55 | "Percentage" => "Percentage", |
||
| 56 | "FixedCost" => "Fixed cost", |
||
| 57 | "WeightMultiplier" => "Cost per kilogram", |
||
| 58 | "WeightUnit" => "Weight unit in kilograms", |
||
| 59 | "MinimumDeliveryCharge" => "Minimum delivery charge", |
||
| 60 | "MaximumDeliveryCharge" => "Maximum delivery charge", |
||
| 61 | "MinimumOrderAmountForZeroRate" => "Minimum for 0 rate", |
||
| 62 | "FreeShippingUpToThisOrderAmount" => "Free shipping up to", |
||
| 63 | "Sort" => "Sort Index", |
||
| 64 | "ListOfCountries" => "Applicable Countries" |
||
| 65 | ); |
||
| 66 | |||
| 67 | private static $field_labels_right = array( |
||
| 68 | "Percentage" => "number between 0 = 0% and 1 = 100% (e.g. 0.05 would add 5 cents to every dollar ordered).", |
||
| 69 | "FixedCost" => "e.g. entering 10 will add a fixed 10 dollars (or whatever currency is being used) delivery fee.", |
||
| 70 | "WeightMultiplier" => "it multiplies the total weight of the total order with this number to work out charge for delivery. NOTE: you can also setup weight brackets (e.g. from 0 - 1kg = $123, from 1kg - 2kg = $456).", |
||
| 71 | "WeightUnit" => "if you enter 0.1 here, the price will go up with every 100 grams of total order weight.", |
||
| 72 | "MinimumDeliveryCharge" => "minimum delivery charge.", |
||
| 73 | "MaximumDeliveryCharge" => "maximum delivery charge.", |
||
| 74 | "MinimumOrderAmountForZeroRate" => "if this option is selected and the total order is over the amounted entered above then delivery is free.", |
||
| 75 | "FreeShippingUpToThisOrderAmount" => "if this option is selected and the total order is less than the amount entered above then delivery is free. This is for situations where a small order would have a large delivery cost.", |
||
| 76 | "Sort" => "lower numbers show first." |
||
| 77 | ); |
||
| 78 | |||
| 79 | private static $defaults = array( |
||
| 80 | "Code" => "homedelivery", |
||
| 81 | "Name" => "Home Delivery", |
||
| 82 | "Percentage" => 0, |
||
| 83 | "FixedCost" => 0, |
||
| 84 | "WeightMultiplier" => 0, |
||
| 85 | "WeightUnit" => 1, |
||
| 86 | "MinimumDeliveryCharge" => 0, |
||
| 87 | "MaximumDeliveryCharge" => 0, |
||
| 88 | "MinimumOrderAmountForZeroRate" => 0, |
||
| 89 | "Sort" => 0 |
||
| 90 | ); |
||
| 91 | |||
| 92 | private static $summary_fields = array( |
||
| 93 | "IsDefaultNice", |
||
| 94 | "Code", |
||
| 95 | "Name", |
||
| 96 | 'ListOfCountries' |
||
| 97 | ); |
||
| 98 | |||
| 99 | private static $casting = array( |
||
| 100 | "IsDefaultNice" => "Varchar", |
||
| 101 | "ListOfCountries" => "Varchar" |
||
| 102 | ); |
||
| 103 | |||
| 104 | private static $singular_name = "Delivery / Pick-up Option"; |
||
| 105 | public function i18n_singular_name() |
||
| 109 | |||
| 110 | private static $plural_name = "Delivery / Pick-up Options"; |
||
| 111 | public function i18n_plural_name() |
||
| 115 | |||
| 116 | private static $default_sort = "\"IsDefault\" DESC, \"Sort\" ASC, \"Name\" ASC"; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * returns the default PickUpOrDeliveryModifierOptions object |
||
| 120 | * if none exists, it creates one. |
||
| 121 | * @return PickUpOrDeliveryModifierOptions |
||
| 122 | */ |
||
| 123 | public static function default_object() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * returns an array of countries available for all options combined. |
||
| 137 | * like this |
||
| 138 | * array( |
||
| 139 | * "NZ" => "NZ" |
||
| 140 | * ); |
||
| 141 | * @return Array |
||
| 142 | */ |
||
| 143 | public static function get_all_as_country_array() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return String |
||
| 163 | */ |
||
| 164 | public function IsDefaultNice() |
||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * standard SS method |
||
| 176 | * @param Member | NULL |
||
| 177 | * @return Boolean |
||
| 178 | */ |
||
| 179 | View Code Duplication | public function canCreate($member = null) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * standard SS method |
||
| 189 | * @param Member | NULL |
||
| 190 | * @return Boolean |
||
| 191 | */ |
||
| 192 | View Code Duplication | public function canView($member = null) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * standard SS method |
||
| 202 | * @param Member | NULL |
||
| 203 | * @return Boolean |
||
| 204 | */ |
||
| 205 | View Code Duplication | public function canEdit($member = null) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * standard SS method |
||
| 215 | * @param Member | NULL |
||
| 216 | * @return Boolean |
||
| 217 | */ |
||
| 218 | View Code Duplication | public function canDelete($member = null) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * standard SS method |
||
| 228 | */ |
||
| 229 | public function getCMSFields() |
||
| 306 | |||
| 307 | private function createGridField($dataObjectName = "EcommerceCountry", $fieldName = "AvailableInCountries", $title) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * make sure there is only exactly one default |
||
| 353 | */ |
||
| 354 | public function onAfterWrite() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * make sure all are unique codes |
||
| 375 | */ |
||
| 376 | public function onBeforeWrite() |
||
| 396 | |||
| 397 | public function getListOfCountries() |
||
| 409 | } |
||
| 410 |
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.