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 | * @param string|EcommerceCountry $countryCode |
||
|
|||
52 | * @param bool $force |
||
53 | * @param bool $runAgain |
||
54 | */ |
||
55 | public static function localise_order($countryCode = null, $force = false, $runAgain = false) |
||
139 | |||
140 | |||
141 | public function onInit() |
||
145 | |||
146 | public function onCalculateOrder() |
||
150 | |||
151 | public function updateCMSFields(FieldList $fields) |
||
167 | |||
168 | /** |
||
169 | * Event handler called after writing to the database. |
||
170 | */ |
||
171 | public function onAfterWrite() |
||
184 | |||
185 | public function canView($member = null) |
||
189 | |||
190 | public function canEdit($member = null) |
||
205 | |||
206 | /** |
||
207 | * it is safer to only allow creation on the front-end... |
||
208 | * |
||
209 | */ |
||
210 | public function canCreate($member = null) |
||
214 | |||
215 | /** |
||
216 | * |
||
217 | * @param string (optional) $countryCode |
||
218 | * @return Distributor | null |
||
219 | */ |
||
220 | public function getDistributor($countryCode = null) |
||
231 | |||
232 | /** |
||
233 | * this needs to run as part of the order live update |
||
234 | * |
||
235 | */ |
||
236 | protected function setCountryDetailsForOrder() |
||
267 | |||
268 | |||
269 | /** |
||
270 | * |
||
271 | * 1. adds distribut emails to order step emails ... if $step->SendEmailToDistributor === true |
||
272 | * |
||
273 | * 2. adds country specific data into arrayData that is used for search |
||
274 | * and replace in email ... |
||
275 | * |
||
276 | * @param ArrayData $arrayData |
||
277 | */ |
||
278 | public function updateReplacementArrayForEmail(ArrayData $arrayData) |
||
318 | } |
||
319 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.