Complex classes like EcommerceCountry 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 EcommerceCountry, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class EcommerceCountry extends DataObject implements EditableEcommerceObject |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * what variables are accessible through http://mysite.com/api/ecommerce/v1/EcommerceCountry/. |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | private static $api_access = array( |
||
|
|
|||
| 22 | 'view' => array( |
||
| 23 | 'Code', |
||
| 24 | 'Name', |
||
| 25 | ) |
||
| 26 | ); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Standard SS Variable. |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | **/ |
||
| 33 | private static $db = array( |
||
| 34 | 'Code' => 'Varchar(20)', |
||
| 35 | 'Name' => 'Varchar(200)', |
||
| 36 | 'DoNotAllowSales' => 'Boolean', |
||
| 37 | ); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Standard SS Variable. |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | **/ |
||
| 44 | private static $has_many = array( |
||
| 45 | 'Regions' => 'EcommerceRegion', |
||
| 46 | ); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * standard SS variable. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private static $summary_fields = array( |
||
| 54 | 'Code' => 'Code', |
||
| 55 | 'Name' => 'Name', |
||
| 56 | 'AllowSalesNice' => 'Allow Sales', |
||
| 57 | ); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * standard SS variable. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private static $casting = array( |
||
| 65 | 'AllowSales' => 'Boolean', |
||
| 66 | 'AllowSalesNice' => 'Varchar', |
||
| 67 | ); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * STANDARD SILVERSTRIPE STUFF. |
||
| 71 | * |
||
| 72 | * @todo: how to translate this? |
||
| 73 | **/ |
||
| 74 | private static $searchable_fields = array( |
||
| 75 | 'Code' => 'PartialMatchFilter', |
||
| 76 | 'Name' => 'PartialMatchFilter', |
||
| 77 | 'DoNotAllowSales' => array( |
||
| 78 | 'title' => 'Sales are prohibited', |
||
| 79 | ), |
||
| 80 | ); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Standard SS Variable. |
||
| 84 | * |
||
| 85 | * @var array |
||
| 86 | **/ |
||
| 87 | private static $indexes = array( |
||
| 88 | 'DoNotAllowSales' => true, |
||
| 89 | 'Name' => true, |
||
| 90 | 'Code' => true |
||
| 91 | ); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Standard SS Variable. |
||
| 95 | * |
||
| 96 | * @var string |
||
| 97 | **/ |
||
| 98 | private static $default_sort = [ |
||
| 99 | 'DoNotAllowSales' => 'ASC', |
||
| 100 | 'Name' => 'ASC', |
||
| 101 | 'ID' => 'ASC' |
||
| 102 | ]; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Standard SS Variable. |
||
| 106 | * |
||
| 107 | * @var string |
||
| 108 | **/ |
||
| 109 | private static $singular_name = 'Country'; |
||
| 110 | public function i18n_singular_name() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Standard SS Variable. |
||
| 117 | * |
||
| 118 | * @var string |
||
| 119 | **/ |
||
| 120 | private static $plural_name = 'Countries'; |
||
| 121 | public function i18n_plural_name() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Standard SS variable. |
||
| 128 | * |
||
| 129 | * @var string |
||
| 130 | */ |
||
| 131 | private static $description = 'A country.'; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Standard SS Method. |
||
| 135 | * |
||
| 136 | * @param Member $member |
||
| 137 | * |
||
| 138 | * @var bool |
||
| 139 | */ |
||
| 140 | public function canCreate($member = null) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Standard SS Method. |
||
| 159 | * |
||
| 160 | * @param Member $member |
||
| 161 | * |
||
| 162 | * @var bool |
||
| 163 | */ |
||
| 164 | public function canView($member = null) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Standard SS Method. |
||
| 182 | * |
||
| 183 | * @param Member $member |
||
| 184 | * |
||
| 185 | * @var bool |
||
| 186 | */ |
||
| 187 | public function canEdit($member = null) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Standard SS method. |
||
| 205 | * |
||
| 206 | * @param Member $member |
||
| 207 | * |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | public function canDelete($member = null) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * returns the country based on the Visitor Country Provider. |
||
| 235 | * this is some sort of IP recogniser system (e.g. Geoip Class). |
||
| 236 | * |
||
| 237 | * @return string (country code) |
||
| 238 | **/ |
||
| 239 | public static function get_country_from_ip() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * returns the country based on the Visitor Country Provider. |
||
| 252 | * this is some sort of IP recogniser system (e.g. Geoip Class). |
||
| 253 | * |
||
| 254 | * @return string (country code) |
||
| 255 | **/ |
||
| 256 | public static function get_ip() |
||
| 266 | |||
| 267 | private static $_countries_from_db_cache = array(); |
||
| 268 | |||
| 269 | /** |
||
| 270 | * e.g. |
||
| 271 | * "NZ" => "New Zealand" |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | public static function get_country_dropdown($showAllCountries = true, $addEmptyString = false, $useIDNotCode = false) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * This function exists as a shortcut. |
||
| 309 | * If there is only ONE allowed country code |
||
| 310 | * then a lot of checking of countries can be avoided. |
||
| 311 | * |
||
| 312 | * @return string | null - countrycode |
||
| 313 | **/ |
||
| 314 | public static function get_fixed_country_code() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @alias for EcommerceCountry::find_title |
||
| 326 | * |
||
| 327 | * @param string $code |
||
| 328 | * We have this as this is the same as Geoip |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | public static function countryCode2name($code) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * returns the country name from a code. |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | **/ |
||
| 342 | public static function find_title($code) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Memory for the order's country. |
||
| 365 | * |
||
| 366 | * @var array |
||
| 367 | */ |
||
| 368 | private static $_country_cache = array(); |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param int (optional) $orderID |
||
| 372 | */ |
||
| 373 | public static function reset_get_country_cache($orderID = 0) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param int (optional) $orderID |
||
| 381 | */ |
||
| 382 | public static function get_country_cache($orderID = 0) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param string $countryCode |
||
| 391 | * @param int (optional) $orderID |
||
| 392 | */ |
||
| 393 | public static function set_country_cache($countryCode, $orderID = 0) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * This function works out the most likely country for the current order. |
||
| 401 | * |
||
| 402 | * @param bool (optional) $recalculate |
||
| 403 | * @param int (optional) $orderID |
||
| 404 | * |
||
| 405 | * @return string - Country Code - e.g. NZ |
||
| 406 | **/ |
||
| 407 | public static function get_country($recalculate = false, $orderID = 0) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * A bling guess at the best country! |
||
| 441 | * |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | public static function get_country_default() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @param int|string|EcommerceCountry |
||
| 463 | * |
||
| 464 | * @return EcommerceCountry|string|null |
||
| 465 | */ |
||
| 466 | public static function get_country_from_mixed_var($var, $asCode = false) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * This function works out the most likely country for the current order |
||
| 487 | * and returns the Country Object, if any. |
||
| 488 | * |
||
| 489 | * @param bool (optional) $recalculate |
||
| 490 | * @param string (optional) $countryCode |
||
| 491 | * |
||
| 492 | * @return EcommerceCountry | Null |
||
| 493 | **/ |
||
| 494 | public static function get_country_object($recalculate = false, $countryCode = null) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * maps like this |
||
| 508 | * NZ => 1 |
||
| 509 | * AU => 2 |
||
| 510 | * etc... |
||
| 511 | * @var array |
||
| 512 | */ |
||
| 513 | private static $_code_to_id_map = array(); |
||
| 514 | /** |
||
| 515 | * returns the ID of the country or 0. |
||
| 516 | * |
||
| 517 | * @param string (optional) $countryCode |
||
| 518 | * @param bool (optional) $recalculate |
||
| 519 | * |
||
| 520 | * @return int |
||
| 521 | **/ |
||
| 522 | public static function get_country_id($countryCode = null, $recalculate = false) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Memory for allow country to check. |
||
| 545 | * |
||
| 546 | * @var null | Boolean |
||
| 547 | */ |
||
| 548 | private static $_allow_sales_cache = array(); |
||
| 549 | |||
| 550 | public static function reset_allow_sales_cache() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Checks if we are allowed to sell to the current country. |
||
| 557 | * |
||
| 558 | * @param int (optional) $orderID |
||
| 559 | * |
||
| 560 | * @return bool |
||
| 561 | */ |
||
| 562 | public static function allow_sales($orderID = 0) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * returns an array of Codes => Names of all countries that can be used. |
||
| 585 | * Use "list_of_allowed_entries_for_dropdown" to get the list. |
||
| 586 | * |
||
| 587 | * @todo add caching |
||
| 588 | * |
||
| 589 | * @return array |
||
| 590 | **/ |
||
| 591 | protected static function get_default_array() |
||
| 609 | |||
| 610 | public function getCMSFields() |
||
| 629 | |||
| 630 | |||
| 631 | |||
| 632 | /** |
||
| 633 | * link to edit the record. |
||
| 634 | * |
||
| 635 | * @param string|null $action - e.g. edit |
||
| 636 | * |
||
| 637 | * @return string |
||
| 638 | */ |
||
| 639 | public function CMSEditLink($action = null) |
||
| 643 | |||
| 644 | /** |
||
| 645 | * standard SS method. |
||
| 646 | */ |
||
| 647 | public function requireDefaultRecords() |
||
| 655 | |||
| 656 | /** |
||
| 657 | * standard SS method |
||
| 658 | * cleans up codes. |
||
| 659 | */ |
||
| 660 | public function onBeforeWrite() |
||
| 666 | |||
| 667 | //DYNAMIC LIMITATIONS |
||
| 668 | |||
| 669 | /** |
||
| 670 | * these variables and methods allow to "dynamically limit the countries available, |
||
| 671 | * based on, for example: ordermodifiers, item selection, etc.... |
||
| 672 | * for example, if a person chooses delivery within Australasia (with modifier) - |
||
| 673 | * then you can limit the countries available to "Australasian" countries. |
||
| 674 | */ |
||
| 675 | |||
| 676 | /** |
||
| 677 | * List of countries that should be shown. |
||
| 678 | * |
||
| 679 | * @param array $a: should be country codes e.g. array("NZ", "NP", "AU"); |
||
| 680 | * |
||
| 681 | * @var array |
||
| 682 | */ |
||
| 683 | private static $for_current_order_only_show_countries = array(); |
||
| 684 | public static function get_for_current_order_only_show_countries() |
||
| 697 | |||
| 698 | /** |
||
| 699 | * List of countries that should NOT be shown. |
||
| 700 | * |
||
| 701 | * @param array $a: should be country codes e.g. array("NZ", "NP", "AU"); |
||
| 702 | * |
||
| 703 | * @var array |
||
| 704 | */ |
||
| 705 | private static $for_current_order_do_not_show_countries = array(); |
||
| 706 | public static function get_for_current_order_do_not_show_countries() |
||
| 715 | |||
| 716 | /** |
||
| 717 | * @var array |
||
| 718 | */ |
||
| 719 | private static $list_of_allowed_entries_for_dropdown_array = array(); |
||
| 720 | |||
| 721 | /** |
||
| 722 | * takes the defaultArray and limits it with "only show" and "do not show" value, relevant for the current order. |
||
| 723 | * |
||
| 724 | * @return array (Code, Title) |
||
| 725 | **/ |
||
| 726 | public static function list_of_allowed_entries_for_dropdown() |
||
| 751 | |||
| 752 | /** |
||
| 753 | * checks if a code is allowed. |
||
| 754 | * |
||
| 755 | * @param string $code - e.g. NZ, NSW, or CO |
||
| 756 | * |
||
| 757 | * @return bool |
||
| 758 | **/ |
||
| 759 | public static function code_allowed($code) |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Casted variable to show if sales are allowed to this country. |
||
| 766 | * |
||
| 767 | * @return bool |
||
| 768 | */ |
||
| 769 | public function AllowSales() |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Casted variable to show if sales are allowed to this country. |
||
| 784 | * |
||
| 785 | * @return string |
||
| 786 | */ |
||
| 787 | public function AllowSalesNice() |
||
| 799 | } |
||
| 800 |