Complex classes like EcommerceRegion 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 EcommerceRegion, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class EcommerceRegion extends DataObject implements EditableEcommerceObject |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * what variables are accessible through http://mysite.com/api/ecommerce/v1/EcommerceRegion/. |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | private static $api_access = array( |
||
|
|
|||
| 23 | 'view' => array( |
||
| 24 | 'Code', |
||
| 25 | 'Name', |
||
| 26 | ), |
||
| 27 | ); |
||
| 28 | |||
| 29 | /** |
||
| 30 | * standard SS variable. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private static $db = array( |
||
| 35 | 'Code' => 'Varchar(20)', |
||
| 36 | 'Name' => 'Varchar(200)', |
||
| 37 | 'DoNotAllowSales' => 'Boolean', |
||
| 38 | 'IsDefault' => 'Boolean', |
||
| 39 | ); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * standard SS variable. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private static $has_one = array( |
||
| 47 | 'Country' => 'EcommerceCountry', |
||
| 48 | ); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * standard SS variable. |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private static $indexes = array( |
||
| 56 | 'Name' => true, |
||
| 57 | 'Code' => true, |
||
| 58 | 'DoNotAllowSales' => true |
||
| 59 | ); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * standard SS variable. |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private static $default_sort = [ |
||
| 67 | 'Name' => 'ASC', |
||
| 68 | 'ID' => 'ASC' |
||
| 69 | ]; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * standard SS variable. |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | private static $singular_name = 'Region'; |
||
| 77 | public function i18n_singular_name() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * standard SS variable. |
||
| 84 | * |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | private static $plural_name = 'Regions'; |
||
| 88 | public function i18n_plural_name() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * standard SS variable. |
||
| 95 | * |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | private static $searchable_fields = array( |
||
| 99 | 'Name' => 'PartialMatchFilter', |
||
| 100 | 'Code' => 'PartialMatchFilter', |
||
| 101 | ); |
||
| 102 | |||
| 103 | /** |
||
| 104 | * standard SS variable. |
||
| 105 | * |
||
| 106 | * @var array |
||
| 107 | */ |
||
| 108 | private static $field_labels = array( |
||
| 109 | 'Name' => 'Region', |
||
| 110 | ); |
||
| 111 | |||
| 112 | /** |
||
| 113 | * standard SS variable. |
||
| 114 | * |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | private static $summary_fields = array( |
||
| 118 | 'Name' => 'Name', |
||
| 119 | 'Country.Title' => 'Country', |
||
| 120 | ); |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Standard SS variable. |
||
| 124 | * |
||
| 125 | * @var string |
||
| 126 | */ |
||
| 127 | private static $description = 'A region within a country. This can be a state or a province or the equivalent.'; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * do we use regions at all in this ecommerce application? |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | **/ |
||
| 134 | public static function show() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Standard SS FieldList. |
||
| 144 | * |
||
| 145 | * @return FieldList |
||
| 146 | **/ |
||
| 147 | public function getCMSFields() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * link to edit the record. |
||
| 158 | * |
||
| 159 | * @param string | Null $action - e.g. edit |
||
| 160 | * |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | public function CMSEditLink($action = null) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * checks if a code is allowed. |
||
| 170 | * |
||
| 171 | * @param string $code - e.g. NZ, NSW, or CO |
||
| 172 | * |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | public static function code_allowed($code) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * checks if a code is allowed. |
||
| 190 | * |
||
| 191 | * @param string $code - e.g. NZ, NSW, or CO |
||
| 192 | * |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | public static function regionid_allowed($regionID) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * converts a code into a proper title. |
||
| 202 | * |
||
| 203 | * @param int $regionID (Code) |
||
| 204 | * |
||
| 205 | * @return string ( name) |
||
| 206 | */ |
||
| 207 | public static function find_title($regionID) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * This function returns back the default list of regions, filtered by the currently selected country. |
||
| 220 | * |
||
| 221 | * @return array - array of Region.ID => Region.Name |
||
| 222 | **/ |
||
| 223 | protected static function get_default_array() |
||
| 240 | |||
| 241 | // DYNAMIC LIMITS..... |
||
| 242 | |||
| 243 | /** |
||
| 244 | * takes the defaultArray and limits it with "only show" and "do not show" value, relevant for the current order. |
||
| 245 | * |
||
| 246 | * @return array (Code, Title) |
||
| 247 | **/ |
||
| 248 | public static function list_of_allowed_entries_for_dropdown() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * these variables and methods allow to to "dynamically limit the regions available, based on, for example: ordermodifiers, item selection, etc.... |
||
| 273 | * for example, if hot delivery of a catering item is only available in a certain region, then the regions can be limited with the methods below. |
||
| 274 | * NOTE: these methods / variables below are IMPORTANT, because they allow the dropdown for the region to be limited for just that order. |
||
| 275 | * |
||
| 276 | * @var array of regions codes, e.g. ("NSW", "WA", "VIC"); |
||
| 277 | **/ |
||
| 278 | protected static $_for_current_order_only_show_regions = array(); |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param int $orderID |
||
| 282 | * |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | public static function get_for_current_order_only_show_regions($orderID = 0) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * merges arrays... |
||
| 294 | * |
||
| 295 | * @param array $a |
||
| 296 | * @param int $orderID |
||
| 297 | */ |
||
| 298 | public static function set_for_current_order_only_show_regions(array $a, $orderID = 0) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @var array |
||
| 307 | */ |
||
| 308 | private static $_for_current_order_do_not_show_regions = array(); |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param int $orderID |
||
| 312 | * |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | public static function get_for_current_order_do_not_show_regions($orderID = 0) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * merges arrays... |
||
| 324 | * |
||
| 325 | * @param array $a |
||
| 326 | * @param int $orderID |
||
| 327 | */ |
||
| 328 | public static function set_for_current_order_do_not_show_regions(array $a, $orderID = 0) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * This function works out the most likely region for the current order. |
||
| 337 | * |
||
| 338 | * @return int |
||
| 339 | **/ |
||
| 340 | public static function get_region_id() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * returns the country based on the Visitor Country Provider. |
||
| 371 | * this is some sort of IP recogniser system (e.g. Geoip Class). |
||
| 372 | * |
||
| 373 | * @return int |
||
| 374 | **/ |
||
| 375 | public static function get_region_from_ip() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @alias for get_region_id |
||
| 388 | */ |
||
| 389 | public static function get_region() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * standard SS method |
||
| 396 | * cleans up codes. |
||
| 397 | */ |
||
| 398 | public function onBeforeWrite() |
||
| 404 | } |
||
| 405 | |||
| 419 |