Complex classes like OrderAddress 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 OrderAddress, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class OrderAddress extends DataObject implements EditableEcommerceObject |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * standard SS static definition. |
||
| 18 | */ |
||
| 19 | private static $singular_name = 'Order Address'; |
||
|
|
|||
| 20 | public function i18n_singular_name() |
||
| 24 | |||
| 25 | /** |
||
| 26 | * standard SS static definition. |
||
| 27 | */ |
||
| 28 | private static $plural_name = 'Order Addresses'; |
||
| 29 | public function i18n_plural_name() |
||
| 33 | |||
| 34 | /** |
||
| 35 | * standard SS static definition. |
||
| 36 | */ |
||
| 37 | private static $casting = array( |
||
| 38 | 'FullName' => 'Text', |
||
| 39 | 'FullString' => 'Text', |
||
| 40 | 'JSONData' => 'Text', |
||
| 41 | ); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * returns the id of the MAIN country field for template manipulation. |
||
| 45 | * Main means the one that is used as the primary one (e.g. for tax purposes). |
||
| 46 | * |
||
| 47 | * @see EcommerceConfig::get("OrderAddress", "use_shipping_address_for_main_region_and_country") |
||
| 48 | * |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | public static function get_country_field_ID() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * returns the id of the MAIN region field for template manipulation. |
||
| 62 | * Main means the one that is used as the primary one (e.g. for tax purposes). |
||
| 63 | * |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public static function get_region_field_ID() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * There might be times when a modifier needs to make an address field read-only. |
||
| 77 | * In that case, this is done here. |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $readOnlyFields = array(); |
||
| 82 | |||
| 83 | /** |
||
| 84 | * sets a field to readonly state |
||
| 85 | * we use this when modifiers have been set that require a field to be a certain value |
||
| 86 | * for example - a PostalCode field maybe set in the modifier. |
||
| 87 | * |
||
| 88 | * @param string $fieldName |
||
| 89 | */ |
||
| 90 | public function addReadOnlyField($fieldName) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * removes a field from the readonly state. |
||
| 97 | * |
||
| 98 | * @param string $fieldName |
||
| 99 | */ |
||
| 100 | public function removeReadOnlyField($fieldName) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * link to edit the record. |
||
| 107 | * |
||
| 108 | * @param string | Null $action - e.g. edit |
||
| 109 | * |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public function CMSEditLink($action = null) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * save edit status for speed's sake. |
||
| 119 | * |
||
| 120 | * @var bool |
||
| 121 | */ |
||
| 122 | protected $_canEdit = null; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * save view status for speed's sake. |
||
| 126 | * |
||
| 127 | * @var bool |
||
| 128 | */ |
||
| 129 | protected $_canView = null; |
||
| 130 | |||
| 131 | public function canCreate($member = null) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Standard SS method |
||
| 149 | * This is an important method. |
||
| 150 | * |
||
| 151 | * @param Member $member |
||
| 152 | * |
||
| 153 | * @return bool |
||
| 154 | **/ |
||
| 155 | public function canView($member = null) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Standard SS method |
||
| 183 | * This is an important method. |
||
| 184 | * |
||
| 185 | * @param Member $member |
||
| 186 | * |
||
| 187 | * @return bool |
||
| 188 | **/ |
||
| 189 | public function canEdit($member = null) |
||
| 216 | |||
| 217 | public function canDelete($member = null) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Determine which properties on the DataObject are |
||
| 232 | * searchable, and map them to their default {@link FormField} |
||
| 233 | * representations. Used for scaffolding a searchform for {@link ModelAdmin}. |
||
| 234 | * |
||
| 235 | * Some additional logic is included for switching field labels, based on |
||
| 236 | * how generic or specific the field type is. |
||
| 237 | * |
||
| 238 | * Used by {@link SearchContext}. |
||
| 239 | * |
||
| 240 | * @param array $_params |
||
| 241 | * 'fieldClasses': Associative array of field names as keys and FormField classes as values |
||
| 242 | * 'restrictFields': Numeric array of a field name whitelist |
||
| 243 | * |
||
| 244 | * @return FieldList |
||
| 245 | */ |
||
| 246 | public function scaffoldSearchFields($_params = null) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @return FieldList |
||
| 257 | */ |
||
| 258 | protected function getEcommerceFields() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * put together a textfield for a postal code field. |
||
| 265 | * |
||
| 266 | * @param string $name - name of the field |
||
| 267 | * |
||
| 268 | * @return TextField |
||
| 269 | **/ |
||
| 270 | protected function getPostalCodeField($name) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * put together a dropdown for the region field. |
||
| 285 | * |
||
| 286 | * @param string $name - name of the field |
||
| 287 | * |
||
| 288 | * @return DropdownField |
||
| 289 | **/ |
||
| 290 | protected function getRegionField($name, $freeTextName = '') |
||
| 320 | |||
| 321 | /** |
||
| 322 | * put together a dropdown for the country field. |
||
| 323 | * |
||
| 324 | * @param string $name - name of the field |
||
| 325 | * |
||
| 326 | * @return DropdownField |
||
| 327 | **/ |
||
| 328 | protected function getCountryField($name) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * makes selected fields into read only using the $this->readOnlyFields array. |
||
| 363 | * |
||
| 364 | * @param FieldList | Composite $fields |
||
| 365 | * |
||
| 366 | * @return FieldList |
||
| 367 | */ |
||
| 368 | protected function makeSelectedFieldsReadOnly($fields) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Saves region - both shipping and billing fields are saved here for convenience sake (only one actually gets saved) |
||
| 384 | * NOTE: do not call this method SetCountry as this has a special meaning! *. |
||
| 385 | * |
||
| 386 | * @param int - RegionID |
||
| 387 | **/ |
||
| 388 | public function SetRegionFields($regionID) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Saves country - both shipping and billing fields are saved here for convenience sake (only one actually gets saved) |
||
| 397 | * NOTE: do not call this method SetCountry as this has a special meaning! |
||
| 398 | * |
||
| 399 | * @param string - CountryCode - e.g. NZ |
||
| 400 | */ |
||
| 401 | public function SetCountryFields($countryCode) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Casted variable |
||
| 410 | * returns the full name of the person, e.g. "John Smith". |
||
| 411 | * |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | public function getFullName() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Casted variable |
||
| 430 | * returns the full strng of the record. |
||
| 431 | * |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | public function FullString() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * returns a string that can be used to find out if two addresses are the same. |
||
| 450 | * |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | public function comparisonString() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * returns the field prefix string for shipping addresses. |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | **/ |
||
| 476 | protected function baseClassLinkingToOrder() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * returns the field prefix string for shipping addresses. |
||
| 487 | * |
||
| 488 | * @return string |
||
| 489 | **/ |
||
| 490 | protected function fieldPrefix() |
||
| 498 | |||
| 499 | /** |
||
| 500 | *@todo: are there times when the Shipping rather than the Billing address should be linked? |
||
| 501 | * Copies the last address used by the member. |
||
| 502 | * |
||
| 503 | * @param object (Member) $member |
||
| 504 | * @param bool $write - should the address be written |
||
| 505 | * |
||
| 506 | * @return OrderAddress | ShippingAddress | BillingAddress |
||
| 507 | **/ |
||
| 508 | public function FillWithLastAddressFromMember(Member $member, $write = false) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * find the member associated with the current Order and address. |
||
| 547 | * |
||
| 548 | * @Note: this needs to be public to give DODS (extensions access to this) |
||
| 549 | * @todo: can wre write $this->Order() instead???? |
||
| 550 | * |
||
| 551 | * @return DataObject (Member) | Null |
||
| 552 | **/ |
||
| 553 | public function getMemberFromOrder() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * make an address obsolete and include all the addresses that are identical. |
||
| 568 | * |
||
| 569 | * @param Member $member |
||
| 570 | */ |
||
| 571 | public function MakeObsolete(Member $member = null) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * standard SS method |
||
| 589 | * We "hackishly" ensure that the OrderID is set to the right value. |
||
| 590 | */ |
||
| 591 | public function onAfterWrite() |
||
| 606 | |||
| 607 | /** |
||
| 608 | * returns the link that can be used to remove (make Obsolete) an address. |
||
| 609 | * |
||
| 610 | * @return string |
||
| 611 | */ |
||
| 612 | public function RemoveLink() |
||
| 616 | |||
| 617 | /** |
||
| 618 | * converts an address into JSON. |
||
| 619 | * |
||
| 620 | * @return string (JSON) |
||
| 621 | */ |
||
| 622 | public function getJSONData() |
||
| 642 | |||
| 643 | /** |
||
| 644 | * returns the instance of EcommerceDBConfig. |
||
| 645 | * |
||
| 646 | * @return EcommerceDBConfig |
||
| 647 | **/ |
||
| 648 | public function EcomConfig() |
||
| 652 | |||
| 653 | /** |
||
| 654 | * standard SS Method |
||
| 655 | * saves the region code. |
||
| 656 | */ |
||
| 657 | public function onBeforeWrite() |
||
| 670 | |||
| 671 | public function debug() |
||
| 675 | } |
||
| 676 |