Complex classes like CountryPrice_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 CountryPrice_EcommerceCountry, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class CountryPrice_EcommerceCountry extends DataExtension |
||
|
|
|||
| 9 | { |
||
| 10 | private static $db = array( |
||
| 11 | 'IsBackupCountry' => 'Boolean', |
||
| 12 | 'FAQContent' => 'HTMLText', |
||
| 13 | 'TopBarMessage' => 'Varchar(255)', |
||
| 14 | 'DeliveryCostNote' => 'Varchar(255)', |
||
| 15 | 'ShippingEstimation' => 'Varchar(255)', |
||
| 16 | 'ReturnInformation' => 'Varchar(255)', |
||
| 17 | 'ProductNotAvailableNote' => 'HTMLText', |
||
| 18 | 'LanguageAndCountryCode' => 'Varchar(20)' |
||
| 19 | ); |
||
| 20 | |||
| 21 | private static $has_one = array( |
||
| 22 | 'Distributor' => 'Distributor', |
||
| 23 | 'EcommerceCurrency' => 'EcommerceCurrency', |
||
| 24 | 'AlwaysTheSameAs' => 'EcommerceCountry' |
||
| 25 | ); |
||
| 26 | |||
| 27 | private static $has_many = array( |
||
| 28 | 'ParentFor' => 'EcommerceCountry' |
||
| 29 | ); |
||
| 30 | |||
| 31 | private static $searchable_fields = array( |
||
| 32 | "AlwaysTheSameAsID" => true, |
||
| 33 | "IsBackupCountry" => "ExactMatchFilter" |
||
| 34 | ); |
||
| 35 | |||
| 36 | private static $default_sort = array( |
||
| 37 | 'Name' => 'DESC' |
||
| 38 | ); |
||
| 39 | |||
| 40 | |||
| 41 | private static $indexes = array( |
||
| 42 | "IsBackupCountry" => true |
||
| 43 | ); |
||
| 44 | |||
| 45 | private static $casting = array( |
||
| 46 | "LanguageAndCountryCode" => 'Varchar' |
||
| 47 | ); |
||
| 48 | |||
| 49 | public function updateCMSFields(FieldList $fields) |
||
| 117 | |||
| 118 | public static function get_real_countries_list() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * |
||
| 126 | * |
||
| 127 | * @param [type] $countryCode [description] |
||
| 128 | * @return DataList |
||
| 129 | */ |
||
| 130 | public static function get_sibling_countries($countryCode = null) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * checks if the country has a distributor |
||
| 154 | * and returns it. If not, returns the defaulf country. |
||
| 155 | * |
||
| 156 | * @return EcommerceCountry |
||
| 157 | * |
||
| 158 | */ |
||
| 159 | public static function get_distributor_country($countryCode = null) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * checks if the country has a distributor |
||
| 172 | * and returns the primary country for the distributor. |
||
| 173 | * If not, returns the defaulf country. |
||
| 174 | * |
||
| 175 | * @return EcommerceCountry |
||
| 176 | * |
||
| 177 | */ |
||
| 178 | public static function get_distributor_primary_country($countryCode = null) |
||
| 189 | |||
| 190 | private static $_get_real_country_cache = array(); |
||
| 191 | |||
| 192 | /** |
||
| 193 | * returns the 'always the same as' (parent) country if necessary |
||
| 194 | * @param EcommerceCountry | string | int (optional) $countryCodeOrObject |
||
| 195 | * @return EcommerceCountry |
||
| 196 | */ |
||
| 197 | public static function get_real_country($country = null) |
||
| 240 | /** |
||
| 241 | * |
||
| 242 | * @return EcommerceCountry |
||
| 243 | */ |
||
| 244 | public static function get_backup_country() |
||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * |
||
| 259 | * |
||
| 260 | * @return boolean |
||
| 261 | */ |
||
| 262 | public function hasDistributor() |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * make sure there is always a backup country ... |
||
| 275 | */ |
||
| 276 | public function requireDefaultRecords() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public function ComputedLanguageAndCountryCode() |
||
| 304 | } |
||
| 305 |
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.