Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Cart 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 Cart, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Darryldecode\Cart; |
||
12 | class Cart |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * the item storage |
||
17 | * |
||
18 | * @var |
||
19 | */ |
||
20 | protected $session; |
||
21 | |||
22 | /** |
||
23 | * the event dispatcher |
||
24 | * |
||
25 | * @var |
||
26 | */ |
||
27 | protected $events; |
||
28 | |||
29 | /** |
||
30 | * the cart session key |
||
31 | * |
||
32 | * @var |
||
33 | */ |
||
34 | protected $instanceName; |
||
35 | |||
36 | /** |
||
37 | * the session key use for the cart |
||
38 | * |
||
39 | * @var |
||
40 | */ |
||
41 | protected $sessionKey; |
||
42 | |||
43 | /** |
||
44 | * the session key use to persist cart items |
||
45 | * |
||
46 | * @var |
||
47 | */ |
||
48 | protected $sessionKeyCartItems; |
||
49 | |||
50 | /** |
||
51 | * the session key use to persist cart conditions |
||
52 | * |
||
53 | * @var |
||
54 | */ |
||
55 | protected $sessionKeyCartConditions; |
||
56 | |||
57 | /** |
||
58 | * Configuration to pass to ItemCollection |
||
59 | * |
||
60 | * @var |
||
61 | */ |
||
62 | protected $config; |
||
63 | |||
64 | /** |
||
65 | * our object constructor |
||
66 | * |
||
67 | * @param $session |
||
68 | * @param $events |
||
69 | * @param $instanceName |
||
70 | * @param $session_key |
||
71 | * @param $config |
||
72 | */ |
||
73 | public function __construct($session, $events, $instanceName, $session_key, $config) |
||
84 | |||
85 | /** |
||
86 | * sets the session key |
||
87 | * |
||
88 | * @param string $sessionKey the session key or identifier |
||
89 | * @return $this|bool |
||
90 | * @throws \Exception |
||
91 | */ |
||
92 | public function session($sessionKey) |
||
102 | |||
103 | /** |
||
104 | * get instance name of the cart |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function getInstanceName() |
||
112 | |||
113 | /** |
||
114 | * get an item on a cart by item ID |
||
115 | * |
||
116 | * @param $itemId |
||
117 | * @return mixed |
||
118 | */ |
||
119 | public function get($itemId) |
||
123 | |||
124 | /** |
||
125 | * check if an item exists by item ID |
||
126 | * |
||
127 | * @param $itemId |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function has($itemId) |
||
134 | |||
135 | /** |
||
136 | * add item to the cart, it can be an array or multi dimensional array |
||
137 | * |
||
138 | * @param string|array $id |
||
139 | * @param string $name |
||
140 | * @param float $price |
||
141 | * @param int $quantity |
||
142 | * @param array $attributes |
||
143 | * @param CartCondition|array $conditions |
||
144 | * @return $this |
||
145 | * @throws InvalidItemException |
||
146 | */ |
||
147 | public function add($id, $name = null, $price = null, $quantity = null, $attributes = array(), $conditions = array()) |
||
204 | |||
205 | /** |
||
206 | * update a cart |
||
207 | * |
||
208 | * @param $id |
||
209 | * @param $data |
||
210 | * |
||
211 | * the $data will be an associative array, you don't need to pass all the data, only the key value |
||
212 | * of the item you want to update on it |
||
213 | * @return bool |
||
214 | */ |
||
215 | public function update($id, $data) |
||
260 | |||
261 | /** |
||
262 | * add condition on an existing item on the cart |
||
263 | * |
||
264 | * @param int|string $productId |
||
265 | * @param CartCondition $itemCondition |
||
266 | * @return $this |
||
267 | */ |
||
268 | public function addItemCondition($productId, $itemCondition) |
||
294 | |||
295 | /** |
||
296 | * removes an item on cart by item ID |
||
297 | * |
||
298 | * @param $id |
||
299 | * @return bool |
||
300 | */ |
||
301 | public function remove($id) |
||
316 | |||
317 | /** |
||
318 | * clear cart |
||
319 | * @return bool |
||
320 | */ |
||
321 | public function clear() |
||
335 | |||
336 | /** |
||
337 | * add a condition on the cart |
||
338 | * |
||
339 | * @param CartCondition|array $condition |
||
340 | * @return $this |
||
341 | * @throws InvalidConditionException |
||
342 | */ |
||
343 | public function condition($condition) |
||
373 | |||
374 | /** |
||
375 | * get conditions applied on the cart |
||
376 | * |
||
377 | * @return CartConditionCollection |
||
378 | */ |
||
379 | public function getConditions() |
||
383 | |||
384 | /** |
||
385 | * get condition applied on the cart by its name |
||
386 | * |
||
387 | * @param $conditionName |
||
388 | * @return CartCondition |
||
389 | */ |
||
390 | public function getCondition($conditionName) |
||
394 | |||
395 | /** |
||
396 | * Get all the condition filtered by Type |
||
397 | * Please Note that this will only return condition added on cart bases, not those conditions added |
||
398 | * specifically on an per item bases |
||
399 | * |
||
400 | * @param $type |
||
401 | * @return CartConditionCollection |
||
402 | */ |
||
403 | public function getConditionsByType($type) |
||
409 | |||
410 | |||
411 | /** |
||
412 | * Remove all the condition with the $type specified |
||
413 | * Please Note that this will only remove condition added on cart bases, not those conditions added |
||
414 | * specifically on an per item bases |
||
415 | * |
||
416 | * @param $type |
||
417 | * @return $this |
||
418 | */ |
||
419 | public function removeConditionsByType($type) |
||
425 | |||
426 | |||
427 | /** |
||
428 | * removes a condition on a cart by condition name, |
||
429 | * this can only remove conditions that are added on cart bases not conditions that are added on an item/product. |
||
430 | * If you wish to remove a condition that has been added for a specific item/product, you may |
||
431 | * use the removeItemCondition(itemId, conditionName) method instead. |
||
432 | * |
||
433 | * @param $conditionName |
||
434 | * @return void |
||
435 | */ |
||
436 | public function removeCartCondition($conditionName) |
||
444 | |||
445 | /** |
||
446 | * remove a condition that has been applied on an item that is already on the cart |
||
447 | * |
||
448 | * @param $itemId |
||
449 | * @param $conditionName |
||
450 | * @return bool |
||
451 | */ |
||
452 | public function removeItemCondition($itemId, $conditionName) |
||
502 | |||
503 | /** |
||
504 | * remove all conditions that has been applied on an item that is already on the cart |
||
505 | * |
||
506 | * @param $itemId |
||
507 | * @return bool |
||
508 | */ |
||
509 | public function clearItemConditions($itemId) |
||
521 | |||
522 | /** |
||
523 | * clears all conditions on a cart, |
||
524 | * this does not remove conditions that has been added specifically to an item/product. |
||
525 | * If you wish to remove a specific condition to a product, you may use the method: removeItemCondition($itemId, $conditionName) |
||
526 | * |
||
527 | * @return void |
||
528 | */ |
||
529 | public function clearCartConditions() |
||
536 | |||
537 | /** |
||
538 | * get cart sub total without conditions |
||
539 | * @param bool $formatted |
||
540 | * @return float |
||
541 | */ |
||
542 | public function getSubTotalWithoutConditions($formatted = true) |
||
552 | |||
553 | /** |
||
554 | * get cart sub total |
||
555 | * @param bool $formatted |
||
556 | * @return float |
||
557 | */ |
||
558 | public function getSubTotal($formatted = true) |
||
594 | |||
595 | /** |
||
596 | * the new total in which conditions are already applied |
||
597 | * |
||
598 | * @return float |
||
599 | */ |
||
600 | public function getTotal() |
||
631 | |||
632 | /** |
||
633 | * get total quantity of items in the cart |
||
634 | * |
||
635 | * @return int |
||
636 | */ |
||
637 | public function getTotalQuantity() |
||
649 | |||
650 | /** |
||
651 | * get total quantity of items individual in the cart |
||
652 | * |
||
653 | * @return int |
||
654 | */ |
||
655 | public function getTotalQuantityIndividual() |
||
661 | |||
662 | /** |
||
663 | * get the cart |
||
664 | * |
||
665 | * @return CartCollection |
||
666 | */ |
||
667 | public function getContent() |
||
671 | |||
672 | /** |
||
673 | * check if cart is empty |
||
674 | * |
||
675 | * @return bool |
||
676 | */ |
||
677 | public function isEmpty() |
||
683 | |||
684 | /** |
||
685 | * validate Item data |
||
686 | * |
||
687 | * @param $item |
||
688 | * @return array $item; |
||
689 | * @throws InvalidItemException |
||
690 | */ |
||
691 | View Code Duplication | protected function validate($item) |
|
708 | |||
709 | /** |
||
710 | * add row to cart collection |
||
711 | * |
||
712 | * @param $id |
||
713 | * @param $item |
||
714 | * @return bool |
||
715 | */ |
||
716 | protected function addRow($id, $item) |
||
732 | |||
733 | /** |
||
734 | * save the cart |
||
735 | * |
||
736 | * @param $cart CartCollection |
||
737 | */ |
||
738 | protected function save($cart) |
||
742 | |||
743 | /** |
||
744 | * save the cart conditions |
||
745 | * |
||
746 | * @param $conditions |
||
747 | */ |
||
748 | protected function saveConditions($conditions) |
||
752 | |||
753 | /** |
||
754 | * check if an item has condition |
||
755 | * |
||
756 | * @param $item |
||
757 | * @return bool |
||
758 | */ |
||
759 | protected function itemHasConditions($item) |
||
773 | |||
774 | /** |
||
775 | * update a cart item quantity relative to its current quantity |
||
776 | * |
||
777 | * @param $item |
||
778 | * @param $key |
||
779 | * @param $value |
||
780 | * @return mixed |
||
781 | */ |
||
782 | protected function updateQuantityRelative($item, $key, $value) |
||
800 | |||
801 | /** |
||
802 | * update cart item quantity not relative to its current quantity value |
||
803 | * |
||
804 | * @param $item |
||
805 | * @param $key |
||
806 | * @param $value |
||
807 | * @return mixed |
||
808 | */ |
||
809 | protected function updateQuantityNotRelative($item, $key, $value) |
||
815 | |||
816 | /** |
||
817 | * Setter for decimals. Change value on demand. |
||
818 | * @param $decimals |
||
819 | */ |
||
820 | public function setDecimals($decimals) |
||
824 | |||
825 | /** |
||
826 | * Setter for decimals point. Change value on demand. |
||
827 | * @param $dec_point |
||
828 | */ |
||
829 | public function setDecPoint($dec_point) |
||
833 | |||
834 | public function setThousandsSep($thousands_sep) |
||
838 | |||
839 | /** |
||
840 | * @param $name |
||
841 | * @param $value |
||
842 | * @return mixed |
||
843 | */ |
||
844 | protected function fireEvent($name, $value = []) |
||
848 | } |
||
849 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.