Complex classes like DiscountCouponModifier 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 DiscountCouponModifier, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class DiscountCouponModifier extends OrderModifier |
||
13 | { |
||
14 | // ######################################## *** model defining static variables (e.g. $db, $has_one) |
||
15 | |||
16 | /** |
||
17 | * standard SS Variable |
||
18 | * @var Array |
||
19 | */ |
||
20 | private static $db = array( |
||
|
|||
21 | 'DebugString' => 'HTMLText', |
||
22 | 'SubTotalAmount' => 'Currency', |
||
23 | 'CouponCodeEntered' => 'Varchar(25)' |
||
24 | ); |
||
25 | |||
26 | /** |
||
27 | * standard SS Variable |
||
28 | * @var Array |
||
29 | */ |
||
30 | private static $has_one = array( |
||
31 | "DiscountCouponOption" => "DiscountCouponOption" |
||
32 | ); |
||
33 | |||
34 | /** |
||
35 | * Should the discount be worked out over the the sub-total or |
||
36 | * the Total Total? |
||
37 | * @var Boolean |
||
38 | */ |
||
39 | private static $include_modifiers_in_subtotal = false; |
||
40 | |||
41 | /** |
||
42 | * If this method is present in the Buyable, the related order item will be excluded |
||
43 | * @var Boolean |
||
44 | */ |
||
45 | private static $exclude_buyable_method = 'ExcludeInDiscountCalculation'; |
||
46 | |||
47 | /** |
||
48 | * Standard SS Variable |
||
49 | * @var String |
||
50 | */ |
||
51 | private static $singular_name = "Discount Coupon Entry"; |
||
52 | |||
53 | public function i18n_singular_name() |
||
57 | |||
58 | /** |
||
59 | * Standard SS Variable |
||
60 | * @var String |
||
61 | */ |
||
62 | private static $plural_name = "Discount Coupon Entries"; |
||
63 | |||
64 | public function i18n_plural_name() |
||
68 | |||
69 | // ######################################## *** cms variables + functions (e.g. getCMSFields, $searchableFields) |
||
70 | |||
71 | /** |
||
72 | * Standard SS Method |
||
73 | * @return FieldList |
||
74 | */ |
||
75 | public function getCMSFields() |
||
99 | |||
100 | // ######################################## *** other (non) static variables (e.g. private static $special_name_for_something, protected $order) |
||
101 | |||
102 | /** |
||
103 | * Used in calculations to work out how much we need. |
||
104 | * @var Double | Null |
||
105 | */ |
||
106 | protected $_actualDeductions = null; |
||
107 | |||
108 | // ######################################## *** CRUD functions (e.g. canEdit) |
||
109 | // ######################################## *** init and update functions |
||
110 | /** |
||
111 | * updates all database fields |
||
112 | * |
||
113 | * @param Bool $force - run it, even if it has run already |
||
114 | */ |
||
115 | public function runUpdate($force = false) |
||
124 | |||
125 | // ######################################## *** form functions (e. g. showform and getform) |
||
126 | |||
127 | /** |
||
128 | * Show the form? |
||
129 | * We always show it when there are items in the cart. |
||
130 | * @return Boolean |
||
131 | */ |
||
132 | public function ShowForm() |
||
153 | |||
154 | /** |
||
155 | * @param Controller $optionalController |
||
156 | * @param Validator $optionalValidator |
||
157 | * @return DiscountCouponModifier_Form |
||
158 | */ |
||
159 | public function getModifierForm(Controller $optionalController = null, Validator $optionalValidator = null) |
||
186 | |||
187 | /** |
||
188 | * @param String $code - code that has been entered |
||
189 | * |
||
190 | * @return array |
||
191 | * */ |
||
192 | public function updateCouponCodeEntered($code) |
||
221 | |||
222 | /** |
||
223 | * @param DiscountCouponOption $coupon |
||
224 | */ |
||
225 | public function setCoupon($coupon) |
||
230 | |||
231 | /** |
||
232 | * @param int $couponID |
||
233 | */ |
||
234 | public function setCouponByID($couponID) |
||
239 | |||
240 | // ######################################## *** template functions (e.g. ShowInTable, TableTitle, etc...) ... USES DB VALUES |
||
241 | |||
242 | /** |
||
243 | * @see self::HideInAjaxUpdate |
||
244 | * |
||
245 | * @return boolean |
||
246 | */ |
||
247 | public function ShowInTable() |
||
258 | |||
259 | /** |
||
260 | * @return boolean |
||
261 | */ |
||
262 | public function CanRemove() |
||
266 | |||
267 | /** |
||
268 | * @return float |
||
269 | */ |
||
270 | public function CartValue() |
||
274 | |||
275 | public function getCartValue() |
||
279 | |||
280 | // ######################################## *** inner calculations.... USES CALCULATED VALUES |
||
281 | |||
282 | /** |
||
283 | * Checks for extensions to make sure it is valid... |
||
284 | * @param DiscountCouponOption $coupon |
||
285 | * @return bool returns true if the coupon is valid |
||
286 | */ |
||
287 | protected function isValidAdditional($coupon) |
||
299 | |||
300 | /** |
||
301 | * returns the discount coupon, if any ... |
||
302 | * |
||
303 | * @return DiscountCouponOption | null |
||
304 | */ |
||
305 | protected function myDiscountCouponOption() |
||
323 | |||
324 | private static $_applicable_products_array = null; |
||
325 | |||
326 | /** |
||
327 | * returns an Array of OrderItem IDs |
||
328 | * to which the coupon applies |
||
329 | * @param DiscountCouponOption |
||
330 | * @return Array |
||
331 | */ |
||
332 | protected function applicableProductsArray($coupon) |
||
374 | |||
375 | // ######################################## *** calculate database fields: protected function Live[field name] ... USES CALCULATED VALUES |
||
376 | |||
377 | /** |
||
378 | * @return int |
||
379 | * */ |
||
380 | protected function LiveName() |
||
391 | |||
392 | private static $subtotal = 0; |
||
393 | |||
394 | /** |
||
395 | * @return float |
||
396 | * */ |
||
397 | protected function LiveSubTotalAmount() |
||
436 | |||
437 | protected $_calculatedTotal = null; |
||
438 | |||
439 | /** |
||
440 | * @return float |
||
441 | * */ |
||
442 | protected function LiveCalculatedTotal() |
||
481 | |||
482 | /** |
||
483 | * @return float |
||
484 | * */ |
||
485 | public function LiveTableValue() |
||
489 | |||
490 | /** |
||
491 | * @return String |
||
492 | * */ |
||
493 | protected function LiveDebugString() |
||
497 | |||
498 | /** |
||
499 | * @return String |
||
500 | * */ |
||
501 | protected function LiveCouponCodeEntered() |
||
505 | |||
506 | /** |
||
507 | * @return int |
||
508 | * */ |
||
509 | protected function LiveDiscountCouponOptionID() |
||
513 | |||
514 | // ######################################## *** Type Functions (IsChargeable, IsDeductable, IsNoChange, IsRemoved) |
||
515 | |||
516 | /** |
||
517 | * @return Boolean |
||
518 | * */ |
||
519 | public function IsDeductable() |
||
523 | |||
524 | // ######################################## *** standard database related functions (e.g. onBeforeWrite, onAfterWrite, etc...) |
||
525 | // ######################################## *** AJAX related functions |
||
526 | /** |
||
527 | * some modifiers can be hidden after an ajax update (e.g. if someone enters a discount coupon and it does not exist). |
||
528 | * There might be instances where ShowInTable (the starting point) is TRUE and HideInAjaxUpdate return false. |
||
529 | * @return Boolean |
||
530 | * */ |
||
531 | public function HideInAjaxUpdate() |
||
544 | |||
545 | // ######################################## *** debug functions |
||
546 | } |
||
547 | |||
600 |