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) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * save edit status for speed's sake. |
||
| 123 | * |
||
| 124 | * @var bool |
||
| 125 | */ |
||
| 126 | protected $_canEdit = null; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * save view status for speed's sake. |
||
| 130 | * |
||
| 131 | * @var bool |
||
| 132 | */ |
||
| 133 | protected $_canView = null; |
||
| 134 | |||
| 135 | public function canCreate($member = null) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Standard SS method |
||
| 153 | * This is an important method. |
||
| 154 | * |
||
| 155 | * @param Member $member |
||
| 156 | * |
||
| 157 | * @return bool |
||
| 158 | **/ |
||
| 159 | public function canView($member = null) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Standard SS method |
||
| 187 | * This is an important method. |
||
| 188 | * |
||
| 189 | * @param Member $member |
||
| 190 | * |
||
| 191 | * @return bool |
||
| 192 | **/ |
||
| 193 | public function canEdit($member = null) |
||
| 220 | |||
| 221 | public function canDelete($member = null) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Determine which properties on the DataObject are |
||
| 236 | * searchable, and map them to their default {@link FormField} |
||
| 237 | * representations. Used for scaffolding a searchform for {@link ModelAdmin}. |
||
| 238 | * |
||
| 239 | * Some additional logic is included for switching field labels, based on |
||
| 240 | * how generic or specific the field type is. |
||
| 241 | * |
||
| 242 | * Used by {@link SearchContext}. |
||
| 243 | * |
||
| 244 | * @param array $_params |
||
| 245 | * 'fieldClasses': Associative array of field names as keys and FormField classes as values |
||
| 246 | * 'restrictFields': Numeric array of a field name whitelist |
||
| 247 | * |
||
| 248 | * @return FieldList |
||
| 249 | */ |
||
| 250 | public function scaffoldSearchFields($_params = null) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return FieldList |
||
| 261 | */ |
||
| 262 | protected function getEcommerceFields() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * put together a textfield for a postal code field. |
||
| 269 | * |
||
| 270 | * @param string $name - name of the field |
||
| 271 | * |
||
| 272 | * @return TextField |
||
| 273 | **/ |
||
| 274 | protected function getPostalCodeField($name) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * put together a dropdown for the region field. |
||
| 289 | * |
||
| 290 | * @param string $name - name of the field |
||
| 291 | * |
||
| 292 | * @return DropdownField |
||
| 293 | **/ |
||
| 294 | protected function getRegionField($name, $freeTextName = '') |
||
| 324 | |||
| 325 | /** |
||
| 326 | * put together a dropdown for the country field. |
||
| 327 | * |
||
| 328 | * @param string $name - name of the field |
||
| 329 | * |
||
| 330 | * @return DropdownField |
||
| 331 | **/ |
||
| 332 | protected function getCountryField($name) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * makes selected fields into read only using the $this->readOnlyFields array. |
||
| 354 | * |
||
| 355 | * @param FieldList | Composite $fields |
||
| 356 | * |
||
| 357 | * @return FieldList |
||
| 358 | */ |
||
| 359 | protected function makeSelectedFieldsReadOnly($fields) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Saves region - both shipping and billing fields are saved here for convenience sake (only one actually gets saved) |
||
| 375 | * NOTE: do not call this method SetCountry as this has a special meaning! *. |
||
| 376 | * |
||
| 377 | * @param int - RegionID |
||
| 378 | **/ |
||
| 379 | public function SetRegionFields($regionID) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Saves country - both shipping and billing fields are saved here for convenience sake (only one actually gets saved) |
||
| 388 | * NOTE: do not call this method SetCountry as this has a special meaning! |
||
| 389 | * |
||
| 390 | * @param string - CountryCode - e.g. NZ |
||
| 391 | */ |
||
| 392 | public function SetCountryFields($countryCode) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Casted variable |
||
| 401 | * returns the full name of the person, e.g. "John Smith". |
||
| 402 | * |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | public function getFullName() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Casted variable |
||
| 421 | * returns the full strng of the record. |
||
| 422 | * |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | public function FullString() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * returns a string that can be used to find out if two addresses are the same. |
||
| 441 | * |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | public function comparisonString() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * returns the field prefix string for shipping addresses. |
||
| 464 | * |
||
| 465 | * @return string |
||
| 466 | **/ |
||
| 467 | protected function baseClassLinkingToOrder() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * returns the field prefix string for shipping addresses. |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | **/ |
||
| 481 | protected function fieldPrefix() |
||
| 489 | |||
| 490 | /** |
||
| 491 | *@todo: are there times when the Shipping rather than the Billing address should be linked? |
||
| 492 | * Copies the last address used by the member. |
||
| 493 | * |
||
| 494 | * @param object (Member) $member |
||
| 495 | * @param bool $write - should the address be written |
||
| 496 | * |
||
| 497 | * @return OrderAddress | ShippingAddress | BillingAddress |
||
| 498 | **/ |
||
| 499 | public function FillWithLastAddressFromMember(Member $member, $write = false) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * find the member associated with the current Order and address. |
||
| 538 | * |
||
| 539 | * @Note: this needs to be public to give DODS (extensions access to this) |
||
| 540 | * @todo: can wre write $this->Order() instead???? |
||
| 541 | * |
||
| 542 | * @return DataObject (Member) | Null |
||
| 543 | **/ |
||
| 544 | public function getMemberFromOrder() |
||
| 556 | |||
| 557 | /** |
||
| 558 | * make an address obsolete and include all the addresses that are identical. |
||
| 559 | * |
||
| 560 | * @param Member $member |
||
| 561 | */ |
||
| 562 | public function MakeObsolete(Member $member = null) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * standard SS method |
||
| 580 | * We "hackishly" ensure that the OrderID is set to the right value. |
||
| 581 | */ |
||
| 582 | public function onAfterWrite() |
||
| 595 | |||
| 596 | /** |
||
| 597 | * returns the link that can be used to remove (make Obsolete) an address. |
||
| 598 | * |
||
| 599 | * @return string |
||
| 600 | */ |
||
| 601 | public function RemoveLink() |
||
| 605 | |||
| 606 | /** |
||
| 607 | * converts an address into JSON. |
||
| 608 | * |
||
| 609 | * @return string (JSON) |
||
| 610 | */ |
||
| 611 | public function getJSONData() |
||
| 631 | |||
| 632 | /** |
||
| 633 | * returns the instance of EcommerceDBConfig. |
||
| 634 | * |
||
| 635 | * @return EcommerceDBConfig |
||
| 636 | **/ |
||
| 637 | public function EcomConfig() |
||
| 641 | |||
| 642 | /** |
||
| 643 | * standard SS Method |
||
| 644 | * saves the region code. |
||
| 645 | */ |
||
| 646 | public function onBeforeWrite() |
||
| 659 | |||
| 660 | public function debug() |
||
| 664 | } |
||
| 665 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.