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 RepeatOrder 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 RepeatOrder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class RepeatOrder extends DataObject |
||
8 | { |
||
9 | |||
10 | |||
11 | ####################### |
||
12 | ### Names Section |
||
13 | ####################### |
||
14 | |||
15 | private static $singular_name = 'Repeat Order'; |
||
|
|||
16 | |||
17 | public function i18n_singular_name() |
||
18 | { |
||
19 | return _t(self::class.'.SINGULAR_NAME', 'Repeat Order'); |
||
20 | } |
||
21 | |||
22 | private static $plural_name = 'Repeat Orders'; |
||
23 | |||
24 | public function i18n_plural_name() |
||
25 | { |
||
26 | return _t(self::class.'.PLURAL_NAME', 'Repeat Orders'); |
||
27 | } |
||
28 | |||
29 | |||
30 | |||
31 | /** |
||
32 | * Minimum of days in the future that the order is lodged. |
||
33 | * @var int |
||
34 | */ |
||
35 | private static $minimum_days_in_the_future = 1; |
||
36 | |||
37 | /** |
||
38 | * Standard SS variable |
||
39 | */ |
||
40 | private static $db = [ |
||
41 | 'Status' => 'Enum(\'Pending, Active, MemberCancelled, AdminCancelled, Finished\', \'Pending\')', |
||
42 | //dates |
||
43 | 'Start' => 'Date', |
||
44 | 'End' => 'Date', |
||
45 | 'Period' => 'Varchar', |
||
46 | //payment |
||
47 | 'PaymentMethod' => 'Varchar', |
||
48 | 'CreditCardOnFile' => 'Boolean', |
||
49 | 'PaymentNote' => 'Text', |
||
50 | //computed values and notes |
||
51 | 'ItemsForSearchPurposes' => 'Text', //FOR SEARCH PURPOSES ONLY! |
||
52 | 'Notes' => 'Text' |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * Standard SS variable |
||
57 | */ |
||
58 | private static $has_one = [ |
||
59 | 'Member' => 'Member', |
||
60 | 'OriginatingOrder' => 'Order' |
||
61 | ]; |
||
62 | |||
63 | |||
64 | /** |
||
65 | * Standard SS variable |
||
66 | */ |
||
67 | private static $has_many = [ |
||
68 | 'OrderItems' => 'RepeatOrder_OrderItem', //products & quanitites |
||
69 | 'Orders' => 'Order' |
||
70 | ]; |
||
71 | |||
72 | /** |
||
73 | * Standard SS variable. |
||
74 | */ |
||
75 | private static $indexes = [ |
||
76 | 'Status' => true, |
||
77 | 'Start' => true, |
||
78 | 'End' => true, |
||
79 | 'Period' => true, |
||
80 | 'ItemsForSearchPurposes' => true |
||
81 | ]; |
||
82 | |||
83 | /** |
||
84 | * Standard SS variable |
||
85 | */ |
||
86 | private static $casting = [ |
||
87 | 'OrderItemList' => 'Text', |
||
88 | 'FirstOrderDate' => 'Date', |
||
89 | 'LastOrderDate' => 'Date', |
||
90 | 'TodaysOrderDate' => 'Date', |
||
91 | 'NextOrderDate' => 'Date', |
||
92 | 'FinalOrderDate' => 'Date', |
||
93 | 'DeliverySchedule' => 'Text', |
||
94 | 'HasFutureOrders' => 'Boolean' |
||
95 | ]; |
||
96 | |||
97 | ####################### |
||
98 | ### Field Names and Presentation Section |
||
99 | ####################### |
||
100 | |||
101 | /** |
||
102 | * Standard SS variable |
||
103 | */ |
||
104 | private static $searchable_fields = [ |
||
105 | 'Status' => 'ExactMatchFilter', |
||
106 | 'Period' => 'PartialMatchFilter', |
||
107 | //payment |
||
108 | 'PaymentMethod' => 'PartialMatchFilter', |
||
109 | 'CreditCardOnFile' => 'ExactMatchFilter', |
||
110 | 'PaymentNote' => 'PartialMatchFilter', |
||
111 | //computed values and notes |
||
112 | 'Notes' => 'PartialMatchFilter', |
||
113 | 'ItemsForSearchPurposes' => 'PartialMatchFilter', |
||
114 | ]; |
||
115 | |||
116 | /** |
||
117 | * Standard SS variable |
||
118 | */ |
||
119 | private static $summary_fields = [ |
||
120 | 'ID' => 'Repeat Order ID', |
||
121 | 'Member.Surname' => 'Surname', |
||
122 | 'Member.Email' => 'Email', |
||
123 | 'HasFutureOrders.Nice' => 'Future Orders', |
||
124 | 'OrderItemList' => 'Order Item List', |
||
125 | 'Start' => 'Start', |
||
126 | 'End' => 'End', |
||
127 | 'Period' => 'Period', |
||
128 | 'Status' => 'Status' |
||
129 | ]; |
||
130 | |||
131 | |||
132 | |||
133 | private static $field_labels = [ |
||
134 | 'Status' => 'Status', |
||
135 | //dates |
||
136 | 'Start' => 'Start Date', |
||
137 | 'End' => 'End Date', |
||
138 | 'Period' => 'Repeat Schedule', |
||
139 | //payment |
||
140 | 'PaymentMethod' => 'Payment Type', |
||
141 | 'CreditCardOnFile' => 'Credit Card on File', |
||
142 | 'PaymentNote' => 'Payment Notes', |
||
143 | 'Notes' => 'Notes' |
||
144 | ]; |
||
145 | |||
146 | |||
147 | |||
148 | /** |
||
149 | * Standard SS variable |
||
150 | */ |
||
151 | private static $default_sort = 'Created DESC'; |
||
152 | |||
153 | |||
154 | /** |
||
155 | * Dropdown options for Period |
||
156 | * @var array 'strtotime period' > 'nice name' |
||
157 | */ |
||
158 | private static $period_fields = [ |
||
159 | '1 day' => 'Daily', |
||
160 | '1 week' => 'Weekly', |
||
161 | '2 weeks' => 'Fornightly', |
||
162 | '1 month' => 'Monthly' |
||
163 | ]; |
||
164 | |||
165 | /** |
||
166 | * @return string |
||
167 | */ |
||
168 | public static function default_period_key() |
||
169 | { |
||
170 | if ($a = Config::inst()->get('RepeatOrder', 'period_fields')) { |
||
171 | foreach ($a as $k => $v) { |
||
172 | return $k; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | return ''; |
||
177 | } |
||
178 | |||
179 | |||
180 | /** |
||
181 | * @var array |
||
182 | */ |
||
183 | private static $status_nice = array( |
||
184 | 'Pending' => 'Pending', |
||
185 | 'Active' => 'Active', |
||
186 | 'MemberCancelled' => 'Pending Cancellation', |
||
187 | 'AdminCancelled' => 'Cancelled', |
||
188 | 'Finished' => 'Finished', |
||
189 | ); |
||
190 | |||
191 | |||
192 | /** |
||
193 | * @var array |
||
194 | */ |
||
195 | protected static $payment_methods = array( |
||
196 | 'DirectCreditPayment' => 'Direct Credit (payment into bank account)' |
||
197 | ); |
||
198 | |||
199 | /** |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | public static function default_payment_method_key() |
||
204 | { |
||
205 | $a = Config::inst()->get('RepeatOrder', 'payment_methods'); |
||
206 | foreach ($a as $k => $v) { |
||
207 | return $k; |
||
208 | } |
||
209 | return ''; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Can it be edited, alias for canEdit |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | public function canModify($member = null) |
||
218 | { |
||
219 | return $this->canEdit(); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Link for viewing |
||
224 | * @return string |
||
225 | */ |
||
226 | public function Link() |
||
230 | |||
231 | /** |
||
232 | * Link for editing |
||
233 | * @return string |
||
234 | */ |
||
235 | public function ModifyLink() |
||
239 | |||
240 | |||
241 | /** |
||
242 | * Link for cancelling |
||
243 | * @return string |
||
244 | */ |
||
245 | public function CancelLink() |
||
249 | |||
250 | /** |
||
251 | * Link for end of view / edit / cancel session |
||
252 | * @return string |
||
253 | */ |
||
254 | public function DoneLink() |
||
265 | |||
266 | /** |
||
267 | * returns a list of actual orders that have been created from this repeat order. |
||
268 | * @return ArrayList|null |
||
269 | */ |
||
270 | public function AutomaticallyCreatedOrders() |
||
283 | |||
284 | //=================================================== |
||
285 | //=================================================== |
||
286 | //=================================================== |
||
287 | |||
288 | /** |
||
289 | * Create due draft orders |
||
290 | */ |
||
291 | public static function create_automatically_created_orders() |
||
302 | |||
303 | |||
304 | /** |
||
305 | * adds the orders that |
||
306 | */ |
||
307 | public function addAutomaticallyCreatedOrders() |
||
342 | |||
343 | |||
344 | |||
345 | /** |
||
346 | * creates order from repeatorder for a specific day. |
||
347 | * IF it does not already exists. |
||
348 | * |
||
349 | */ |
||
350 | protected function createOrderFromRepeatOrder($orderDateInteger) |
||
387 | |||
388 | |||
389 | /** |
||
390 | * Create a RepeatOrder from a regular Order and its Order Items |
||
391 | * @param Order $Order |
||
392 | * @return RepeatOrder |
||
393 | */ |
||
394 | public static function create_repeat_order_from_order(Order $Order) |
||
417 | |||
418 | |||
419 | |||
420 | |||
421 | |||
422 | |||
423 | /** |
||
424 | * @return string |
||
425 | */ |
||
426 | public function TablePaymentMethod() |
||
434 | |||
435 | /** |
||
436 | * @return string |
||
437 | */ |
||
438 | public function TableStatus() |
||
446 | |||
447 | |||
448 | //========================================================================================================================================================== |
||
449 | |||
450 | |||
451 | |||
452 | public function getCMSFields() |
||
542 | |||
543 | |||
544 | //============ |
||
545 | //============ |
||
546 | //============ |
||
547 | |||
548 | public function onBeforeWrite() |
||
553 | |||
554 | |||
555 | //============ |
||
556 | //============ |
||
557 | //============ |
||
558 | |||
559 | /** |
||
560 | * List of products |
||
561 | * |
||
562 | * @return string |
||
563 | */ |
||
564 | public function OrderItemList() |
||
568 | /** |
||
569 | * |
||
570 | * @return string |
||
571 | */ |
||
572 | public function getOrderItemList() |
||
588 | |||
589 | //=========================================================================================================================================================================================== |
||
590 | |||
591 | /** |
||
592 | * The first order date |
||
593 | * |
||
594 | * @return Date|null |
||
595 | */ |
||
596 | public function FirstOrderDate() |
||
600 | |||
601 | View Code Duplication | public function getFirstOrderDate() |
|
610 | |||
611 | /** |
||
612 | * Last date that an order was placed |
||
613 | * |
||
614 | * @return Date|null |
||
615 | */ |
||
616 | public function LastOrderDate() |
||
620 | |||
621 | /** |
||
622 | * Last date that an order was placed |
||
623 | * |
||
624 | * @return Date|null |
||
625 | */ |
||
626 | public function getLastOrderDate() |
||
641 | |||
642 | /** |
||
643 | * today's' date for the order - if ANY! |
||
644 | * |
||
645 | * @return Date|null |
||
646 | */ |
||
647 | public function TodaysOrderDate() |
||
651 | |||
652 | /** |
||
653 | * today's' date for the order - if ANY! |
||
654 | * |
||
655 | * @return Date|null |
||
656 | */ |
||
657 | View Code Duplication | public function getTodaysOrderDate() |
|
669 | |||
670 | /** |
||
671 | * Next date (from the viewpoint of today) |
||
672 | * |
||
673 | * @return Date|null |
||
674 | */ |
||
675 | public function NextOrderDate() |
||
679 | |||
680 | View Code Duplication | public function getNextOrderDate() |
|
692 | |||
693 | |||
694 | /** |
||
695 | * Last Delivery Date |
||
696 | * |
||
697 | * @return Date|null |
||
698 | */ |
||
699 | public function FinalOrderDate() |
||
703 | |||
704 | View Code Duplication | public function getFinalOrderDate() |
|
716 | |||
717 | /** |
||
718 | * List of delivery dates |
||
719 | * |
||
720 | * @return string |
||
721 | */ |
||
722 | public function DeliverySchedule() |
||
726 | |||
727 | public function getDeliverySchedule() |
||
734 | |||
735 | /** |
||
736 | * @var Array |
||
737 | */ |
||
738 | private static $_schedule = []; |
||
739 | |||
740 | /** |
||
741 | * Work out the delivery schedule |
||
742 | * @return Array |
||
743 | */ |
||
744 | protected function workOutSchedule() |
||
769 | |||
770 | |||
771 | /** |
||
772 | * Are there any orders scheduled for the future |
||
773 | * @return bool |
||
774 | */ |
||
775 | public function HasFutureOrders() |
||
779 | |||
780 | /** |
||
781 | * Are there any orders scheduled for the future |
||
782 | * @return bool |
||
783 | */ |
||
784 | public function getHasFutureOrders() |
||
792 | |||
793 | /** |
||
794 | * Are there any orders scheduled for today |
||
795 | * @return bool |
||
796 | */ |
||
797 | public function HasAnOrderToday() |
||
804 | |||
805 | //=========================================================================================================================================================================================== |
||
806 | |||
807 | public function canView($member = null) |
||
820 | |||
821 | View Code Duplication | public function canEdit($member = null) |
|
829 | |||
830 | View Code Duplication | public function canDelete($member = null) |
|
840 | } |
||
841 |