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 ProductQuestion_OrderItemExtension 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 ProductQuestion_OrderItemExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class ProductQuestion_OrderItemExtension extends DataExtension |
||
|
|||
7 | { |
||
8 | private static $db = array( |
||
9 | 'ProductQuestionsAnswer' => 'HTMLText', |
||
10 | 'JSONAnswers' => 'Text', |
||
11 | ); |
||
12 | |||
13 | private static $casting = array( |
||
14 | 'ProductQuestionsAnswerNOHTML' => 'Text', |
||
15 | 'ConfigureLabel' => 'Varchar', |
||
16 | 'ConfigureLink' => 'Varchar', |
||
17 | ); |
||
18 | |||
19 | public function updateCMSFields(FieldList $fields) |
||
30 | |||
31 | /** |
||
32 | * @return bool |
||
33 | */ |
||
34 | View Code Duplication | public function AllQuestionsAnswered() |
|
46 | |||
47 | /** |
||
48 | * @return bool |
||
49 | */ |
||
50 | View Code Duplication | public function AllRequiredQuestionsAnswered() |
|
64 | |||
65 | /** |
||
66 | * casted variable. |
||
67 | * |
||
68 | * @return string |
||
69 | */ |
||
70 | public function ProductQuestionsAnswerNOHTML() |
||
74 | |||
75 | public function getProductQuestionsAnswerNOHTML() |
||
79 | |||
80 | /** |
||
81 | * can the order item be configured. |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | public function canConfigure() |
||
93 | |||
94 | /** |
||
95 | * can the order item be configured. |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | public function HasRequiredQuestions() |
||
107 | |||
108 | /** |
||
109 | * returns a link to configure an OrderItem |
||
110 | * and adds the relevant requirements. |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | public function ConfigureLabel() |
||
120 | |||
121 | /** |
||
122 | * returns a link to configure an OrderItem |
||
123 | * and adds the relevant requirements. |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | public function ConfigureLink() |
||
133 | |||
134 | /** |
||
135 | * returns the link to edit the products. |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | public function ProductQuestionsAnswerFormLabel() |
||
154 | |||
155 | /** |
||
156 | * returns the link to edit the products. |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | public function ProductQuestionsAnswerFormLink() |
||
171 | |||
172 | /** |
||
173 | * cache only! |
||
174 | * |
||
175 | * @var array |
||
176 | */ |
||
177 | private static $_has_product_questions = array(); |
||
178 | |||
179 | /** |
||
180 | * Does the buyable associated with the orderitem |
||
181 | * have product questions? |
||
182 | * |
||
183 | * @return bool |
||
184 | */ |
||
185 | View Code Duplication | public function HasProductQuestions() |
|
198 | |||
199 | /** |
||
200 | * cache only! |
||
201 | * |
||
202 | * @var array |
||
203 | */ |
||
204 | private static $_product_questions = array(); |
||
205 | |||
206 | /** |
||
207 | * @alias for ProductQuestions |
||
208 | */ |
||
209 | public function ApplicableProductQuestions() |
||
213 | |||
214 | /** |
||
215 | * @return DataList | Null |
||
216 | */ |
||
217 | View Code Duplication | public function ProductQuestions() |
|
229 | |||
230 | /** |
||
231 | * cache only! |
||
232 | * |
||
233 | * @var array |
||
234 | */ |
||
235 | private static $_product_question_product = null; |
||
236 | |||
237 | /** |
||
238 | * @return Product | Null |
||
239 | */ |
||
240 | public function productQuestionBuyable() |
||
248 | |||
249 | /** |
||
250 | * |
||
251 | * @return Form |
||
252 | */ |
||
253 | public function ProductQuestionsAnswerFormInCheckoutPage() |
||
257 | |||
258 | /** |
||
259 | * @return Form |
||
260 | */ |
||
261 | public function ProductQuestionsAnswerForm($controller = null, $name = 'productquestionsanswerselect') |
||
297 | |||
298 | /** |
||
299 | * @return ArrayList | NULL |
||
300 | */ |
||
301 | public function ProductQuestionsAnswers() |
||
324 | |||
325 | /** |
||
326 | * @param array $answers |
||
327 | * ID = ProductQuestion.ID |
||
328 | * "ID" => "Answer" (String) |
||
329 | * @param bool $write |
||
330 | */ |
||
331 | public function updateOrderItemWithProductAnswers($answers, $write = true) |
||
350 | |||
351 | public function onBeforeWrite() |
||
377 | } |
||
378 |
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.