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 $indexes = array( |
||
| 54 | 'Code' => true, |
||
| 55 | 'DoNotAllowSales' => true, |
||
| 56 | ); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * standard SS variable. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | private static $summary_fields = array( |
||
| 64 | 'Code' => 'Code', |
||
| 65 | 'Name' => 'Name', |
||
| 66 | 'AllowSalesNice' => 'Allow Sales', |
||
| 67 | ); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * standard SS variable. |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private static $casting = array( |
||
| 75 | 'AllowSales' => 'Boolean', |
||
| 76 | 'AllowSalesNice' => 'Varchar', |
||
| 77 | ); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * STANDARD SILVERSTRIPE STUFF. |
||
| 81 | * |
||
| 82 | * @todo: how to translate this? |
||
| 83 | **/ |
||
| 84 | private static $searchable_fields = array( |
||
| 85 | 'Code' => 'PartialMatchFilter', |
||
| 86 | 'Name' => 'PartialMatchFilter', |
||
| 87 | 'DoNotAllowSales' => array( |
||
| 88 | 'title' => 'Sales are prohibited', |
||
| 89 | ), |
||
| 90 | ); |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Standard SS Variable. |
||
| 94 | * |
||
| 95 | * @var string |
||
| 96 | **/ |
||
| 97 | private static $default_sort = '"DoNotAllowSales" ASC, "Name" ASC'; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Standard SS Variable. |
||
| 101 | * |
||
| 102 | * @var string |
||
| 103 | **/ |
||
| 104 | private static $singular_name = 'Country'; |
||
| 105 | public function i18n_singular_name() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Standard SS Variable. |
||
| 112 | * |
||
| 113 | * @var string |
||
| 114 | **/ |
||
| 115 | private static $plural_name = 'Countries'; |
||
| 116 | public function i18n_plural_name() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Standard SS variable. |
||
| 123 | * |
||
| 124 | * @var string |
||
| 125 | */ |
||
| 126 | private static $description = 'A country.'; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Standard SS Method. |
||
| 130 | * |
||
| 131 | * @param Member $member |
||
| 132 | * |
||
| 133 | * @var bool |
||
| 134 | */ |
||
| 135 | public function canCreate($member = null) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Standard SS Method. |
||
| 150 | * |
||
| 151 | * @param Member $member |
||
| 152 | * |
||
| 153 | * @var bool |
||
| 154 | */ |
||
| 155 | public function canView($member = null) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Standard SS Method. |
||
| 173 | * |
||
| 174 | * @param Member $member |
||
| 175 | * |
||
| 176 | * @var bool |
||
| 177 | */ |
||
| 178 | public function canEdit($member = null) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Standard SS method. |
||
| 196 | * |
||
| 197 | * @param Member $member |
||
| 198 | * |
||
| 199 | * @return bool |
||
| 200 | */ |
||
| 201 | public function canDelete($member = null) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * returns the country based on the Visitor Country Provider. |
||
| 226 | * this is some sort of IP recogniser system (e.g. Geoip Class). |
||
| 227 | * |
||
| 228 | * @return string (country code) |
||
| 229 | **/ |
||
| 230 | public static function get_country_from_ip() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * returns the country based on the Visitor Country Provider. |
||
| 243 | * this is some sort of IP recogniser system (e.g. Geoip Class). |
||
| 244 | * |
||
| 245 | * @return string (country code) |
||
| 246 | **/ |
||
| 247 | public static function get_ip() |
||
| 257 | |||
| 258 | private static $_countries_from_db_cache = array(); |
||
| 259 | |||
| 260 | /** |
||
| 261 | * e.g. |
||
| 262 | * "NZ" => "New Zealand" |
||
| 263 | * @return array |
||
| 264 | */ |
||
| 265 | public static function get_country_dropdown($showAllCountries = true, $addEmptyString = false, $useIDNotCode = false) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * This function exists as a shortcut. |
||
| 300 | * If there is only ONE allowed country code |
||
| 301 | * then a lot of checking of countries can be avoided. |
||
| 302 | * |
||
| 303 | * @return string | null - countrycode |
||
| 304 | **/ |
||
| 305 | public static function get_fixed_country_code() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @alias for EcommerceCountry::find_title |
||
| 317 | * |
||
| 318 | * @param string $code |
||
| 319 | * We have this as this is the same as Geoip |
||
| 320 | * |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | public static function countryCode2name($code) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * returns the country name from a code. |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | **/ |
||
| 333 | public static function find_title($code) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Memory for the order's country. |
||
| 353 | * |
||
| 354 | * @var array |
||
| 355 | */ |
||
| 356 | private static $_country_cache = array(); |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param int (optional) $orderID |
||
| 360 | */ |
||
| 361 | public static function reset_get_country_cache($orderID = 0) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param int (optional) $orderID |
||
| 369 | */ |
||
| 370 | public static function get_country_cache($orderID = 0) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param string $countryCode |
||
| 379 | * @param int (optional) $orderID |
||
| 380 | */ |
||
| 381 | public static function set_country_cache($countryCode, $orderID = 0) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * This function works out the most likely country for the current order. |
||
| 389 | * |
||
| 390 | * @param bool (optional) $recalculate |
||
| 391 | * @param int (optional) $orderID |
||
| 392 | * |
||
| 393 | * @return string - Country Code - e.g. NZ |
||
| 394 | **/ |
||
| 395 | public static function get_country($recalculate = false, $orderID = 0) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * A bling guess at the best country! |
||
| 425 | * |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | public static function get_country_default() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * This function works out the most likely country for the current order |
||
| 447 | * and returns the Country Object, if any. |
||
| 448 | * |
||
| 449 | * @param bool (optional) $recalculate |
||
| 450 | * @param string (optional) $countryCode |
||
| 451 | * |
||
| 452 | * @return EcommerceCountry | Null |
||
| 453 | **/ |
||
| 454 | public static function get_country_object($recalculate = false, $countryCode = null) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * maps like this |
||
| 465 | * NZ => 1 |
||
| 466 | * AU => 2 |
||
| 467 | * etc... |
||
| 468 | * @var array |
||
| 469 | */ |
||
| 470 | private static $_code_to_id_map = array(); |
||
| 471 | /** |
||
| 472 | * returns the ID of the country or 0. |
||
| 473 | * |
||
| 474 | * @param string (optional) $countryCode |
||
| 475 | * @param bool (optional) $recalculate |
||
| 476 | * |
||
| 477 | * @return int |
||
| 478 | **/ |
||
| 479 | public static function get_country_id($countryCode = null, $recalculate = false) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Memory for allow country to check. |
||
| 501 | * |
||
| 502 | * @var null | Boolean |
||
| 503 | */ |
||
| 504 | private static $_allow_sales_cache = array(); |
||
| 505 | |||
| 506 | public static function reset_allow_sales_cache() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Checks if we are allowed to sell to the current country. |
||
| 513 | * |
||
| 514 | * @param int (optional) $orderID |
||
| 515 | * |
||
| 516 | * @return bool |
||
| 517 | */ |
||
| 518 | public static function allow_sales($orderID = 0) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * returns an array of Codes => Names of all countries that can be used. |
||
| 541 | * Use "list_of_allowed_entries_for_dropdown" to get the list. |
||
| 542 | * |
||
| 543 | * @todo add caching |
||
| 544 | * |
||
| 545 | * @return array |
||
| 546 | **/ |
||
| 547 | protected static function get_default_array() |
||
| 565 | |||
| 566 | public function getCMSFields() |
||
| 582 | |||
| 583 | /** |
||
| 584 | * link to edit the record. |
||
| 585 | * |
||
| 586 | * @param string | Null $action - e.g. edit |
||
| 587 | * |
||
| 588 | * @return string |
||
| 589 | */ |
||
| 590 | public function CMSEditLink($action = null) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * standard SS method. |
||
| 601 | */ |
||
| 602 | public function requireDefaultRecords() |
||
| 610 | |||
| 611 | /** |
||
| 612 | * standard SS method |
||
| 613 | * cleans up codes. |
||
| 614 | */ |
||
| 615 | public function onBeforeWrite() |
||
| 621 | |||
| 622 | //DYNAMIC LIMITATIONS |
||
| 623 | |||
| 624 | /** |
||
| 625 | * these variables and methods allow to "dynamically limit the countries available, |
||
| 626 | * based on, for example: ordermodifiers, item selection, etc.... |
||
| 627 | * for example, if a person chooses delivery within Australasia (with modifier) - |
||
| 628 | * then you can limit the countries available to "Australasian" countries. |
||
| 629 | */ |
||
| 630 | |||
| 631 | /** |
||
| 632 | * List of countries that should be shown. |
||
| 633 | * |
||
| 634 | * @param array $a: should be country codes e.g. array("NZ", "NP", "AU"); |
||
| 635 | * |
||
| 636 | * @var array |
||
| 637 | */ |
||
| 638 | private static $for_current_order_only_show_countries = array(); |
||
| 639 | public static function get_for_current_order_only_show_countries() |
||
| 652 | |||
| 653 | /** |
||
| 654 | * List of countries that should NOT be shown. |
||
| 655 | * |
||
| 656 | * @param array $a: should be country codes e.g. array("NZ", "NP", "AU"); |
||
| 657 | * |
||
| 658 | * @var array |
||
| 659 | */ |
||
| 660 | private static $for_current_order_do_not_show_countries = array(); |
||
| 661 | public static function get_for_current_order_do_not_show_countries() |
||
| 670 | |||
| 671 | /** |
||
| 672 | * @var array |
||
| 673 | */ |
||
| 674 | private static $list_of_allowed_entries_for_dropdown_array = array(); |
||
| 675 | |||
| 676 | /** |
||
| 677 | * takes the defaultArray and limits it with "only show" and "do not show" value, relevant for the current order. |
||
| 678 | * |
||
| 679 | * @return array (Code, Title) |
||
| 680 | **/ |
||
| 681 | public static function list_of_allowed_entries_for_dropdown() |
||
| 706 | |||
| 707 | /** |
||
| 708 | * checks if a code is allowed. |
||
| 709 | * |
||
| 710 | * @param string $code - e.g. NZ, NSW, or CO |
||
| 711 | * |
||
| 712 | * @return bool |
||
| 713 | **/ |
||
| 714 | public static function code_allowed($code) |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Casted variable to show if sales are allowed to this country. |
||
| 721 | * |
||
| 722 | * @return bool |
||
| 723 | */ |
||
| 724 | public function AllowSales() |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Casted variable to show if sales are allowed to this country. |
||
| 739 | * |
||
| 740 | * @return string |
||
| 741 | */ |
||
| 742 | public function AllowSalesNice() |
||
| 754 | } |
||
| 755 |
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.