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 |
||
| 9 | class Order extends DataObject |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Status codes and what they mean: |
||
| 13 | * |
||
| 14 | * Unpaid (default): Order created but no successful payment by customer yet |
||
| 15 | * Query: Order not being processed yet (customer has a query, or could be out of stock) |
||
| 16 | * Paid: Order successfully paid for by customer |
||
| 17 | * Processing: Order paid for, package is currently being processed before shipping to customer |
||
| 18 | * Sent: Order paid for, processed for shipping, and now sent to the customer |
||
| 19 | * Complete: Order completed (paid and shipped). Customer assumed to have received their goods |
||
| 20 | * AdminCancelled: Order cancelled by the administrator |
||
| 21 | * MemberCancelled: Order cancelled by the customer (Member) |
||
| 22 | */ |
||
| 23 | private static $db = array( |
||
| 24 | 'Total' => 'Currency', |
||
| 25 | 'Reference' => 'Varchar', //allow for customised order numbering schemes |
||
| 26 | //status |
||
| 27 | 'Placed' => "SS_Datetime", //date the order was placed (went from Cart to Order) |
||
| 28 | 'Paid' => 'SS_Datetime', //no outstanding payment left |
||
| 29 | 'ReceiptSent' => 'SS_Datetime', //receipt emailed to customer |
||
| 30 | 'Printed' => 'SS_Datetime', |
||
| 31 | 'Dispatched' => 'SS_Datetime', //products have been sent to customer |
||
| 32 | 'Status' => "Enum('Unpaid,Paid,Processing,Sent,Complete,AdminCancelled,MemberCancelled,Cart','Cart')", |
||
| 33 | //customer (for guest orders) |
||
| 34 | 'FirstName' => 'Varchar', |
||
| 35 | 'Surname' => 'Varchar', |
||
| 36 | 'Email' => 'Varchar', |
||
| 37 | 'Notes' => 'Text', |
||
| 38 | 'IPAddress' => 'Varchar(15)', |
||
| 39 | //separate shipping |
||
| 40 | 'SeparateBillingAddress' => 'Boolean', |
||
| 41 | // keep track of customer locale |
||
| 42 | 'Locale' => 'DBLocale', |
||
| 43 | ); |
||
| 44 | |||
| 45 | private static $has_one = array( |
||
| 46 | 'Member' => 'Member', |
||
| 47 | 'ShippingAddress' => 'Address', |
||
| 48 | 'BillingAddress' => 'Address', |
||
| 49 | ); |
||
| 50 | |||
| 51 | private static $has_many = array( |
||
| 52 | 'Items' => 'OrderItem', |
||
| 53 | 'Modifiers' => 'OrderModifier', |
||
| 54 | 'OrderStatusLogs' => 'OrderStatusLog', |
||
| 55 | ); |
||
| 56 | |||
| 57 | private static $defaults = array( |
||
| 58 | 'Status' => 'Cart', |
||
| 59 | ); |
||
| 60 | |||
| 61 | private static $casting = array( |
||
| 62 | 'FullBillingAddress' => 'Text', |
||
| 63 | 'FullShippingAddress' => 'Text', |
||
| 64 | 'Total' => 'Currency', |
||
| 65 | 'SubTotal' => 'Currency', |
||
| 66 | 'TotalPaid' => 'Currency', |
||
| 67 | 'Shipping' => 'Currency', |
||
| 68 | 'TotalOutstanding' => 'Currency', |
||
| 69 | ); |
||
| 70 | |||
| 71 | private static $summary_fields = array( |
||
| 72 | 'Reference' => 'Order No', |
||
| 73 | 'Placed' => 'Date', |
||
| 74 | 'Name' => 'Customer', |
||
| 75 | 'LatestEmail' => 'Email', |
||
| 76 | 'Total' => 'Total', |
||
| 77 | 'Status' => 'Status', |
||
| 78 | ); |
||
| 79 | |||
| 80 | private static $searchable_fields = array( |
||
| 81 | 'Reference' => array(), |
||
| 82 | 'FirstName' => array( |
||
| 83 | 'title' => 'Customer Name', |
||
| 84 | ), |
||
| 85 | 'Email' => array( |
||
| 86 | 'title' => 'Customer Email', |
||
| 87 | ), |
||
| 88 | 'Status' => array( |
||
| 89 | 'filter' => 'ExactMatchFilter', |
||
| 90 | 'field' => 'CheckboxSetField', |
||
| 91 | ), |
||
| 92 | ); |
||
| 93 | |||
| 94 | private static $singular_name = "Order"; |
||
| 95 | |||
| 96 | private static $plural_name = "Orders"; |
||
| 97 | |||
| 98 | private static $default_sort = "\"Placed\" DESC, \"Created\" DESC"; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Statuses for orders that have been placed. |
||
| 102 | */ |
||
| 103 | private static $placed_status = array( |
||
| 104 | 'Paid', |
||
| 105 | 'Unpaid', |
||
| 106 | 'Processing', |
||
| 107 | 'Sent', |
||
| 108 | 'Complete', |
||
| 109 | 'MemberCancelled', |
||
| 110 | 'AdminCancelled', |
||
| 111 | ); |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Statuses for which an order can be paid for |
||
| 115 | */ |
||
| 116 | private static $payable_status = array( |
||
| 117 | 'Cart', |
||
| 118 | 'Unpaid', |
||
| 119 | 'Processing', |
||
| 120 | 'Sent', |
||
| 121 | ); |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Statuses that shouldn't show in user account. |
||
| 125 | */ |
||
| 126 | private static $hidden_status = array('Cart'); |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Flags to determine when an order can be cancelled. |
||
| 130 | */ |
||
| 131 | private static $cancel_before_payment = true; |
||
| 132 | |||
| 133 | private static $cancel_before_processing = false; |
||
| 134 | |||
| 135 | private static $cancel_before_sending = false; |
||
| 136 | |||
| 137 | private static $cancel_after_sending = false; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Place an order before payment processing begins |
||
| 141 | * |
||
| 142 | * @var boolean |
||
| 143 | */ |
||
| 144 | private static $place_before_payment = false; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Modifiers represent the additional charges or |
||
| 148 | * deductions associated to an order, such as |
||
| 149 | * shipping, taxes, vouchers etc. |
||
| 150 | */ |
||
| 151 | private static $modifiers = array(); |
||
| 152 | |||
| 153 | private static $rounding_precision = 2; |
||
| 154 | |||
| 155 | private static $reference_id_padding = 5; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @var boolean Will allow completion of orders with GrandTotal=0, |
||
| 159 | * which could be the case for orders paid with loyalty points or vouchers. |
||
| 160 | * Will send the "Paid" date on the order, even though no actual payment was taken. |
||
| 161 | * Will trigger the payment related extension points: |
||
| 162 | * Order->onPayment, OrderItem->onPayment, Order->onPaid. |
||
| 163 | */ |
||
| 164 | private static $allow_zero_order_total = false; |
||
| 165 | |||
| 166 | public static function get_order_status_options() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Create CMS fields for cms viewing and editing orders |
||
| 173 | */ |
||
| 174 | public function getCMSFields() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Adjust scafolded search context |
||
| 201 | * |
||
| 202 | * @return SearchContext the updated search context |
||
| 203 | */ |
||
| 204 | public function getDefaultSearchContext() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Hack for swapping out relation list with OrderItemList |
||
| 240 | */ |
||
| 241 | 32 | public function getComponents($componentName, $filter = "", $sort = "", $join = "", $limit = null) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Returns the subtotal of the items for this order. |
||
| 258 | */ |
||
| 259 | 12 | public function SubTotal() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Calculate the total |
||
| 270 | * |
||
| 271 | * @return the final total |
||
| 272 | */ |
||
| 273 | 8 | public function calculate() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * This is needed to maintain backwards compatiability with |
||
| 284 | * some subsystems using modifiers. eg discounts |
||
| 285 | */ |
||
| 286 | public function getModifier($className, $forcecreate = false) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Enforce rounding precision when setting total |
||
| 294 | */ |
||
| 295 | 62 | public function setTotal($val) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Get final value of order. |
||
| 302 | * Retrieves value from DataObject's record array. |
||
| 303 | */ |
||
| 304 | 14 | public function Total() |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Alias for Total. |
||
| 311 | */ |
||
| 312 | 12 | public function GrandTotal() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Calculate how much is left to be paid on the order. |
||
| 319 | * Enforces rounding precision. |
||
| 320 | */ |
||
| 321 | 10 | public function TotalOutstanding() |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Get the link for finishing order processing. |
||
| 331 | */ |
||
| 332 | 2 | public function Link() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Returns TRUE if the order can be cancelled |
||
| 342 | * PRECONDITION: Order is in the DB. |
||
| 343 | * |
||
| 344 | * @return boolean |
||
| 345 | */ |
||
| 346 | 1 | public function canCancel() |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Check if an order can be paid for. |
||
| 364 | * |
||
| 365 | * @return boolean |
||
| 366 | */ |
||
| 367 | 2 | public function canPay($member = null) |
|
| 377 | |||
| 378 | /* |
||
| 379 | * Prevent deleting orders. |
||
| 380 | * @return boolean |
||
| 381 | */ |
||
| 382 | public function canDelete($member = null) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Check if an order can be viewed. |
||
| 389 | * |
||
| 390 | * @return boolean |
||
| 391 | */ |
||
| 392 | public function canView($member = null) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Check if an order can be edited. |
||
| 399 | * |
||
| 400 | * @return boolean |
||
| 401 | */ |
||
| 402 | public function canEdit($member = null) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Prevent standard creation of orders. |
||
| 409 | * |
||
| 410 | * @return boolean |
||
| 411 | */ |
||
| 412 | public function canCreate($member = null, $context = array()) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Return the currency of this order. |
||
| 419 | * Note: this is a fixed value across the entire site. |
||
| 420 | * |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | 3 | public function Currency() |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Get the latest email for this order. |
||
| 430 | */ |
||
| 431 | 3 | public function getLatestEmail() |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Gets the name of the customer. |
||
| 441 | */ |
||
| 442 | public function getName() |
||
| 448 | |||
| 449 | public function getTitle() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Get shipping address, or member default shipping address. |
||
| 456 | */ |
||
| 457 | 1 | public function getShippingAddress() |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Get billing address, if marked to use seperate address, otherwise use shipping address, |
||
| 464 | * or the member default billing address. |
||
| 465 | */ |
||
| 466 | public function getBillingAddress() |
||
| 474 | 1 | ||
| 475 | /** |
||
| 476 | * @param string $type - Billing or Shipping |
||
| 477 | * @return Address |
||
| 478 | * @throws Exception |
||
| 479 | */ |
||
| 480 | protected function getAddress($type) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Check if the two addresses saved differ. |
||
| 498 | * |
||
| 499 | 1 | * @return boolean |
|
| 500 | */ |
||
| 501 | 1 | public function getAddressesDiffer() |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Has this order been sent to the customer? |
||
| 508 | * (at "Sent" status). |
||
| 509 | * |
||
| 510 | 1 | * @return boolean |
|
| 511 | */ |
||
| 512 | 1 | public function IsSent() |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Is this order currently being processed? |
||
| 519 | * (at "Sent" OR "Processing" status). |
||
| 520 | * |
||
| 521 | * @return boolean |
||
| 522 | 1 | */ |
|
| 523 | public function IsProcessing() |
||
| 527 | 80 | ||
| 528 | /** |
||
| 529 | 80 | * Return whether this Order has been paid for (Status == Paid) |
|
| 530 | * or Status == Processing, where it's been paid for, but is |
||
| 531 | * currently in a processing state. |
||
| 532 | * |
||
| 533 | * @return boolean |
||
| 534 | */ |
||
| 535 | 63 | public function IsPaid() |
|
| 539 | 63 | ||
| 540 | public function IsCart() |
||
| 544 | 63 | ||
| 545 | 63 | /** |
|
| 546 | 63 | * Create a unique reference identifier string for this order. |
|
| 547 | 63 | */ |
|
| 548 | public function generateReference() |
||
| 561 | |||
| 562 | 84 | /** |
|
| 563 | 84 | * Get the reference for this order, or fall back to order ID. |
|
| 564 | 63 | */ |
|
| 565 | 63 | public function getReference() |
|
| 569 | 84 | ||
| 570 | 84 | /** |
|
| 571 | 84 | * Force creating an order reference |
|
| 572 | 84 | */ |
|
| 573 | public function onBeforeWrite() |
||
| 586 | |||
| 587 | /** |
||
| 588 | * delete attributes, statuslogs, and payments |
||
| 589 | */ |
||
| 590 | public function onBeforeDelete() |
||
| 598 | |||
| 599 | public function debug() |
||
| 620 | } |
||
| 621 |
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.