Complex classes like EcommerceCurrency 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 EcommerceCurrency, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class EcommerceCurrency extends DataObject implements EditableEcommerceObject |
||
|
|
|||
| 12 | { |
||
| 13 | /** |
||
| 14 | * standard SS variable. |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | private static $db = array( |
||
| 19 | 'Code' => 'Varchar(3)', |
||
| 20 | 'Name' => 'Varchar(100)', |
||
| 21 | 'InUse' => 'Boolean', |
||
| 22 | ); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * standard SS variable. |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private static $indexes = array( |
||
| 30 | 'Code' => true, |
||
| 31 | ); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * standard SS variable. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private static $casting = array( |
||
| 39 | 'IsDefault' => 'Boolean', |
||
| 40 | 'IsDefaultNice' => 'Varchar', |
||
| 41 | 'InUseNice' => 'Varchar', |
||
| 42 | 'ExchangeRate' => 'Double', |
||
| 43 | 'DefaultSymbol' => 'Varchar', |
||
| 44 | 'ShortSymbol' => 'Varchar', |
||
| 45 | 'LongSymbol' => 'Varchar', |
||
| 46 | ); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * standard SS variable. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private static $searchable_fields = array( |
||
| 54 | 'Code' => 'PartialMatchFilter', |
||
| 55 | 'Name' => 'PartialMatchFilter', |
||
| 56 | ); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * standard SS variable. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | private static $field_labels = array( |
||
| 64 | 'Code' => 'Short Code', |
||
| 65 | 'Name' => 'Name', |
||
| 66 | 'InUse' => 'It is available for use?', |
||
| 67 | 'ExchangeRate' => 'Exchange Rate', |
||
| 68 | 'ExchangeRateExplanation' => 'Exchange Rate explanation', |
||
| 69 | 'IsDefaultNice' => 'Is default currency for site', |
||
| 70 | 'DefaultSymbol' => 'Default symbol', |
||
| 71 | 'ShortSymbol' => 'Short symbol', |
||
| 72 | 'LongSymbol' => 'Long symbol', |
||
| 73 | ); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * standard SS variable. |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | private static $summary_fields = array( |
||
| 81 | 'Code' => 'Code', |
||
| 82 | 'Name' => 'Name', |
||
| 83 | 'InUseNice' => 'Available', |
||
| 84 | 'IsDefaultNice' => 'Default Currency', |
||
| 85 | 'ExchangeRate' => 'Exchange Rate', |
||
| 86 | ); //note no => for relational fields |
||
| 87 | |||
| 88 | /** |
||
| 89 | * standard SS variable. |
||
| 90 | * |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | private static $singular_name = 'Currency'; |
||
| 94 | public function i18n_singular_name() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * standard SS variable. |
||
| 101 | * |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | private static $plural_name = 'Currencies'; |
||
| 105 | public function i18n_plural_name() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * standard SS variable. |
||
| 112 | * |
||
| 113 | * @var string |
||
| 114 | */ |
||
| 115 | private static $default_sort = '"InUse" DESC, "Name" ASC, "Code" ASC'; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * standard SS variable. |
||
| 119 | * |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | private static $defaults = array( |
||
| 123 | 'InUse' => true, |
||
| 124 | ); |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Standard SS Method. |
||
| 128 | * |
||
| 129 | * @param Member $member |
||
| 130 | * |
||
| 131 | * @var bool |
||
| 132 | */ |
||
| 133 | public function canCreate($member = null) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Standard SS Method. |
||
| 151 | * |
||
| 152 | * @param Member $member |
||
| 153 | * |
||
| 154 | * @var bool |
||
| 155 | */ |
||
| 156 | public function canView($member = null) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Standard SS Method. |
||
| 174 | * |
||
| 175 | * @param Member $member |
||
| 176 | * |
||
| 177 | * @var bool |
||
| 178 | */ |
||
| 179 | public function canEdit($member = null) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Standard SS method. |
||
| 197 | * |
||
| 198 | * @param Member $member |
||
| 199 | * |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | public function canDelete($member = null) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * NOTE: when there is only one currency we return an empty DataList |
||
| 224 | * as one currency is meaningless. |
||
| 225 | * |
||
| 226 | * @return DataList | null |
||
| 227 | */ |
||
| 228 | public static function ecommerce_currency_list() |
||
| 245 | |||
| 246 | public static function get_list() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param float $price |
||
| 261 | * @param Order $order |
||
| 262 | * |
||
| 263 | * @return Money |
||
| 264 | */ |
||
| 265 | public static function get_money_object_from_order_currency($price, Order $order = null) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * returns the default currency. |
||
| 288 | */ |
||
| 289 | public static function default_currency() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * returns the default currency as Code. |
||
| 303 | * |
||
| 304 | * @return string - e.g. NZD |
||
| 305 | */ |
||
| 306 | public static function default_currency_code() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return int |
||
| 324 | */ |
||
| 325 | public static function default_currency_id() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Only returns a currency when it is a valid currency. |
||
| 334 | * |
||
| 335 | * @param string $currencyCode - the code of the currency, e.g. nzd |
||
| 336 | * |
||
| 337 | * @return EcommerceCurrency | Null |
||
| 338 | */ |
||
| 339 | public static function get_one_from_code($currencyCode) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * STANDARD SILVERSTRIPE STUFF. |
||
| 353 | **/ |
||
| 354 | public function getCMSFields() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * link to edit the record. |
||
| 379 | * |
||
| 380 | * @param string | Null $action - e.g. edit |
||
| 381 | * |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | public function CMSEditLink($action = null) |
||
| 392 | |||
| 393 | public function DefaultSymbol() |
||
| 401 | |||
| 402 | public function ShortSymbol() |
||
| 410 | |||
| 411 | public function LongSymbol() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * casted variable method. |
||
| 422 | * |
||
| 423 | * @return bool |
||
| 424 | */ |
||
| 425 | public function IsDefault() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * casted variable method. |
||
| 443 | * |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | public function IsDefaultNice() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * casted variable method. |
||
| 461 | * |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | public function InUseNice() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * casted variable. |
||
| 479 | * @alias for getExchangeRate |
||
| 480 | * |
||
| 481 | * @return float |
||
| 482 | */ |
||
| 483 | public function ExchangeRate() |
||
| 487 | |||
| 488 | /** |
||
| 489 | * |
||
| 490 | * @return float |
||
| 491 | */ |
||
| 492 | public function getExchangeRate() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * casted variable. |
||
| 502 | * |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | public function ExchangeRateExplanation() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * |
||
| 512 | * |
||
| 513 | * @return string |
||
| 514 | */ |
||
| 515 | public function getExchangeRateExplanation() |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @return bool |
||
| 529 | */ |
||
| 530 | public function IsCurrent() |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Returns the link that can be used in the shopping cart to |
||
| 539 | * set the preferred currency to this one. |
||
| 540 | * For example: /shoppingcart/setcurrency/nzd/ |
||
| 541 | * Dont be fooled by the set_ part in the set_currency_link.... |
||
| 542 | * |
||
| 543 | * @return string |
||
| 544 | */ |
||
| 545 | public function Link() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * returns the link type. |
||
| 552 | * |
||
| 553 | * @return string (link | default | current) |
||
| 554 | */ |
||
| 555 | public function LinkingMode() |
||
| 569 | |||
| 570 | public function validate() |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Standard SS Method |
||
| 598 | * Adds the default currency. |
||
| 599 | */ |
||
| 600 | public function populateDefaults() |
||
| 605 | |||
| 606 | public function onBeforeWrite() |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Standard SS Method |
||
| 621 | * Adds the default currency. |
||
| 622 | */ |
||
| 623 | public function requireDefaultRecords() |
||
| 631 | |||
| 632 | /** |
||
| 633 | * checks if a currency exists, creates it and returns it. |
||
| 634 | * |
||
| 635 | * @param string $code |
||
| 636 | * @param string $name OPTIONAL |
||
| 637 | */ |
||
| 638 | public static function create_new($code, $name = '') |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Debug helper method. |
||
| 668 | * Can be called from /shoppingcart/debug/. |
||
| 669 | * |
||
| 670 | * @return string |
||
| 671 | */ |
||
| 672 | public function debug() |
||
| 676 | |||
| 677 | private static $currencies = array( |
||
| 678 | 'AFA' => 'afghanistan afghanis', |
||
| 679 | 'ALL' => 'albania leke', |
||
| 680 | 'DZD' => 'algeria dinars', |
||
| 681 | 'ARS' => 'argentina pesos', |
||
| 682 | 'AUD' => 'australia dollars', |
||
| 683 | 'ATS' => 'austria schillings*', |
||
| 684 | 'BSD' => 'bahamas dollars', |
||
| 685 | 'BHD' => 'bahrain dinars', |
||
| 686 | 'BDT' => 'bangladesh taka', |
||
| 687 | 'BBD' => 'barbados dollars', |
||
| 688 | 'BEF' => 'belgium francs*', |
||
| 689 | 'BMD' => 'bermuda dollars', |
||
| 690 | 'BRL' => 'brazil reais', |
||
| 691 | 'BGN' => 'bulgaria leva', |
||
| 692 | 'CAD' => 'canada dollars', |
||
| 693 | 'XOF' => 'cfa bceao francs', |
||
| 694 | 'XAF' => 'cfa beac francs', |
||
| 695 | 'CLP' => 'chile pesos', |
||
| 696 | 'CNY' => 'china yuan renminbi', |
||
| 697 | 'COP' => 'colombia pesos', |
||
| 698 | 'CRC' => 'costa rica colones', |
||
| 699 | 'HRK' => 'croatia kuna', |
||
| 700 | 'CYP' => 'cyprus pounds', |
||
| 701 | 'CZK' => 'czech republic koruny', |
||
| 702 | 'DKK' => 'denmark kroner', |
||
| 703 | 'DOP' => 'dominican republic pesos', |
||
| 704 | 'XCD' => 'eastern caribbean dollars', |
||
| 705 | 'EGP' => 'egypt pounds', |
||
| 706 | 'EEK' => 'estonia krooni', |
||
| 707 | 'EUR' => 'euro', |
||
| 708 | 'FJD' => 'fiji dollars', |
||
| 709 | 'DEM' => 'germany deutsche marks*', |
||
| 710 | 'XAU' => 'gold ounces', |
||
| 711 | 'NLG' => 'holland (netherlands) guilders*', |
||
| 712 | 'HKD' => 'hong kong dollars', |
||
| 713 | 'HUF' => 'hungary forint', |
||
| 714 | 'ISK' => 'iceland kronur', |
||
| 715 | 'XDR' => 'imf special drawing right', |
||
| 716 | 'INR' => 'india rupees', |
||
| 717 | 'IDR' => 'indonesia rupiahs', |
||
| 718 | 'IRR' => 'iran rials', |
||
| 719 | 'IQD' => 'iraq dinars', |
||
| 720 | 'ILS' => 'israel new shekels', |
||
| 721 | 'JMD' => 'jamaica dollars', |
||
| 722 | 'JPY' => 'japan yen', |
||
| 723 | 'JOD' => 'jordan dinars', |
||
| 724 | 'KES' => 'kenya shillings', |
||
| 725 | 'KRW' => 'korea (south) won', |
||
| 726 | 'KWD' => 'kuwait dinars', |
||
| 727 | 'LBP' => 'lebanon pounds', |
||
| 728 | 'MYR' => 'malaysia ringgits', |
||
| 729 | 'MTL' => 'malta liri', |
||
| 730 | 'MUR' => 'mauritius rupees', |
||
| 731 | 'MXN' => 'mexico pesos', |
||
| 732 | 'MAD' => 'morocco dirhams', |
||
| 733 | 'NZD' => 'new zealand dollars', |
||
| 734 | 'NOK' => 'norway kroner', |
||
| 735 | 'OMR' => 'oman rials', |
||
| 736 | 'PKR' => 'pakistan rupees', |
||
| 737 | 'XPD' => 'palladium ounces', |
||
| 738 | 'PEN' => 'peru nuevos soles', |
||
| 739 | 'PHP' => 'philippines pesos', |
||
| 740 | 'PLN' => 'poland zlotych', |
||
| 741 | 'QAR' => 'qatar riyals', |
||
| 742 | 'ROL' => 'romania lei', |
||
| 743 | 'RUB' => 'russia rubles', |
||
| 744 | 'SAR' => 'saudi arabia riyals', |
||
| 745 | 'XAG' => 'silver ounces', |
||
| 746 | 'SGD' => 'singapore dollars', |
||
| 747 | 'SKK' => 'slovakia koruny', |
||
| 748 | 'SIT' => 'slovenia tolars', |
||
| 749 | 'ZAR' => 'south africa rand', |
||
| 750 | 'KRW' => 'south korea won', |
||
| 751 | 'LKR' => 'sri lanka rupees', |
||
| 752 | 'SDD' => 'sudan dinars', |
||
| 753 | 'SEK' => 'sweden kronor', |
||
| 754 | 'CHF' => 'switzerland francs', |
||
| 755 | 'TWD' => 'taiwan new dollars', |
||
| 756 | 'THB' => 'thailand baht', |
||
| 757 | 'TTD' => 'trinidad and tobago dollars', |
||
| 758 | 'TND' => 'tunisia dinars', |
||
| 759 | 'TRY' => 'turkey new lira', |
||
| 760 | 'AED' => 'united arab emirates dirhams', |
||
| 761 | 'gbp' => 'united kingdom pounds', |
||
| 762 | 'USD' => 'united states dollars', |
||
| 763 | 'VEB' => 'venezuela bolivares', |
||
| 764 | 'VND' => 'vietnam dong', |
||
| 765 | 'ZMK' => 'zambia kwacha', |
||
| 766 | ); |
||
| 767 | } |
||
| 768 |
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.