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 | * Minimum of days in the future that the order is lodged. |
||
| 13 | * @var int |
||
| 14 | */ |
||
| 15 | private static $minimum_days_in_the_future = 1; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Standard SS variable |
||
| 19 | */ |
||
| 20 | private static $db = [ |
||
|
|
|||
| 21 | 'Status' => "Enum('Pending, Active, MemberCancelled, AdminCancelled, Finished', 'Pending')", |
||
| 22 | //dates |
||
| 23 | 'Start' => 'Date', |
||
| 24 | 'End' => 'Date', |
||
| 25 | 'Period' => 'Varchar', |
||
| 26 | 'DeliveryDay' => 'Text', |
||
| 27 | //payment |
||
| 28 | 'PaymentMethod' => 'Varchar', |
||
| 29 | "CreditCardOnFile" => "Boolean", |
||
| 30 | "PaymentNote" => "Text", |
||
| 31 | //computed values and notes |
||
| 32 | 'ItemsForSearchPurposes' => 'Text', //FOR SEARCH PURPOSES ONLY! |
||
| 33 | 'Notes' => 'Text' |
||
| 34 | ]; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Standard SS variable |
||
| 38 | */ |
||
| 39 | private static $has_one = [ |
||
| 40 | 'Member' => 'Member', |
||
| 41 | 'OriginatingOrder' => 'Order' |
||
| 42 | ]; |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * Standard SS variable |
||
| 47 | */ |
||
| 48 | private static $has_many = [ |
||
| 49 | 'OrderItems' => 'RepeatOrder_OrderItem', //products & quanitites |
||
| 50 | 'Orders' => 'Order' |
||
| 51 | ]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Standard SS variable. |
||
| 55 | */ |
||
| 56 | private static $indexes = [ |
||
| 57 | "Status" => true |
||
| 58 | ]; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Standard SS variable |
||
| 62 | */ |
||
| 63 | private static $casting = [ |
||
| 64 | "OrderItemList" => "Text", |
||
| 65 | "FirstOrderDate" => "Date", |
||
| 66 | "LastOrderDate" => "Date", |
||
| 67 | "TodaysOrderDate" => "Date", |
||
| 68 | "NextOrderDate" => "Date", |
||
| 69 | "FinalOrderDate" => "Date", |
||
| 70 | "DeliverySchedule" => "Text" |
||
| 71 | ]; |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * Standard SS variable |
||
| 76 | */ |
||
| 77 | private static $searchable_fields = [ |
||
| 78 | "ItemsForSearchPurposes" => "PartialMatchFilter", |
||
| 79 | "Period" => "ExactMatchFilter", |
||
| 80 | "DeliveryDay" => "ExactMatchFilter", |
||
| 81 | "Status" => "ExactMatchFilter" |
||
| 82 | ]; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Standard SS variable |
||
| 86 | */ |
||
| 87 | private static $summary_fields = [ |
||
| 88 | 'ID' => 'Repeat Order ID', |
||
| 89 | 'Member.Surname' => 'Surname', |
||
| 90 | 'Member.Email' => 'Email', |
||
| 91 | 'OrderItemList' => 'Order Item List', |
||
| 92 | 'Start' => 'Start', |
||
| 93 | 'End' => 'End', |
||
| 94 | 'Period' => 'Period', |
||
| 95 | 'DeliveryDay' => 'Delivery Day', |
||
| 96 | 'Status' => 'Status' |
||
| 97 | ]; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Standard SS variable |
||
| 101 | */ |
||
| 102 | private static $default_sort = 'Created DESC'; |
||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * Dropdown options for Period |
||
| 107 | * @var array 'strtotime period' > 'nice name' |
||
| 108 | */ |
||
| 109 | private static $period_fields = [ |
||
| 110 | '1 day' => 'Daily', |
||
| 111 | '1 week' => 'Weekly', |
||
| 112 | '2 weeks' => 'Fornightly', |
||
| 113 | '1 month' => 'Monthly' |
||
| 114 | ]; |
||
| 115 | |||
| 116 | public static function default_period_key() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var Array |
||
| 127 | */ |
||
| 128 | private static $schedule = []; |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * @array |
||
| 133 | */ |
||
| 134 | private static $status_nice = array( |
||
| 135 | 'Pending' => 'Pending', |
||
| 136 | 'Active' => 'Active', |
||
| 137 | 'MemberCancelled' => 'Pending Cancellation', |
||
| 138 | 'AdminCancelled' => 'Cancelled', |
||
| 139 | 'Finished' => 'Finished', |
||
| 140 | ); |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * @var array |
||
| 145 | */ |
||
| 146 | protected static $payment_methods = array( |
||
| 147 | 'DirectCreditPayment' => 'Direct Credit (payment into bank account)' |
||
| 148 | ); |
||
| 149 | public static function set_payment_methods($a) |
||
| 153 | |||
| 154 | public static function default_payment_method_key() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Can it be edited, alias for canEdit |
||
| 164 | * @return Boolean |
||
| 165 | */ |
||
| 166 | public function CanModify($member = null) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Link for viewing |
||
| 173 | * @return String |
||
| 174 | */ |
||
| 175 | public function Link() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Link for editing |
||
| 182 | * @return String |
||
| 183 | */ |
||
| 184 | public function ModifyLink() |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * Link for cancelling |
||
| 192 | * @return String |
||
| 193 | */ |
||
| 194 | public function CancelLink() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Link for end of view / edit / cancel session |
||
| 201 | * @return String |
||
| 202 | */ |
||
| 203 | public function DoneLink() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * returns a list of actual orders that have been created from this repeat order. |
||
| 217 | * @return DOS | Null |
||
| 218 | */ |
||
| 219 | public function AutomaticallyCreatedOrders() |
||
| 232 | |||
| 233 | //==================================================================================================================================================================================== |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Create due draft orders |
||
| 237 | */ |
||
| 238 | public static function create_automatically_created_orders() |
||
| 249 | |||
| 250 | |||
| 251 | /** |
||
| 252 | * adds the orders that |
||
| 253 | */ |
||
| 254 | public function addAutomaticallyCreatedOrders() |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * creates order from repeatorder for a specific day. |
||
| 292 | * IF it does not already exists. |
||
| 293 | * |
||
| 294 | */ |
||
| 295 | protected function createOrderFromRepeatOrder($orderDateInteger) |
||
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * Create a RepeatOrder from a regular Order and its Order Items |
||
| 355 | * @param Order $Order |
||
| 356 | * @return RepeatOrder |
||
| 357 | */ |
||
| 358 | public static function create_repeat_order_from_order(Order $Order) |
||
| 380 | |||
| 381 | |||
| 382 | |||
| 383 | |||
| 384 | |||
| 385 | //================================================================================================================================================================================ |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | public function TableDeliveryDay() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | public function TablePaymentMethod() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | public function TableStatus() |
||
| 413 | |||
| 414 | |||
| 415 | //========================================================================================================================================================== |
||
| 416 | |||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * CMS Fields for ModelAdmin, use different fields for adding/editing |
||
| 421 | * @see sapphire/core/model/DataObject#getCMSFields($params) |
||
| 422 | */ |
||
| 423 | public function getCMSFields() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * CMS Fields to adding via ModelAdmin |
||
| 435 | * @return FieldList |
||
| 436 | */ |
||
| 437 | public function getCMSFields_add() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * CMS Fields to adding via ModelAdmin |
||
| 467 | * @return FieldList |
||
| 468 | */ |
||
| 469 | public function getCMSFields_edit() |
||
| 556 | |||
| 557 | /** |
||
| 558 | * CMS Fields for Popup |
||
| 559 | * @return FieldList |
||
| 560 | */ |
||
| 561 | public function getCMSFields_forPopup() |
||
| 592 | |||
| 593 | |||
| 594 | |||
| 595 | /** |
||
| 596 | * Get previous actual order table |
||
| 597 | * @return ComplexTableField |
||
| 598 | */ |
||
| 599 | public function getCMSPreviousOrders() |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Get products table |
||
| 629 | * @return ComplexTableField |
||
| 630 | */ |
||
| 631 | public function getCMSProductsTable() |
||
| 649 | |||
| 650 | |||
| 651 | |||
| 652 | |||
| 653 | //======================================================================================================================================================================================================================================================================== |
||
| 654 | |||
| 655 | public function onBeforeWrite() |
||
| 660 | |||
| 661 | //=========================================================================================================================================================================================== |
||
| 662 | |||
| 663 | /** |
||
| 664 | * List of products |
||
| 665 | * |
||
| 666 | * @return String |
||
| 667 | */ |
||
| 668 | public function OrderItemList() |
||
| 688 | |||
| 689 | //=========================================================================================================================================================================================== |
||
| 690 | |||
| 691 | /** |
||
| 692 | * The first order date |
||
| 693 | * |
||
| 694 | * @return Date | Null |
||
| 695 | */ |
||
| 696 | public function FirstOrderDate() |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Last date that an order was placed |
||
| 712 | * |
||
| 713 | * @return Date | Null |
||
| 714 | */ |
||
| 715 | public function LastOrderDate() |
||
| 734 | |||
| 735 | /** |
||
| 736 | * today's' date for the order - if ANY! |
||
| 737 | * |
||
| 738 | * @return Date | Null |
||
| 739 | */ |
||
| 740 | public function TodaysOrderDate() |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Next date (from the viewpoint of today) |
||
| 759 | * |
||
| 760 | * @return Date | Null |
||
| 761 | */ |
||
| 762 | public function NextOrderDate() |
||
| 778 | |||
| 779 | |||
| 780 | /** |
||
| 781 | * Last Delivery Date |
||
| 782 | * |
||
| 783 | * @return Date | Null |
||
| 784 | */ |
||
| 785 | public function FinalOrderDate() |
||
| 801 | |||
| 802 | /** |
||
| 803 | * List of delivery dates |
||
| 804 | * |
||
| 805 | * @return String |
||
| 806 | */ |
||
| 807 | public function DeliverySchedule() |
||
| 818 | |||
| 819 | |||
| 820 | /** |
||
| 821 | * Work out the delivery schedule |
||
| 822 | * @return Array |
||
| 823 | */ |
||
| 824 | protected function workOutSchedule() |
||
| 854 | |||
| 855 | |||
| 856 | /** |
||
| 857 | * Are there any orders scheduled for the future |
||
| 858 | * @return Boolean |
||
| 859 | */ |
||
| 860 | public function HasFutureOrders() |
||
| 866 | |||
| 867 | /** |
||
| 868 | * Are there any orders scheduled for today |
||
| 869 | * @return Boolean |
||
| 870 | */ |
||
| 871 | public function HasAnOrderToday() |
||
| 877 | |||
| 878 | //=========================================================================================================================================================================================== |
||
| 879 | |||
| 880 | public function canView($member = null) |
||
| 893 | |||
| 894 | View Code Duplication | public function canEdit($member = null) |
|
| 902 | |||
| 903 | View Code Duplication | public function canDelete($member = null) |
|
| 911 | } |
||
| 912 |