Complex classes like ShoppingCart_Controller 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 ShoppingCart_Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 16 | class ShoppingCart_Controller extends Controller | ||
|  | |||
| 17 | { | ||
| 18 | |||
| 19 | /** | ||
| 20 | * Default URL handlers - (Action)/(ID)/(OtherID). | ||
| 21 | */ | ||
| 22 | private static $url_handlers = array( | ||
| 23 | '$Action//$ID/$OtherID/$Version' => 'handleAction', | ||
| 24 | ); | ||
| 25 | |||
| 26 | /** | ||
| 27 | * We need to only use the Security ID on a few | ||
| 28 | * actions, these are listed here. | ||
| 29 | * | ||
| 30 | * @var array | ||
| 31 | */ | ||
| 32 | protected $methodsRequiringSecurityID = array( | ||
| 33 | 'additem', | ||
| 34 | 'removeitem', | ||
| 35 | 'removeallitem', | ||
| 36 | 'removeallitemandedit', | ||
| 37 | 'removemodifier', | ||
| 38 | 'addmodifier', | ||
| 39 | 'copyorder', | ||
| 40 | 'deleteorder', | ||
| 41 | 'save', | ||
| 42 | ); | ||
| 43 | |||
| 44 | /** | ||
| 45 | * @var ShoppingCart | ||
| 46 | */ | ||
| 47 | protected $cart = null; | ||
| 48 | |||
| 49 | public function init() | ||
| 72 | |||
| 73 | private static $allowed_actions = array( | ||
| 74 | 'json', | ||
| 75 | 'index', | ||
| 76 | 'additem', | ||
| 77 | 'removeitem', | ||
| 78 | 'removeallitem', | ||
| 79 | 'removeallitemandedit', | ||
| 80 | 'removemodifier', | ||
| 81 | 'addmodifier', | ||
| 82 | 'setcountry', | ||
| 83 | 'setregion', | ||
| 84 | 'setcurrency', | ||
| 85 | 'removefromsale', | ||
| 86 | 'setquantityitem', | ||
| 87 | 'clear', | ||
| 88 | 'clearandlogout', | ||
| 89 | 'save', | ||
| 90 | 'deleteorder', | ||
| 91 | 'numberofitemsincart', | ||
| 92 | 'showcart', | ||
| 93 | 'loadorder', | ||
| 94 | 'copyorder', | ||
| 95 | 'removeaddress', | ||
| 96 | 'submittedbuyable', | ||
| 97 | 'placeorderformember', | ||
| 98 | 'loginas',// no need to set to => 'ADMIN', | ||
| 99 | 'debug', // no need to set to => 'ADMIN', | ||
| 100 | 'ajaxtest', // no need to set to => 'ADMIN', | ||
| 101 | ); | ||
| 102 | |||
| 103 | public function index() | ||
| 114 | |||
| 115 | /******************************************************* | ||
| 116 | * CONTROLLER LINKS | ||
| 117 | *******************************************************/ | ||
| 118 | |||
| 119 | /** | ||
| 120 | * @param string $action | ||
| 121 | * | ||
| 122 | * @return string (Link) | ||
| 123 | */ | ||
| 124 | public function Link($action = null) | ||
| 128 | |||
| 129 | /** | ||
| 130 | * returns ABSOLUTE link to the shopping cart controller. | ||
| 131 | * @param null | array | string $actionAndOtherLinkVariables | ||
| 132 | * @return string | ||
| 133 | */ | ||
| 134 | protected static function create_link($actionAndOtherLinkVariables = null) | ||
| 142 | |||
| 143 | /** | ||
| 144 | * @param int $buyableID | ||
| 145 | * @param string $classNameForBuyable | ||
| 146 | * @param array $parameters | ||
| 147 | * | ||
| 148 | * @return string | ||
| 149 | */ | ||
| 150 | public static function add_item_link($buyableID, $classNameForBuyable = 'Product', array $parameters = array()) | ||
| 154 | |||
| 155 | /** | ||
| 156 | * @param int $buyableID | ||
| 157 | * @param string $classNameForBuyable | ||
| 158 | * @param array $parameters | ||
| 159 | * | ||
| 160 | * @return string | ||
| 161 | */ | ||
| 162 | public static function remove_item_link($buyableID, $classNameForBuyable = 'Product', array $parameters = array()) | ||
| 166 | |||
| 167 | /** | ||
| 168 | * @param int $buyableID | ||
| 169 | * @param string $classNameForBuyable | ||
| 170 | * @param array $parameters | ||
| 171 | * | ||
| 172 | * @return string | ||
| 173 | */ | ||
| 174 | public static function remove_all_item_link($buyableID, $classNameForBuyable = 'Product', array $parameters = array()) | ||
| 178 | |||
| 179 | /** | ||
| 180 | * @param int $buyableID | ||
| 181 | * @param string $classNameForBuyable | ||
| 182 | * @param array $parameters | ||
| 183 | * | ||
| 184 | * @return string | ||
| 185 | */ | ||
| 186 | public static function remove_all_item_and_edit_link($buyableID, $classNameForBuyable = 'Product', array $parameters = array()) | ||
| 190 | |||
| 191 | /** | ||
| 192 | * @param int $buyableID | ||
| 193 | * @param string $classNameForBuyable | ||
| 194 | * @param array $parameters | ||
| 195 | * | ||
| 196 | * @return string | ||
| 197 | */ | ||
| 198 | public static function set_quantity_item_link($buyableID, $classNameForBuyable = 'Product', array $parameters = array()) | ||
| 202 | |||
| 203 | /** | ||
| 204 | * @param int $modifierID | ||
| 205 | * @param array $parameters | ||
| 206 | * | ||
| 207 | * @return string | ||
| 208 | */ | ||
| 209 | public static function remove_modifier_link($modifierID, array $parameters = array()) | ||
| 213 | |||
| 214 | /** | ||
| 215 | * @param int $modifierID | ||
| 216 | * @param array $parameters | ||
| 217 | * | ||
| 218 | * @return string | ||
| 219 | */ | ||
| 220 | public static function add_modifier_link($modifierID, array $parameters = array()) | ||
| 224 | |||
| 225 | /** | ||
| 226 | * @param int $addressID | ||
| 227 | * @param string $addressClassName | ||
| 228 | * @param array $parameters | ||
| 229 | * | ||
| 230 | * @return string | ||
| 231 | */ | ||
| 232 | public static function remove_address_link($addressID, $addressClassName, array $parameters = array()) | ||
| 236 | |||
| 237 | /** | ||
| 238 | * @param array $parameters | ||
| 239 | * | ||
| 240 | * @return string | ||
| 241 | */ | ||
| 242 | public static function clear_cart_link($parameters = array()) | ||
| 246 | |||
| 247 | /** | ||
| 248 | * @param array $parameters | ||
| 249 | * | ||
| 250 | * @return string | ||
| 251 | */ | ||
| 252 | public static function save_cart_link(array $parameters = array()) | ||
| 256 | |||
| 257 | /** | ||
| 258 | * @param array $parameters | ||
| 259 | * | ||
| 260 | * @return string | ||
| 261 | */ | ||
| 262 | public static function clear_cart_and_logout_link(array $parameters = array()) | ||
| 266 | |||
| 267 | /** | ||
| 268 | * @param array $parameters | ||
| 269 | * | ||
| 270 | * @return string | ||
| 271 | */ | ||
| 272 | public static function delete_order_link($orderID, array $parameters = array()) | ||
| 276 | |||
| 277 | /** | ||
| 278 | * | ||
| 279 | * @return null | string | ||
| 280 | */ | ||
| 281 | public static function copy_order_link($orderID, $parameters = array()) | ||
| 288 | |||
| 289 | /** | ||
| 290 | * returns a link that allows you to set a currency... | ||
| 291 | * dont be fooled by the set_ part... | ||
| 292 | * | ||
| 293 | * @param string $code | ||
| 294 | * | ||
| 295 | * @return string | ||
| 296 | */ | ||
| 297 | public static function set_currency_link($code, array $parameters = array()) | ||
| 301 | |||
| 302 | /** | ||
| 303 | * | ||
| 304 | * | ||
| 305 | * @param int $id | ||
| 306 | * @param string $className | ||
| 307 | * @return string | ||
| 308 | */ | ||
| 309 | public static function remove_from_sale_link($id, $className) | ||
| 313 | |||
| 314 | /** | ||
| 315 | * return json for cart... no further actions. | ||
| 316 | * | ||
| 317 | * @param SS_HTTPRequest | ||
| 318 | * | ||
| 319 | * @return JSON | ||
| 320 | */ | ||
| 321 | public function json(SS_HTTPRequest $request) | ||
| 325 | |||
| 326 | /** | ||
| 327 | * Adds item to cart via controller action; one by default. | ||
| 328 | * | ||
| 329 | * @param HTTPRequest | ||
| 330 | * | ||
| 331 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); | ||
| 332 | * If it is not AJAX it redirects back to requesting page. | ||
| 333 | */ | ||
| 334 | public function additem(SS_HTTPRequest $request) | ||
| 345 | |||
| 346 | /** | ||
| 347 | * Sets the exact passed quantity. | ||
| 348 | * Note: If no ?quantity=x is specified in URL, then quantity will be set to 1. | ||
| 349 | * | ||
| 350 | * @param HTTPRequest | ||
| 351 | * | ||
| 352 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); | ||
| 353 | * If it is not AJAX it redirects back to requesting page. | ||
| 354 | */ | ||
| 355 | public function setquantityitem(SS_HTTPRequest $request) | ||
| 366 | |||
| 367 | /** | ||
| 368 | * Removes item from cart via controller action; one by default. | ||
| 369 | * | ||
| 370 | * @param HTTPRequest | ||
| 371 | * | ||
| 372 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); | ||
| 373 | * If it is not AJAX it redirects back to requesting page. | ||
| 374 | */ | ||
| 375 | public function removeitem(SS_HTTPRequest $request) | ||
| 386 | |||
| 387 | /** | ||
| 388 | * Removes all of a specific item. | ||
| 389 | * | ||
| 390 | * @param HTTPRequest | ||
| 391 | * | ||
| 392 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); | ||
| 393 | * If it is not AJAX it redirects back to requesting page. | ||
| 394 | */ | ||
| 395 | public function removeallitem(SS_HTTPRequest $request) | ||
| 409 | |||
| 410 | /** | ||
| 411 | * Removes all of a specific item AND return back. | ||
| 412 | * | ||
| 413 | * @param HTTPRequest | ||
| 414 | * | ||
| 415 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); | ||
| 416 | * If it is not AJAX it redirects back to requesting page. | ||
| 417 | */ | ||
| 418 | public function removeallitemandedit(SS_HTTPRequest $request) | ||
| 429 | |||
| 430 | /** | ||
| 431 | * Removes a specified modifier from the cart;. | ||
| 432 | * | ||
| 433 | * @param HTTPRequest | ||
| 434 | * | ||
| 435 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); | ||
| 436 | * If it is not AJAX it redirects back to requesting page. | ||
| 437 | */ | ||
| 438 | public function removemodifier(SS_HTTPRequest $request) | ||
| 447 | |||
| 448 | /** | ||
| 449 | * Adds a specified modifier to the cart;. | ||
| 450 | * | ||
| 451 | * @param HTTPRequest | ||
| 452 | * | ||
| 453 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); | ||
| 454 | * If it is not AJAX it redirects back to requesting page. | ||
| 455 | */ | ||
| 456 | public function addmodifier(SS_HTTPRequest $request) | ||
| 465 | |||
| 466 | /** | ||
| 467 | * sets the country. | ||
| 468 | * | ||
| 469 | * @param SS_HTTPRequest | ||
| 470 | * | ||
| 471 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); | ||
| 472 | * If it is not AJAX it redirects back to requesting page. | ||
| 473 | **/ | ||
| 474 | public function setcountry(SS_HTTPRequest $request) | ||
| 482 | |||
| 483 | /** | ||
| 484 | * @param SS_HTTPRequest | ||
| 485 | * | ||
| 486 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); | ||
| 487 | * If it is not AJAX it redirects back to requesting page. | ||
| 488 | **/ | ||
| 489 | public function setregion(SS_HTTPRequest $request) | ||
| 496 | |||
| 497 | /** | ||
| 498 | * @param SS_HTTPRequest | ||
| 499 | * | ||
| 500 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); | ||
| 501 | * If it is not AJAX it redirects back to requesting page. | ||
| 502 | **/ | ||
| 503 | public function setcurrency(SS_HTTPRequest $request) | ||
| 510 | |||
| 511 | /** | ||
| 512 | * @param SS_HTTPRequest | ||
| 513 | * | ||
| 514 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); | ||
| 515 | * If it is not AJAX it redirects back to requesting page. | ||
| 516 | **/ | ||
| 517 | public function removefromsale(SS_HTTPRequest $request) | ||
| 538 | |||
| 539 | /** | ||
| 540 | * @param SS_HTTPRequest | ||
| 541 | * | ||
| 542 | * @return mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); | ||
| 543 | * If it is not AJAX it redirects back to requesting page. | ||
| 544 | **/ | ||
| 545 | public function save(SS_HTTPRequest $request) | ||
| 551 | |||
| 552 | /** | ||
| 553 | * @param SS_HTTPRequest | ||
| 554 | * | ||
| 555 | * @return REDIRECT | ||
| 556 | **/ | ||
| 557 | public function clear(SS_HTTPRequest $request) | ||
| 564 | |||
| 565 | /** | ||
| 566 | * @param SS_HTTPRequest | ||
| 567 | * | ||
| 568 | * @return REDIRECT | ||
| 569 | **/ | ||
| 570 | public function clearandlogout(SS_HTTPRequest $request) | ||
| 580 | |||
| 581 | /** | ||
| 582 | * @param SS_HTTPRequest | ||
| 583 | * | ||
| 584 | * @return REDIRECT | ||
| 585 | **/ | ||
| 586 | public function deleteorder(SS_HTTPRequest $request) | ||
| 596 | |||
| 597 | public function copyorder($request) | ||
| 606 | |||
| 607 | /** | ||
| 608 | * return number of items in cart. | ||
| 609 | * | ||
| 610 | * @param SS_HTTPRequest | ||
| 611 | * | ||
| 612 | * @return int | ||
| 613 | **/ | ||
| 614 | public function numberofitemsincart(SS_HTTPRequest $request) | ||
| 620 | |||
| 621 | /** | ||
| 622 | * return cart for ajax call. | ||
| 623 | * | ||
| 624 | * @param SS_HTTPRequest | ||
| 625 | * | ||
| 626 | * @return HTML | ||
| 627 | */ | ||
| 628 | public function showcart(SS_HTTPRequest $request) | ||
| 632 | |||
| 633 | /** | ||
| 634 | * loads an order. | ||
| 635 | * | ||
| 636 | * @param SS_HTTPRequest | ||
| 637 | * | ||
| 638 | * @return REDIRECT | ||
| 639 | */ | ||
| 640 | public function loadorder(SS_HTTPRequest $request) | ||
| 650 | |||
| 651 | /** | ||
| 652 | * remove address from list of available addresses in checkout. | ||
| 653 | * | ||
| 654 | * @param SS_HTTPRequest | ||
| 655 | * | ||
| 656 | * @return string | REDIRECT | ||
| 657 | * @TODO: add non-ajax version of this request. | ||
| 658 | */ | ||
| 659 | public function removeaddress(SS_HTTPRequest $request) | ||
| 685 | |||
| 686 | /** | ||
| 687 | * allows us to view out-dated buyables that have been deleted | ||
| 688 | * where only old versions exist. | ||
| 689 | * this method should redirect. | ||
| 690 | * | ||
| 691 | * @param SS_HTTPRequest | ||
| 692 | * | ||
| 693 | * @return REDIRECT | ||
| 694 | */ | ||
| 695 | public function submittedbuyable(SS_HTTPRequest $request) | ||
| 728 | |||
| 729 | /** | ||
| 730 | * This can be used by admins to log in as customers | ||
| 731 | * to place orders on their behalf... | ||
| 732 | * | ||
| 733 | * @param SS_HTTPRequest | ||
| 734 | * | ||
| 735 | * @return REDIRECT | ||
| 736 | */ | ||
| 737 | public function placeorderformember(SS_HTTPRequest $request) | ||
| 758 | |||
| 759 | /** | ||
| 760 | * This can be used by admins to log in as customers | ||
| 761 | * to place orders on their behalf... | ||
| 762 | * | ||
| 763 | * @param SS_HTTPRequest | ||
| 764 | * | ||
| 765 | * @return REDIRECT | ||
| 766 | */ | ||
| 767 | public function loginas(SS_HTTPRequest $request) | ||
| 786 | |||
| 787 | /** | ||
| 788 | * Helper function used by link functions | ||
| 789 | * Creates the appropriate url-encoded string parameters for links from array. | ||
| 790 | * | ||
| 791 | * Produces string such as: MyParam%3D11%26OtherParam%3D1 | ||
| 792 | * ...which decodes to: MyParam=11&OtherParam=1 | ||
| 793 | * | ||
| 794 | * you will need to decode the url with javascript before using it. | ||
| 795 | * | ||
| 796 | * @todo: check that comment description actually matches what it does | ||
| 797 | * | ||
| 798 | * @return string (URLSegment) | ||
| 799 | */ | ||
| 800 | protected static function params_to_get_string(array $array) | ||
| 809 | |||
| 810 | /** | ||
| 811 | * Gets a buyable object based on URL actions. | ||
| 812 | * | ||
| 813 | * @return DataObject | Null - returns buyable | ||
| 814 | */ | ||
| 815 | protected function buyable() | ||
| 836 | |||
| 837 | /** | ||
| 838 | * Gets the requested quantity. | ||
| 839 | * | ||
| 840 | * @return float | ||
| 841 | */ | ||
| 842 | protected function quantity() | ||
| 851 | |||
| 852 | /** | ||
| 853 | * Gets the request parameters. | ||
| 854 | * | ||
| 855 | * @param $getpost - choose between obtaining the chosen parameters from GET or POST | ||
| 856 | * | ||
| 857 | * @return array | ||
| 858 | */ | ||
| 859 | protected function parameters($getpost = 'GET') | ||
| 863 | |||
| 864 | protected function goToErrorPage() | ||
| 875 | |||
| 876 | /** | ||
| 877 | * Handy debugging action visit. | ||
| 878 | * Log in as an administrator and visit mysite/shoppingcart/debug. | ||
| 879 | */ | ||
| 880 | public function debug() | ||
| 889 | |||
| 890 | /** | ||
| 891 | * test the ajax response | ||
| 892 | * for developers only. | ||
| 893 | * | ||
| 894 | * @return output to buffer | ||
| 895 | */ | ||
| 896 | public function ajaxtest(SS_HTTPRequest $request) | ||
| 916 | } | ||
| 917 | 
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.