Complex classes like EcommerceDBConfig 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 EcommerceDBConfig, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class EcommerceDBConfig extends DataObject implements EditableEcommerceObject |
||
|
|
|||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Standard SS Variable. |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | private static $db = array( |
||
| 21 | 'Title' => 'Varchar(30)', |
||
| 22 | 'UseThisOne' => 'Boolean', |
||
| 23 | 'ShopClosed' => 'Boolean', |
||
| 24 | 'ShopPricesAreTaxExclusive' => 'Boolean', |
||
| 25 | 'InvoiceTitle' => 'Varchar(200)', |
||
| 26 | 'PackingSlipTitle' => 'Varchar(200)', |
||
| 27 | 'PackingSlipNote' => 'HTMLText', |
||
| 28 | 'ShopPhysicalAddress' => 'HTMLText', |
||
| 29 | 'ReceiptEmail' => 'Varchar(255)', |
||
| 30 | 'PostalCodeURL' => 'Varchar(255)', |
||
| 31 | 'PostalCodeLabel' => 'Varchar(255)', |
||
| 32 | 'NumberOfProductsPerPage' => 'Int', |
||
| 33 | 'ProductsAlsoInOtherGroups' => 'Boolean', |
||
| 34 | 'OnlyShowProductsThatCanBePurchased' => 'Boolean', |
||
| 35 | 'NotForSaleMessage' => 'HTMLText', |
||
| 36 | 'ProductsHaveWeight' => 'Boolean', |
||
| 37 | 'ProductsHaveModelNames' => 'Boolean', |
||
| 38 | 'ProductsHaveQuantifiers' => 'Boolean', |
||
| 39 | //"ProductsHaveVariations" => "Boolean", |
||
| 40 | 'CurrenciesExplanation' => 'HTMLText', |
||
| 41 | 'AllowFreeProductPurchase' => 'Boolean', |
||
| 42 | ); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Standard SS Variable. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private static $has_one = array( |
||
| 50 | 'EmailLogo' => 'Image', |
||
| 51 | 'DefaultProductImage' => 'Product_Image', |
||
| 52 | ); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Standard SS Variable. |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private static $indexes = array( |
||
| 60 | 'UseThisOne' => true, |
||
| 61 | 'ShopClosed' => true, |
||
| 62 | 'ShopPricesAreTaxExclusive' => true, |
||
| 63 | 'NumberOfProductsPerPage' => true, |
||
| 64 | 'OnlyShowProductsThatCanBePurchased' => true, |
||
| 65 | ); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Standard SS Variable. |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | private static $casting = array( |
||
| 73 | 'UseThisOneNice' => 'Varchar', |
||
| 74 | ); //adds computed fields that can also have a type (e.g. |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Standard SS Variable. |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | private static $searchable_fields = array( |
||
| 82 | 'Title' => 'PartialMatchFilter', |
||
| 83 | ); |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Standard SS Variable. |
||
| 87 | * |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | private static $field_labels = array(); |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Standard SS Variable. |
||
| 94 | * |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | private static $summary_fields = array( |
||
| 98 | 'Title' => 'Title', |
||
| 99 | 'UseThisOneNice' => 'Use this configuration set', |
||
| 100 | ); //note no => for relational fields |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Standard SS Method. |
||
| 104 | * |
||
| 105 | * @param Member $member |
||
| 106 | * |
||
| 107 | * @var bool |
||
| 108 | */ |
||
| 109 | public function canCreate($member = null) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Standard SS Method. |
||
| 124 | * |
||
| 125 | * @param Member $member |
||
| 126 | * |
||
| 127 | * @var bool |
||
| 128 | */ |
||
| 129 | public function canView($member = null) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Standard SS Method. |
||
| 144 | * |
||
| 145 | * @param Member $member |
||
| 146 | * |
||
| 147 | * @var bool |
||
| 148 | */ |
||
| 149 | public function canEdit($member = null) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Standard SS Method. |
||
| 167 | * |
||
| 168 | * @param Member $member |
||
| 169 | * |
||
| 170 | * @var bool |
||
| 171 | */ |
||
| 172 | public function canDelete($member = null) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Standard SS variable. |
||
| 194 | * |
||
| 195 | * @var string |
||
| 196 | */ |
||
| 197 | private static $default_sort = '"UseThisOne" DESC, "Created" ASC'; |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Standard SS variable. |
||
| 201 | * |
||
| 202 | * @var array |
||
| 203 | */ |
||
| 204 | private static $defaults = array( |
||
| 205 | 'Title' => 'Ecommerce Site Config', |
||
| 206 | 'UseThisOne' => true, |
||
| 207 | 'ShopClosed' => false, |
||
| 208 | 'ShopPricesAreTaxExclusive' => false, |
||
| 209 | 'InvoiceTitle' => 'Invoice', |
||
| 210 | 'PackingSlipTitle' => 'Package Contents', |
||
| 211 | 'PackingSlipNote' => 'Please make sure that all items are contained in this package.', |
||
| 212 | 'ShopPhysicalAddress' => '<p>Enter your shop address here.</p>', |
||
| 213 | //"ReceiptEmail" => "Varchar(255)", - see populate defaults |
||
| 214 | 'PostalCodeURL' => '', |
||
| 215 | 'PostalCodeLabel' => '', |
||
| 216 | 'NumberOfProductsPerPage' => 12, |
||
| 217 | 'ProductsAlsoInOtherGroups' => false, |
||
| 218 | 'OnlyShowProductsThatCanBePurchased' => false, |
||
| 219 | 'NotForSaleMessage' => '<p>Not for sale, please contact us for more information.</p>', |
||
| 220 | 'ProductsHaveWeight' => false, |
||
| 221 | 'ProductsHaveModelNames' => false, |
||
| 222 | 'ProductsHaveQuantifiers' => false, |
||
| 223 | //"ProductsHaveVariations" => false, |
||
| 224 | 'CurrenciesExplanation' => '<p>Apart from our main currency, you can view prices in a number of other currencies. The exchange rate is indicative only.</p>', |
||
| 225 | 'AllowFreeProductPurchase' => true, |
||
| 226 | ); |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Standard SS Method. |
||
| 230 | * |
||
| 231 | * @var array |
||
| 232 | */ |
||
| 233 | public function populateDefaults() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Standard SS variable. |
||
| 241 | * |
||
| 242 | * @var string |
||
| 243 | */ |
||
| 244 | private static $singular_name = 'Main E-commerce Configuration'; |
||
| 245 | public function i18n_singular_name() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Standard SS variable. |
||
| 252 | * |
||
| 253 | * @var string |
||
| 254 | */ |
||
| 255 | private static $plural_name = 'Main E-commerce Configurations'; |
||
| 256 | public function i18n_plural_name() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Standard SS variable. |
||
| 263 | * |
||
| 264 | * @var string |
||
| 265 | */ |
||
| 266 | private static $description = 'A set of configurations for the shop. Each shop needs to have one or more of these settings.'; |
||
| 267 | |||
| 268 | /** |
||
| 269 | * static holder for its own (or other EcommerceDBConfig) class. |
||
| 270 | * |
||
| 271 | * @var string | NULL |
||
| 272 | */ |
||
| 273 | private static $_my_current_one = null; |
||
| 274 | public static function reset_my_current_one() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * implements singleton pattern. |
||
| 281 | * Gets the current USE THIS ONE e-commerce option. |
||
| 282 | * |
||
| 283 | * @return EcommerceDBConfig | Object |
||
| 284 | */ |
||
| 285 | public static function current_ecommerce_db_config() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * standard SS method for decorators. |
||
| 304 | * |
||
| 305 | * @param bool $includerelations |
||
| 306 | * |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | public function fieldLabels($includerelations = true) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * definition of field lables |
||
| 326 | * TODO: is this a common SS method? |
||
| 327 | * |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | public function customFieldLabels() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * definition of field lables |
||
| 367 | * TODO: is this a common SS method? |
||
| 368 | * |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | public function customDescriptionsForFields() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * standard SS method. |
||
| 390 | * |
||
| 391 | * @return FieldList |
||
| 392 | */ |
||
| 393 | public function getCMSFields() |
||
| 521 | |||
| 522 | /** |
||
| 523 | * link to edit the record. |
||
| 524 | * |
||
| 525 | * @param string | Null $action - e.g. edit |
||
| 526 | * |
||
| 527 | * @return string |
||
| 528 | */ |
||
| 529 | public function CMSEditLink($action = null) |
||
| 537 | |||
| 538 | public function getOrderStepsField() |
||
| 552 | |||
| 553 | /** |
||
| 554 | * tells us if a Class Name is a buyable. |
||
| 555 | * |
||
| 556 | * @todo: consider using Ecomerce Configuration instead? |
||
| 557 | * In EcomConfig we only list base classes. |
||
| 558 | * |
||
| 559 | * @param string $className - name of the class to be tested |
||
| 560 | * |
||
| 561 | * @return bool |
||
| 562 | */ |
||
| 563 | public static function is_buyable($className) |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Returns the Current Member. |
||
| 575 | */ |
||
| 576 | public function Customer() |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Returns the Current Member. |
||
| 583 | */ |
||
| 584 | public function CustomerForOrder() |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Return the currency being used on the site e.g. "NZD" or "USD". |
||
| 593 | * |
||
| 594 | * @return string |
||
| 595 | */ |
||
| 596 | public function Currency() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * return null if there is less than two currencies in use |
||
| 603 | * on the site. |
||
| 604 | * |
||
| 605 | * @return DataList | Null |
||
| 606 | */ |
||
| 607 | public function Currencies() |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @return string (URLSegment) |
||
| 617 | **/ |
||
| 618 | public function AccountPageLink() |
||
| 622 | |||
| 623 | /** |
||
| 624 | * @return string (URLSegment) |
||
| 625 | **/ |
||
| 626 | public function CheckoutLink() |
||
| 630 | |||
| 631 | /** |
||
| 632 | *@return string (URLSegment) |
||
| 633 | **/ |
||
| 634 | public function CartPageLink() |
||
| 638 | |||
| 639 | /** |
||
| 640 | *@return string (URLSegment) |
||
| 641 | **/ |
||
| 642 | public function OrderConfirmationPageLink() |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Returns a link to a default image. |
||
| 649 | * If a default image is set in the site config then this link is returned |
||
| 650 | * Otherwise, a standard link is returned. |
||
| 651 | * |
||
| 652 | * @return string |
||
| 653 | */ |
||
| 654 | public function DefaultImageLink() |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Returns the default image or a dummy one if it does not exists. |
||
| 668 | * |
||
| 669 | * @return string |
||
| 670 | */ |
||
| 671 | public function DefaultImage() |
||
| 686 | |||
| 687 | /** |
||
| 688 | * standard SS method. |
||
| 689 | */ |
||
| 690 | public function onAfterWrite() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * standard SS Method. |
||
| 716 | */ |
||
| 717 | public function requireDefaultRecords() |
||
| 731 | |||
| 732 | /** |
||
| 733 | * returns site config. |
||
| 734 | * |
||
| 735 | * @return SiteConfig |
||
| 736 | */ |
||
| 737 | public function SiteConfig() |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Casted Variable. |
||
| 744 | * |
||
| 745 | * @return string |
||
| 746 | */ |
||
| 747 | public function UseThisOneNice() |
||
| 751 | } |
||
| 752 |
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.