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 CountryPrice_OrderDOD 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 CountryPrice_OrderDOD, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class CountryPrice_OrderDOD extends DataExtension |
||
|
|
|||
| 9 | { |
||
| 10 | private static $db = array( |
||
| 11 | 'IP' => 'Varchar(45)', |
||
| 12 | 'CurrencyCountry' => 'Varchar(3)', |
||
| 13 | 'OriginatingCountryCode' => 'Varchar(3)' |
||
| 14 | ); |
||
| 15 | |||
| 16 | private static $has_one = array( |
||
| 17 | 'Distributor' => 'Distributor' |
||
| 18 | ); |
||
| 19 | |||
| 20 | private static $searchable_fields = array( |
||
| 21 | 'DistributorID' => array( |
||
| 22 | 'title' => 'Distributor' |
||
| 23 | ), |
||
| 24 | 'OriginatingCountryCode' => array( |
||
| 25 | 'field' => 'TextField', |
||
| 26 | 'filter' => 'PartialMatchFilter', |
||
| 27 | 'title' => 'Country Code (e.g. NZ)' |
||
| 28 | ) |
||
| 29 | ); |
||
| 30 | |||
| 31 | private static $field_labels = array( |
||
| 32 | 'CurrencyCountry' => 'Currency Country', |
||
| 33 | 'OriginatingCountryCode' => 'Country' |
||
| 34 | ); |
||
| 35 | |||
| 36 | private static $summary_fields = array( |
||
| 37 | 'Distributor.Title' => 'Distributor', |
||
| 38 | 'OriginatingCountryCode' => 'OriginatingCountryCode', |
||
| 39 | 'CurrencyUsed.Title' => 'Currency' |
||
| 40 | ); |
||
| 41 | |||
| 42 | private static $_number_of_times_we_have_run_localise_order = 0; |
||
| 43 | |||
| 44 | private static $only_allow_within_country_sales = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * this method basically makes sure that the Order |
||
| 48 | * has all the localised stuff attached to it, specifically |
||
| 49 | * the right currency |
||
| 50 | */ |
||
| 51 | public static function localise_order($countryCode = null, $force = false) |
||
| 124 | |||
| 125 | |||
| 126 | public function onInit() |
||
| 130 | |||
| 131 | public function onCalculateOrder() |
||
| 135 | |||
| 136 | public function updateCMSFields(FieldList $fields) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Event handler called after writing to the database. |
||
| 155 | */ |
||
| 156 | public function onAfterWrite() |
||
| 169 | |||
| 170 | public function canView($member = null) |
||
| 174 | |||
| 175 | public function canEdit($member = null) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * it is safer to only allow creation on the front-end... |
||
| 193 | * |
||
| 194 | */ |
||
| 195 | public function canCreate($member = null) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * |
||
| 202 | * @param string (optional) $countryCode |
||
| 203 | * @return Distributor | null |
||
| 204 | */ |
||
| 205 | public function getDistributor($countryCode = null) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * this needs to run as part of the order live update |
||
| 219 | * |
||
| 220 | */ |
||
| 221 | protected function setCountryDetailsForOrder() |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * |
||
| 256 | * 1. adds distribut emails to order step emails ... if $step->SendEmailToDistributor === true |
||
| 257 | * |
||
| 258 | * 2. adds country specific data into arrayData that is used for search |
||
| 259 | * and replace in email ... |
||
| 260 | * |
||
| 261 | * @param ArrayData $arrayData |
||
| 262 | */ |
||
| 263 | public function updateReplacementArrayForEmail(ArrayData $arrayData) |
||
| 303 | } |
||
| 304 |
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.