Complex classes like ShoppingCart 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, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class ShoppingCart extends Object |
||
38 | { |
||
39 | /** |
||
40 | * List of names that can be used as session variables. |
||
41 | * Also @see ShoppingCart::sessionVariableName. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private static $session_variable_names = array('OrderID', 'Messages'); |
||
46 | |||
47 | /** |
||
48 | * This is where we hold the (singleton) Shoppingcart. |
||
49 | * |
||
50 | * @var object (ShoppingCart) |
||
51 | */ |
||
52 | private static $_singletoncart = null; |
||
53 | |||
54 | /** |
||
55 | * Feedback message to user (e.g. cart updated, could not delete item, someone in standing behind you). |
||
56 | * |
||
57 | *@var array |
||
58 | **/ |
||
59 | protected $messages = array(); |
||
60 | |||
61 | /** |
||
62 | * stores a reference to the current order object. |
||
63 | * |
||
64 | * @var object |
||
65 | **/ |
||
66 | protected $order = null; |
||
67 | |||
68 | /** |
||
69 | * This variable is set to YES when we actually need an order (i.e. write it). |
||
70 | * |
||
71 | * @var bool |
||
72 | */ |
||
73 | protected $requireSavedOrder = false; |
||
74 | |||
75 | /** |
||
76 | * Allows access to the cart from anywhere in code. |
||
77 | * |
||
78 | * @return ShoppingCart Object |
||
79 | */ |
||
80 | public static function singleton() |
||
88 | |||
89 | /** |
||
90 | * Allows access to the current order from anywhere in the code.. |
||
91 | * |
||
92 | * if you do not like the session Order then you can set it here ... |
||
93 | * |
||
94 | * @param Order $order (optional) |
||
|
|||
95 | * |
||
96 | * @return Order |
||
97 | */ |
||
98 | public static function current_order($order = null) |
||
102 | |||
103 | /** |
||
104 | * useful when the order has been updated ... |
||
105 | */ |
||
106 | public static function reset_order_reference() |
||
110 | |||
111 | /** |
||
112 | * looks up current order id. |
||
113 | * you may supply an ID here, so that it looks up the current order ID |
||
114 | * only when none is supplied. |
||
115 | * |
||
116 | * @param int (optional) | Order $orderOrOrderID |
||
117 | * |
||
118 | * @return int; |
||
119 | */ |
||
120 | public static function current_order_id($orderOrOrderID = 0) |
||
137 | |||
138 | /** |
||
139 | * Allows access to the current order from anywhere in the code.. |
||
140 | * |
||
141 | * @return Order |
||
142 | */ |
||
143 | public static function session_order() |
||
150 | |||
151 | /** |
||
152 | * set a specific order, other than the one from session .... |
||
153 | * |
||
154 | * @param Order $order |
||
155 | * |
||
156 | * @return Order |
||
157 | */ |
||
158 | public function setOrder($order) |
||
163 | |||
164 | /** |
||
165 | * Gets or creates the current order. |
||
166 | * Based on the session ONLY unless the order has been explictely set. |
||
167 | * IMPORTANT FUNCTION! |
||
168 | * |
||
169 | * returns null if the current user does not allow order manipulation or saving (e.g. session disabled) |
||
170 | * |
||
171 | * However, you can pass an order in case you want to manipulate an order that is not in sesssion |
||
172 | * |
||
173 | * @param int $recurseCount (optional) |
||
174 | * @param Order $order (optional) |
||
175 | * |
||
176 | * @return Order |
||
177 | */ |
||
178 | public function currentOrder($recurseCount = 0, $order = null) |
||
313 | |||
314 | |||
315 | private static $_allow_writes_cache = null; |
||
316 | |||
317 | /** |
||
318 | * can the current user use sessions and therefore write to cart??? |
||
319 | * the method also returns if an order has explicitely been set |
||
320 | * @return bool |
||
321 | */ |
||
322 | protected function allowWrites() |
||
342 | |||
343 | /** |
||
344 | * Allows access to the current order from anywhere in the code.. |
||
345 | * |
||
346 | * @param Order $order (optional) |
||
347 | * |
||
348 | * @return ShoppingCart Object |
||
349 | */ |
||
350 | public function Link($order = null) |
||
357 | |||
358 | /** |
||
359 | * Adds any number of items to the cart. |
||
360 | * Returns the order item on succes OR false on failure. |
||
361 | * |
||
362 | * @param DataObject $buyable - the buyable (generally a product) being added to the cart |
||
363 | * @param float $quantity - number of items add. |
||
364 | * @param mixed $parameters - array of parameters to target a specific order item. eg: group=1, length=5 |
||
365 | * if you make it a form, it will save the form into the orderitem |
||
366 | * returns null if the current user does not allow order manipulation or saving (e.g. session disabled) |
||
367 | * |
||
368 | * @return false | DataObject (OrderItem) |
||
369 | */ |
||
370 | public function addBuyable(BuyableModel $buyable, $quantity = 1, $parameters = array()) |
||
404 | |||
405 | /** |
||
406 | * Sets quantity for an item in the cart. |
||
407 | * |
||
408 | * returns null if the current user does not allow order manipulation or saving (e.g. session disabled) |
||
409 | * |
||
410 | * @param DataObject $buyable - the buyable (generally a product) being added to the cart |
||
411 | * @param float $quantity - number of items add. |
||
412 | * @param array $parameters - array of parameters to target a specific order item. eg: group=1, length=5 |
||
413 | * |
||
414 | * @return false | DataObject (OrderItem) | null |
||
415 | */ |
||
416 | public function setQuantity(BuyableModel $buyable, $quantity, array $parameters = array()) |
||
434 | |||
435 | /** |
||
436 | * Removes any number of items from the cart. |
||
437 | * |
||
438 | * returns null if the current user does not allow order manipulation or saving (e.g. session disabled) |
||
439 | * |
||
440 | * @param DataObject $buyable - the buyable (generally a product) being added to the cart |
||
441 | * @param float $quantity - number of items add. |
||
442 | * @param array $parameters - array of parameters to target a specific order item. eg: group=1, length=5 |
||
443 | * |
||
444 | * @return false | OrderItem | null |
||
445 | */ |
||
446 | public function decrementBuyable(BuyableModel $buyable, $quantity = 1, array $parameters = array()) |
||
472 | |||
473 | /** |
||
474 | * Delete item from the cart. |
||
475 | * |
||
476 | * returns null if the current user does not allow order manipulation or saving (e.g. session disabled) |
||
477 | * |
||
478 | * @param OrderItem $buyable - the buyable (generally a product) being added to the cart |
||
479 | * @param array $parameters - array of parameters to target a specific order item. eg: group=1, length=5 |
||
480 | * |
||
481 | * @return bool | item | null - successfully removed |
||
482 | */ |
||
483 | public function deleteBuyable(BuyableModel $buyable, array $parameters = array()) |
||
501 | |||
502 | /** |
||
503 | * Checks and prepares variables for a quantity change (add, edit, remove) for an Order Item. |
||
504 | * |
||
505 | * @param DataObject $buyable - the buyable (generally a product) being added to the cart |
||
506 | * @param float $quantity - number of items add. |
||
507 | * @param bool $mustBeExistingItems - if false, the Order Item gets created if it does not exist - if TRUE the order item is searched for and an error shows if there is no Order item. |
||
508 | * @param array | Form $parameters - array of parameters to target a specific order item. eg: group=1, length=5* |
||
509 | * - form saved into item... |
||
510 | * |
||
511 | * @return bool | DataObject ($orderItem) |
||
512 | */ |
||
513 | public function prepareOrderItem(BuyableModel $buyable, $parameters = array(), $mustBeExistingItem = true) |
||
543 | |||
544 | /** |
||
545 | * @todo: what does this method do??? |
||
546 | * |
||
547 | * |
||
548 | * @param DataObject ($buyable) |
||
549 | * @param float $quantity |
||
550 | * |
||
551 | * @return int |
||
552 | */ |
||
553 | public function prepareQuantity(BuyableModel $buyable, $quantity) |
||
566 | |||
567 | /** |
||
568 | * Helper function for making / retrieving order items. |
||
569 | * we do not need things like "canPurchase" here, because that is with the "addBuyable" method. |
||
570 | * NOTE: does not write! |
||
571 | * |
||
572 | * @param DataObject $buyable |
||
573 | * @param array $parameters |
||
574 | * |
||
575 | * @return OrderItem |
||
576 | */ |
||
577 | public function findOrMakeItem(BuyableModel $buyable, array $parameters = array()) |
||
609 | |||
610 | /** |
||
611 | * submit the order so that it is no longer available |
||
612 | * in the cart but will continue its journey through the |
||
613 | * order steps. |
||
614 | * |
||
615 | * @return bool |
||
616 | */ |
||
617 | public function submit() |
||
630 | |||
631 | /** |
||
632 | * returns null if the current user does not allow order manipulation or saving (e.g. session disabled) |
||
633 | * |
||
634 | * @return bool | null |
||
635 | */ |
||
636 | public function save() |
||
645 | |||
646 | /** |
||
647 | * Clears the cart contents completely by removing the orderID from session, and |
||
648 | * thus creating a new cart on next request. |
||
649 | * |
||
650 | * @return bool |
||
651 | */ |
||
652 | public function clear() |
||
679 | |||
680 | /** |
||
681 | * alias for clear. |
||
682 | */ |
||
683 | public function reset() |
||
687 | |||
688 | /** |
||
689 | * Removes a modifier from the cart |
||
690 | * It does not actually remove it, but it just |
||
691 | * sets it as "removed", to avoid that it is being |
||
692 | * added again. |
||
693 | * |
||
694 | * returns null if the current user does not allow order manipulation or saving (e.g. session disabled) |
||
695 | * |
||
696 | * @param OrderModifier $modifier |
||
697 | * |
||
698 | * @return bool | null |
||
699 | */ |
||
700 | public function removeModifier(OrderModifier $modifier) |
||
723 | |||
724 | /** |
||
725 | * Removes a modifier from the cart. |
||
726 | * |
||
727 | * returns null if the current user does not allow order manipulation or saving (e.g. session disabled) |
||
728 | * |
||
729 | * @param Int/ OrderModifier |
||
730 | * |
||
731 | * @return bool |
||
732 | */ |
||
733 | public function addModifier($modifier) |
||
753 | |||
754 | /** |
||
755 | * Sets an order as the current order. |
||
756 | * |
||
757 | * @param int | Order $order |
||
758 | * |
||
759 | * @return bool |
||
760 | */ |
||
761 | public function loadOrder($order) |
||
799 | |||
800 | /** |
||
801 | * NOTE: tried to copy part to the Order Class - but that was not much of a go-er. |
||
802 | * |
||
803 | * returns null if the current user does not allow order manipulation or saving (e.g. session disabled) |
||
804 | * |
||
805 | * @param int | Order $order |
||
806 | * |
||
807 | * @return Order | false | null |
||
808 | **/ |
||
809 | public function copyOrder($oldOrder) |
||
853 | |||
854 | /** |
||
855 | * |
||
856 | * @param Order $oldOrder |
||
857 | * @param Order $newOrder |
||
858 | * |
||
859 | * @return Ordeer (the new order) |
||
860 | */ |
||
861 | public function CopyOrderOnly($oldOrder, $newOrder) |
||
877 | |||
878 | /** |
||
879 | * add buyables into new Order |
||
880 | * |
||
881 | * @param Order $newOrder |
||
882 | * @param array $buyables can also be another iterable object (e.g. ArrayList) |
||
883 | * @param array $parameters |
||
884 | * |
||
885 | * @return Order (same order as was passed) |
||
886 | */ |
||
887 | public function CopyBuyablesToNewOrder($newOrder, $buyables, $parameters = []) |
||
904 | |||
905 | /** |
||
906 | * sets country in order so that modifiers can be recalculated, etc... |
||
907 | * |
||
908 | * @param string - $countryCode |
||
909 | * |
||
910 | * @return bool |
||
911 | **/ |
||
912 | public function setCountry($countryCode) |
||
927 | |||
928 | /** |
||
929 | * sets region in order so that modifiers can be recalculated, etc... |
||
930 | * |
||
931 | * @param int | String - $regionID you can use the ID or the code. |
||
932 | * |
||
933 | * @return bool |
||
934 | **/ |
||
935 | public function setRegion($regionID) |
||
948 | |||
949 | /** |
||
950 | * sets the display currency for the cart. |
||
951 | * |
||
952 | * @param string $currencyCode |
||
953 | * |
||
954 | * @return bool |
||
955 | **/ |
||
956 | public function setCurrency($currencyCode) |
||
978 | |||
979 | /** |
||
980 | * Produces a debug of the shopping cart. |
||
981 | */ |
||
982 | public function debug() |
||
1070 | |||
1071 | /** |
||
1072 | * Stores a message that can later be returned via ajax or to $form->sessionMessage();. |
||
1073 | * |
||
1074 | * @param $message - the message, which could be a notification of successful action, or reason for failure |
||
1075 | * @param $type - please use good, bad, warning |
||
1076 | */ |
||
1077 | public function addMessage($message, $status = 'good') |
||
1092 | |||
1093 | /******************************************************* |
||
1094 | * HELPER FUNCTIONS |
||
1095 | *******************************************************/ |
||
1096 | |||
1097 | /** |
||
1098 | * Gets an existing order item based on buyable and passed parameters. |
||
1099 | * |
||
1100 | * @param DataObject $buyable |
||
1101 | * @param array $parameters |
||
1102 | * |
||
1103 | * @return OrderItem | null |
||
1104 | */ |
||
1105 | protected function getExistingItem(BuyableModel $buyable, array $parameters = array()) |
||
1121 | |||
1122 | /** |
||
1123 | * Removes parameters that aren't in the default array, merges with default parameters, and converts raw2SQL. |
||
1124 | * |
||
1125 | * @param array $parameters |
||
1126 | * |
||
1127 | * @return cleaned array |
||
1128 | */ |
||
1129 | protected function cleanParameters(array $params = array()) |
||
1144 | |||
1145 | /** |
||
1146 | * @param array $parameters |
||
1147 | * Converts parameter array to SQL query filter |
||
1148 | */ |
||
1149 | protected function parametersToSQL(array $parameters = array()) |
||
1166 | |||
1167 | /******************************************************* |
||
1168 | * UI MESSAGE HANDLING |
||
1169 | *******************************************************/ |
||
1170 | |||
1171 | /** |
||
1172 | * Retrieves all good, bad, and ugly messages that have been produced during the current request. |
||
1173 | * |
||
1174 | * @return array of messages |
||
1175 | */ |
||
1176 | public function getMessages() |
||
1190 | |||
1191 | /** |
||
1192 | *Saves current messages in session for retrieving them later. |
||
1193 | * |
||
1194 | *@return array of messages |
||
1195 | */ |
||
1196 | protected function StoreMessagesInSession() |
||
1201 | |||
1202 | /** |
||
1203 | * This method is used to return data after an ajax call was made. |
||
1204 | * When a asynchronious request is made to the shopping cart (ajax), |
||
1205 | * then you will first action the request and then use this function |
||
1206 | * to return some values. |
||
1207 | * |
||
1208 | * It can also be used without ajax, in wich case it will redirects back |
||
1209 | * to the last page. |
||
1210 | * |
||
1211 | * Note that you can set the ajax response class in the configuration file. |
||
1212 | * |
||
1213 | * |
||
1214 | * @param string $message |
||
1215 | * @param string $status |
||
1216 | * @param Form $form |
||
1217 | * @returns String (JSON) |
||
1218 | */ |
||
1219 | public function setMessageAndReturn($message = '', $status = '', Form $form = null) |
||
1252 | |||
1253 | /** |
||
1254 | * @return EcommerceDBConfig |
||
1255 | */ |
||
1256 | protected function EcomConfig() |
||
1260 | |||
1261 | /** |
||
1262 | * Return the name of the session variable that should be used. |
||
1263 | * |
||
1264 | * @param string $name |
||
1265 | * |
||
1266 | * @return string |
||
1267 | */ |
||
1268 | protected function sessionVariableName($name = '') |
||
1277 | } |
||
1278 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.