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 ComplexPriceObject 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 ComplexPriceObject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class ComplexPriceObject extends DataObject |
||
|
|||
10 | { |
||
11 | private static $db = array( |
||
12 | 'NewPrice' => 'Currency', |
||
13 | 'Percentage' => 'Double', |
||
14 | 'Reduction' => 'Currency', |
||
15 | 'From' => 'SS_Datetime', |
||
16 | 'Until' => 'SS_Datetime', |
||
17 | 'NoLongerValid' => 'Boolean' |
||
18 | ); |
||
19 | |||
20 | private static $many_many = array( |
||
21 | 'Groups' => 'Group', |
||
22 | 'EcommerceCountries' => 'EcommerceCountry' |
||
23 | ); |
||
24 | |||
25 | private static $searchable_fields = array( |
||
26 | "NoLongerValid" => true, |
||
27 | "From" => true, |
||
28 | "Until" => true |
||
29 | ); |
||
30 | |||
31 | private static $field_labels = array( |
||
32 | 'From' => 'Valid From', |
||
33 | 'Until' => 'Valid Until', |
||
34 | 'NoLongerValidNice' => 'Valid?' |
||
35 | ); |
||
36 | |||
37 | private static $summary_fields = array( |
||
38 | 'From' => 'Valid From', |
||
39 | 'Until' => 'Valid Until', |
||
40 | 'AppliesTo' => 'Applies To', |
||
41 | 'CalculatedPrice' => 'New Price', |
||
42 | 'NoLongerValidNice' => 'Valid' |
||
43 | ); |
||
44 | |||
45 | private static $casting = array( |
||
46 | 'NoLongerValidNice' => 'Varchar', |
||
47 | 'Buyable' => 'DataOject', |
||
48 | 'Name' => 'Varchar', |
||
49 | 'CalculatedPrice' => 'Currency', |
||
50 | 'AppliesTo' => 'Text' |
||
51 | ); |
||
52 | |||
53 | private static $singular_name = "Price"; |
||
54 | |||
55 | private static $plural_name = "Prices"; |
||
56 | |||
57 | //defaults |
||
58 | private static $default_sort = "\"Until\" DESC"; |
||
59 | |||
60 | public function getCMSFields() |
||
94 | |||
95 | public function Buyable() |
||
111 | |||
112 | /** |
||
113 | * |
||
114 | * works out any price reductions |
||
115 | */ |
||
116 | public function CalculatedPrice() |
||
140 | |||
141 | public function AppliesTo() |
||
163 | |||
164 | public function NoLongerValidNice() |
||
183 | |||
184 | public function validate() |
||
205 | |||
206 | public function Name() |
||
217 | |||
218 | public function onBeforeWrite() |
||
222 | } |
||
223 |
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.