Complex classes like Order 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 Order, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class Order extends DataObject |
||
35 | { |
||
36 | /** |
||
37 | * Status codes and what they mean: |
||
38 | * |
||
39 | * Unpaid (default): Order created but no successful payment by customer yet |
||
40 | * Query: Order not being processed yet (customer has a query, or could be out of stock) |
||
41 | * Paid: Order successfully paid for by customer |
||
42 | * Processing: Order paid for, package is currently being processed before shipping to customer |
||
43 | * Sent: Order paid for, processed for shipping, and now sent to the customer |
||
44 | * Complete: Order completed (paid and shipped). Customer assumed to have received their goods |
||
45 | * AdminCancelled: Order cancelled by the administrator |
||
46 | * MemberCancelled: Order cancelled by the customer (Member) |
||
47 | */ |
||
48 | private static $db = array( |
||
49 | 'Total' => 'Currency', |
||
50 | 'Reference' => 'Varchar', //allow for customised order numbering schemes |
||
51 | //status |
||
52 | 'Placed' => "SS_Datetime", //date the order was placed (went from Cart to Order) |
||
53 | 'Paid' => 'SS_Datetime', //no outstanding payment left |
||
54 | 'ReceiptSent' => 'SS_Datetime', //receipt emailed to customer |
||
55 | 'Printed' => 'SS_Datetime', |
||
56 | 'Dispatched' => 'SS_Datetime', //products have been sent to customer |
||
57 | 'Status' => "Enum('Unpaid,Paid,Processing,Sent,Complete,AdminCancelled,MemberCancelled,Cart','Cart')", |
||
58 | //customer (for guest orders) |
||
59 | 'FirstName' => 'Varchar', |
||
60 | 'Surname' => 'Varchar', |
||
61 | 'Email' => 'Varchar', |
||
62 | 'Notes' => 'Text', |
||
63 | 'IPAddress' => 'Varchar(15)', |
||
64 | //separate shipping |
||
65 | 'SeparateBillingAddress' => 'Boolean', |
||
66 | // keep track of customer locale |
||
67 | 'Locale' => 'DBLocale', |
||
68 | ); |
||
69 | |||
70 | private static $has_one = array( |
||
71 | 'Member' => 'Member', |
||
72 | 'ShippingAddress' => 'Address', |
||
73 | 'BillingAddress' => 'Address', |
||
74 | ); |
||
75 | |||
76 | private static $has_many = array( |
||
77 | 'Items' => 'OrderItem', |
||
78 | 'Modifiers' => 'OrderModifier', |
||
79 | 'OrderStatusLogs' => 'OrderStatusLog', |
||
80 | ); |
||
81 | |||
82 | private static $defaults = array( |
||
83 | 'Status' => 'Cart', |
||
84 | ); |
||
85 | |||
86 | private static $casting = array( |
||
87 | 'FullBillingAddress' => 'Text', |
||
88 | 'FullShippingAddress' => 'Text', |
||
89 | 'Total' => 'Currency', |
||
90 | 'SubTotal' => 'Currency', |
||
91 | 'TotalPaid' => 'Currency', |
||
92 | 'Shipping' => 'Currency', |
||
93 | 'TotalOutstanding' => 'Currency', |
||
94 | ); |
||
95 | |||
96 | private static $summary_fields = array( |
||
97 | 'Reference' => 'Order No', |
||
98 | 'Placed' => 'Date', |
||
99 | 'Name' => 'Customer', |
||
100 | 'LatestEmail' => 'Email', |
||
101 | 'Total' => 'Total', |
||
102 | 'Status' => 'Status', |
||
103 | ); |
||
104 | |||
105 | private static $searchable_fields = array( |
||
106 | 'Reference' => array(), |
||
107 | 'FirstName' => array( |
||
108 | 'title' => 'Customer Name', |
||
109 | ), |
||
110 | 'Email' => array( |
||
111 | 'title' => 'Customer Email', |
||
112 | ), |
||
113 | 'Status' => array( |
||
114 | 'filter' => 'ExactMatchFilter', |
||
115 | 'field' => 'CheckboxSetField', |
||
116 | ), |
||
117 | ); |
||
118 | |||
119 | private static $singular_name = "Order"; |
||
120 | |||
121 | private static $plural_name = "Orders"; |
||
122 | |||
123 | private static $default_sort = "\"Placed\" DESC, \"Created\" DESC"; |
||
124 | |||
125 | /** |
||
126 | * Statuses for orders that have been placed. |
||
127 | */ |
||
128 | private static $placed_status = array( |
||
129 | 'Paid', |
||
130 | 'Unpaid', |
||
131 | 'Processing', |
||
132 | 'Sent', |
||
133 | 'Complete', |
||
134 | 'MemberCancelled', |
||
135 | 'AdminCancelled', |
||
136 | ); |
||
137 | |||
138 | /** |
||
139 | * Statuses for which an order can be paid for |
||
140 | */ |
||
141 | private static $payable_status = array( |
||
142 | 'Cart', |
||
143 | 'Unpaid', |
||
144 | 'Processing', |
||
145 | 'Sent', |
||
146 | ); |
||
147 | |||
148 | /** |
||
149 | * Statuses that shouldn't show in user account. |
||
150 | */ |
||
151 | private static $hidden_status = array('Cart'); |
||
152 | |||
153 | |||
154 | /** |
||
155 | * Status for logging changes |
||
156 | * @var array |
||
157 | */ |
||
158 | private static $log_status = array(); |
||
159 | |||
160 | /** |
||
161 | * Flags to determine when an order can be cancelled. |
||
162 | */ |
||
163 | private static $cancel_before_payment = true; |
||
164 | |||
165 | private static $cancel_before_processing = false; |
||
166 | |||
167 | private static $cancel_before_sending = false; |
||
168 | |||
169 | private static $cancel_after_sending = false; |
||
170 | |||
171 | /** |
||
172 | * Place an order before payment processing begins |
||
173 | * |
||
174 | * @var boolean |
||
175 | */ |
||
176 | private static $place_before_payment = false; |
||
177 | |||
178 | /** |
||
179 | * Modifiers represent the additional charges or |
||
180 | * deductions associated to an order, such as |
||
181 | * shipping, taxes, vouchers etc. |
||
182 | */ |
||
183 | private static $modifiers = array(); |
||
184 | |||
185 | private static $rounding_precision = 2; |
||
186 | |||
187 | private static $reference_id_padding = 5; |
||
188 | |||
189 | /** |
||
190 | * @var boolean Will allow completion of orders with GrandTotal=0, |
||
191 | * which could be the case for orders paid with loyalty points or vouchers. |
||
192 | * Will send the "Paid" date on the order, even though no actual payment was taken. |
||
193 | * Will trigger the payment related extension points: |
||
194 | * Order->onPayment, OrderItem->onPayment, Order->onPaid. |
||
195 | */ |
||
196 | private static $allow_zero_order_total = false; |
||
197 | |||
198 | public static function get_order_status_options() |
||
206 | |||
207 | /** |
||
208 | * Create CMS fields for cms viewing and editing orders |
||
209 | */ |
||
210 | public function getCMSFields() |
||
234 | |||
235 | /** |
||
236 | * Adjust scafolded search context |
||
237 | * |
||
238 | * @return SearchContext the updated search context |
||
239 | */ |
||
240 | public function getDefaultSearchContext() |
||
292 | 42 | ||
293 | 38 | /** |
|
294 | 38 | * Hack for swapping out relation list with OrderItemList |
|
295 | 38 | */ |
|
296 | 38 | public function getComponents($componentName, $filter = "", $sort = "", $join = "", $limit = null) |
|
310 | 17 | ||
311 | /** |
||
312 | * Returns the subtotal of the items for this order. |
||
313 | 8 | */ |
|
314 | public function SubTotal() |
||
322 | |||
323 | 18 | /** |
|
324 | 1 | * Calculate the total |
|
325 | * |
||
326 | 17 | * @return the final total |
|
327 | 17 | */ |
|
328 | public function calculate() |
||
336 | |||
337 | /** |
||
338 | * This is needed to maintain backwards compatiability with |
||
339 | * some subsystems using modifiers. eg discounts |
||
340 | */ |
||
341 | public function getModifier($className, $forcecreate = false) |
||
346 | 76 | ||
347 | /** |
||
348 | * Enforce rounding precision when setting total |
||
349 | */ |
||
350 | public function setTotal($val) |
||
354 | 19 | ||
355 | /** |
||
356 | * Get final value of order. |
||
357 | * Retrieves value from DataObject's record array. |
||
358 | */ |
||
359 | public function Total() |
||
363 | |||
364 | /** |
||
365 | * Alias for Total. |
||
366 | */ |
||
367 | public function GrandTotal() |
||
371 | |||
372 | /** |
||
373 | * Calculate how much is left to be paid on the order. |
||
374 | * Enforces rounding precision. |
||
375 | * |
||
376 | * Payments that have been authorized via a non-manual gateway should count towards the total paid amount. |
||
377 | 15 | * However, it's possible to exclude these by setting the $includeAuthorized parameter to false, which is |
|
378 | * useful to determine the status of the Order. Order status should only change to 'Paid' when all |
||
379 | 15 | * payments are 'Captured'. |
|
380 | 15 | * |
|
381 | 15 | * @param bool $includeAuthorized whether or not to include authorized payments (excluding manual payments) |
|
382 | 15 | * @return float |
|
383 | */ |
||
384 | public function TotalOutstanding($includeAuthorized = true) |
||
391 | |||
392 | 2 | /** |
|
393 | * Get the order status. This will return a localized value if available. |
||
394 | * |
||
395 | * @return string the payment status |
||
396 | */ |
||
397 | public function getStatusI18N() |
||
401 | 1 | ||
402 | /** |
||
403 | 2 | * Get the link for finishing order processing. |
|
404 | */ |
||
405 | public function Link() |
||
412 | 4 | ||
413 | /** |
||
414 | 4 | * Returns TRUE if the order can be cancelled |
|
415 | 4 | * PRECONDITION: Order is in the DB. |
|
416 | 4 | * |
|
417 | 1 | * @return boolean |
|
418 | 1 | */ |
|
419 | 1 | public function canCancel() |
|
434 | |||
435 | 5 | /** |
|
436 | 1 | * Check if an order can be paid for. |
|
437 | * |
||
438 | 5 | * @return boolean |
|
439 | 5 | */ |
|
440 | public function canPay($member = null) |
||
450 | 1 | ||
451 | /* |
||
452 | * Prevent deleting orders. |
||
453 | * @return boolean |
||
454 | */ |
||
455 | public function canDelete($member = null) |
||
459 | |||
460 | /** |
||
461 | * Check if an order can be viewed. |
||
462 | * |
||
463 | * @return boolean |
||
464 | */ |
||
465 | public function canView($member = null) |
||
469 | |||
470 | 1 | /** |
|
471 | * Check if an order can be edited. |
||
472 | * |
||
473 | * @return boolean |
||
474 | */ |
||
475 | public function canEdit($member = null) |
||
479 | |||
480 | 1 | /** |
|
481 | * Prevent standard creation of orders. |
||
482 | * |
||
483 | * @return boolean |
||
484 | */ |
||
485 | public function canCreate($member = null, $context = array()) |
||
489 | 5 | ||
490 | /** |
||
491 | 5 | * Return the currency of this order. |
|
492 | * Note: this is a fixed value across the entire site. |
||
493 | * |
||
494 | * @return string |
||
495 | */ |
||
496 | public function Currency() |
||
500 | 3 | ||
501 | /** |
||
502 | 2 | * Get the latest email for this order.z |
|
503 | */ |
||
504 | public function getLatestEmail() |
||
511 | |||
512 | /** |
||
513 | * Gets the name of the customer. |
||
514 | */ |
||
515 | public function getName() |
||
521 | |||
522 | public function getTitle() |
||
526 | |||
527 | /** |
||
528 | * Get shipping address, or member default shipping address. |
||
529 | */ |
||
530 | public function getShippingAddress() |
||
534 | 6 | ||
535 | 6 | /** |
|
536 | * Get billing address, if marked to use seperate address, otherwise use shipping address, |
||
537 | * or the member default billing address. |
||
538 | */ |
||
539 | public function getBillingAddress() |
||
547 | |||
548 | 7 | /** |
|
549 | * @param string $type - Billing or Shipping |
||
550 | 7 | * @return Address |
|
551 | 7 | * @throws Exception |
|
552 | 7 | */ |
|
553 | protected function getAddress($type) |
||
575 | |||
576 | /** |
||
577 | * Check if the two addresses saved differ. |
||
578 | * |
||
579 | * @return boolean |
||
580 | */ |
||
581 | public function getAddressesDiffer() |
||
585 | |||
586 | /** |
||
587 | 1 | * Has this order been sent to the customer? |
|
588 | * (at "Sent" status). |
||
589 | * |
||
590 | * @return boolean |
||
591 | */ |
||
592 | public function IsSent() |
||
596 | |||
597 | /** |
||
598 | 1 | * Is this order currently being processed? |
|
599 | * (at "Sent" OR "Processing" status). |
||
600 | * |
||
601 | * @return boolean |
||
602 | */ |
||
603 | public function IsProcessing() |
||
607 | |||
608 | /** |
||
609 | * Return whether this Order has been paid for (Status == Paid) |
||
610 | 2 | * or Status == Processing, where it's been paid for, but is |
|
611 | * currently in a processing state. |
||
612 | * |
||
613 | * @return boolean |
||
614 | */ |
||
615 | 93 | public function IsPaid() |
|
619 | |||
620 | public function IsCart() |
||
624 | 76 | ||
625 | 76 | /** |
|
626 | * Create a unique reference identifier string for this order. |
||
627 | 76 | */ |
|
628 | 76 | public function generateReference() |
|
641 | |||
642 | /** |
||
643 | * Get the reference for this order, or fall back to order ID. |
||
644 | */ |
||
645 | public function getReference() |
||
649 | 96 | ||
650 | 76 | /** |
|
651 | 76 | * Force creating an order reference |
|
652 | */ |
||
653 | protected function onBeforeWrite() |
||
674 | |||
675 | /** |
||
676 | 6 | * Called from @see onBeforeWrite whenever status changes |
|
677 | * @param string $fromStatus status to transition away from |
||
678 | 6 | * @param string $toStatus target status |
|
679 | 2 | */ |
|
680 | 2 | protected function statusTransition($fromStatus, $toStatus) |
|
699 | 1 | ||
700 | 2 | /** |
|
701 | * delete attributes, statuslogs, and payments |
||
702 | 2 | */ |
|
703 | 1 | protected function onBeforeDelete() |
|
723 | |||
724 | public function onAfterWrite() |
||
748 | |||
749 | public function debug() |
||
770 | |||
771 | /** |
||
772 | * Provide i18n entities for the order class |
||
773 | * |
||
774 | * @return array |
||
775 | */ |
||
776 | public function provideI18nEntities() |
||
791 | } |
||
792 |
Too many fields generally indicate a class which does too much and does not follow the single responsibility principle.
We suggest taking a look at the “Code” section for further suggestions on how to fix this.