Complex classes like ProductAttributeType 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 ProductAttributeType, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class ProductAttributeType extends DataObject implements EditableEcommerceObject |
||
|
|||
15 | { |
||
16 | |||
17 | /** |
||
18 | * Standard SS variable. |
||
19 | */ |
||
20 | private static $api_access = array( |
||
21 | 'view' => array( |
||
22 | "Name", |
||
23 | "Label", |
||
24 | "Values" |
||
25 | ) |
||
26 | ); |
||
27 | |||
28 | /** |
||
29 | * Standard SS variable. |
||
30 | */ |
||
31 | private static $db = array( |
||
32 | 'Name' => 'Varchar', //for back-end use |
||
33 | 'Label' => 'Varchar', //for front-end use |
||
34 | 'Sort' => 'Int', //for front-end use |
||
35 | 'MergeIntoNote' => 'Varchar(255)' |
||
36 | //'Unit' => 'Varchar' //TODO: for future use |
||
37 | ); |
||
38 | |||
39 | /** |
||
40 | * Standard SS variable. |
||
41 | */ |
||
42 | private static $has_one = array( |
||
43 | 'MoreInfoLink' => 'SiteTree', |
||
44 | 'MergeInto' => 'ProductAttributeType' |
||
45 | ); |
||
46 | |||
47 | /** |
||
48 | * Standard SS variable. |
||
49 | */ |
||
50 | private static $has_many = array( |
||
51 | 'Values' => 'ProductAttributeValue' |
||
52 | ); |
||
53 | |||
54 | /** |
||
55 | * Standard SS variable. |
||
56 | */ |
||
57 | private static $summary_fields = array( |
||
58 | 'FullName' => 'Type' |
||
59 | ); |
||
60 | |||
61 | /** |
||
62 | * Standard SS variable. |
||
63 | */ |
||
64 | private static $searchable_fields = array( |
||
65 | 'Name' => 'PartialMatchFilter', |
||
66 | 'Label' => 'PartialMatchFilter' |
||
67 | ); |
||
68 | |||
69 | /** |
||
70 | * Standard SS variable. |
||
71 | */ |
||
72 | private static $belongs_many_many = array( |
||
73 | 'Products' => 'Product' |
||
74 | ); |
||
75 | |||
76 | /** |
||
77 | * Standard SS variable. |
||
78 | */ |
||
79 | private static $casting = array( |
||
80 | 'FullName' => 'Varchar' |
||
81 | ); |
||
82 | |||
83 | /** |
||
84 | * Standard SS variable. |
||
85 | */ |
||
86 | private static $indexes = array( |
||
87 | "Sort" => true |
||
88 | ); |
||
89 | |||
90 | /** |
||
91 | * Standard SS variable. |
||
92 | */ |
||
93 | private static $default_sort = "\"Sort\" ASC, \"Name\""; |
||
94 | |||
95 | private static $dropdown_field_for_orderform = 'DropdownField'; |
||
96 | |||
97 | /** |
||
98 | * Standard SS variable. |
||
99 | */ |
||
100 | private static $singular_name = "Variation Attribute Type"; |
||
101 | public function i18n_singular_name() |
||
105 | |||
106 | /** |
||
107 | * Standard SS variable. |
||
108 | */ |
||
109 | private static $plural_name = "Variation Attribute Types"; |
||
110 | public function i18n_plural_name() |
||
119 | |||
120 | /** |
||
121 | * finds or makes a ProductAttributeType, based on the lower case Name. |
||
122 | * |
||
123 | * @param String $name |
||
124 | * @param Boolean $create |
||
125 | * |
||
126 | * @return ProductAttributeType |
||
127 | */ |
||
128 | public static function find_or_make($name, $create = true) |
||
147 | |||
148 | /** |
||
149 | * Standard SS Methodd. |
||
150 | */ |
||
151 | public function getCMSFields() |
||
184 | |||
185 | /** |
||
186 | * link to edit the record |
||
187 | * @param String | Null $action - e.g. edit |
||
188 | * @return String |
||
189 | */ |
||
190 | public function CMSEditLink($action = null) |
||
198 | |||
199 | /** |
||
200 | * add more values to a type |
||
201 | * array should be an something like red, blue, orange (strings NOT objects) |
||
202 | * @param Array |
||
203 | */ |
||
204 | public function addValues(array $values) |
||
209 | |||
210 | /** |
||
211 | * takes an array of values |
||
212 | * and finds them or creates them. |
||
213 | * |
||
214 | * @param Array $values |
||
215 | * @return ArrayList |
||
216 | */ |
||
217 | public function convertArrayToValues(array $values) |
||
231 | |||
232 | /** |
||
233 | * |
||
234 | * @param String $emptyString |
||
235 | * @param DataList $values |
||
236 | * |
||
237 | * @return DropdownField | HiddenField |
||
238 | */ |
||
239 | public function getDropDownField($emptystring = null, $values = null) |
||
255 | |||
256 | private static $_drop_down_values = array(); |
||
257 | |||
258 | /** |
||
259 | * |
||
260 | * @param DataList $values |
||
261 | * |
||
262 | * @return array |
||
263 | */ |
||
264 | public function getValuesForDropdown($values = null) |
||
283 | |||
284 | /** |
||
285 | * It can be deleted if all its Values can be deleted only... |
||
286 | * |
||
287 | * @return Boolean |
||
288 | */ |
||
289 | public function canDelete($member = null) |
||
303 | |||
304 | /** |
||
305 | * standard SS method |
||
306 | * Adds a name if there is no name. |
||
307 | * Adds a label is there is no label. |
||
308 | * |
||
309 | */ |
||
310 | public function onBeforeWrite() |
||
326 | |||
327 | public function onAfterWrite() |
||
388 | |||
389 | /** |
||
390 | * Delete all the values |
||
391 | * that are related to this type. |
||
392 | */ |
||
393 | public function onBeforeDelete() |
||
405 | |||
406 | public function cleanup() |
||
431 | |||
432 | /** |
||
433 | * useful for GridField |
||
434 | * @return String |
||
435 | */ |
||
436 | public function getFullName() |
||
447 | } |
||
448 |
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.