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; |
||
13 | class Cart |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * the item storage |
||
18 | * |
||
19 | * @var |
||
20 | */ |
||
21 | protected $session; |
||
22 | |||
23 | /** |
||
24 | * the event dispatcher |
||
25 | * |
||
26 | * @var |
||
27 | */ |
||
28 | protected $events; |
||
29 | |||
30 | /** |
||
31 | * the cart session key |
||
32 | * |
||
33 | * @var |
||
34 | */ |
||
35 | protected $instanceName; |
||
36 | |||
37 | /** |
||
38 | * the session key use for the cart |
||
39 | * |
||
40 | * @var |
||
41 | */ |
||
42 | protected $sessionKey; |
||
43 | |||
44 | /** |
||
45 | * the session key use to persist cart items |
||
46 | * |
||
47 | * @var |
||
48 | */ |
||
49 | protected $sessionKeyCartItems; |
||
50 | |||
51 | /** |
||
52 | * the session key use to persist cart conditions |
||
53 | * |
||
54 | * @var |
||
55 | */ |
||
56 | protected $sessionKeyCartConditions; |
||
57 | |||
58 | /** |
||
59 | * Configuration to pass to ItemCollection |
||
60 | * |
||
61 | * @var |
||
62 | */ |
||
63 | protected $config; |
||
64 | |||
65 | /** |
||
66 | * This holds the currently added item id in cart for association |
||
67 | * |
||
68 | * @var |
||
69 | */ |
||
70 | protected $currentItemId; |
||
71 | |||
72 | /** |
||
73 | * our object constructor |
||
74 | * |
||
75 | * @param $session |
||
76 | * @param $events |
||
77 | * @param $instanceName |
||
78 | * @param $session_key |
||
79 | * @param $config |
||
80 | */ |
||
81 | public function __construct($session, $events, $instanceName, $session_key, $config) |
||
93 | |||
94 | /** |
||
95 | * sets the session key |
||
96 | * |
||
97 | * @param string $sessionKey the session key or identifier |
||
98 | * @return $this|bool |
||
99 | * @throws \Exception |
||
100 | */ |
||
101 | public function session($sessionKey) |
||
111 | |||
112 | /** |
||
113 | * get instance name of the cart |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | public function getInstanceName() |
||
121 | |||
122 | /** |
||
123 | * get an item on a cart by item ID |
||
124 | * |
||
125 | * @param $itemId |
||
126 | * @return mixed |
||
127 | */ |
||
128 | public function get($itemId) |
||
132 | |||
133 | /** |
||
134 | * check if an item exists by item ID |
||
135 | * |
||
136 | * @param $itemId |
||
137 | * @return bool |
||
138 | */ |
||
139 | public function has($itemId) |
||
143 | |||
144 | /** |
||
145 | * add item to the cart, it can be an array or multi dimensional array |
||
146 | * |
||
147 | * @param string|array $id |
||
148 | * @param string $name |
||
149 | * @param float $price |
||
150 | * @param int $quantity |
||
151 | * @param array $attributes |
||
152 | * @param CartCondition|array $conditions |
||
153 | * @param string $associatedModel |
||
154 | * @return $this |
||
155 | * @throws InvalidItemException |
||
156 | */ |
||
157 | public function add($id, $name = null, $price = null, $quantity = null, $attributes = array(), $conditions = array(), $associatedModel = null) |
||
223 | |||
224 | /** |
||
225 | * update a cart |
||
226 | * |
||
227 | * @param $id |
||
228 | * @param array $data |
||
229 | * |
||
230 | * the $data will be an associative array, you don't need to pass all the data, only the key value |
||
231 | * of the item you want to update on it |
||
232 | * @return bool |
||
233 | */ |
||
234 | public function update($id, $data) |
||
279 | |||
280 | /** |
||
281 | * add condition on an existing item on the cart |
||
282 | * |
||
283 | * @param int|string $productId |
||
284 | * @param CartCondition $itemCondition |
||
285 | * @return $this |
||
286 | */ |
||
287 | public function addItemCondition($productId, $itemCondition) |
||
313 | |||
314 | /** |
||
315 | * removes an item on cart by item ID |
||
316 | * |
||
317 | * @param $id |
||
318 | * @return bool |
||
319 | */ |
||
320 | public function remove($id) |
||
335 | |||
336 | /** |
||
337 | * clear cart |
||
338 | * @return bool |
||
339 | */ |
||
340 | public function clear() |
||
354 | |||
355 | /** |
||
356 | * add a condition on the cart |
||
357 | * |
||
358 | * @param CartCondition|array $condition |
||
359 | * @return $this |
||
360 | * @throws InvalidConditionException |
||
361 | */ |
||
362 | public function condition($condition) |
||
392 | |||
393 | /** |
||
394 | * get conditions applied on the cart |
||
395 | * |
||
396 | * @return CartConditionCollection |
||
397 | */ |
||
398 | public function getConditions() |
||
402 | |||
403 | /** |
||
404 | * get condition applied on the cart by its name |
||
405 | * |
||
406 | * @param $conditionName |
||
407 | * @return CartCondition |
||
408 | */ |
||
409 | public function getCondition($conditionName) |
||
413 | |||
414 | /** |
||
415 | * Get all the condition filtered by Type |
||
416 | * Please Note that this will only return condition added on cart bases, not those conditions added |
||
417 | * specifically on an per item bases |
||
418 | * |
||
419 | * @param $type |
||
420 | * @return CartConditionCollection |
||
421 | */ |
||
422 | public function getConditionsByType($type) |
||
428 | |||
429 | |||
430 | /** |
||
431 | * Remove all the condition with the $type specified |
||
432 | * Please Note that this will only remove condition added on cart bases, not those conditions added |
||
433 | * specifically on an per item bases |
||
434 | * |
||
435 | * @param $type |
||
436 | * @return $this |
||
437 | */ |
||
438 | public function removeConditionsByType($type) |
||
444 | |||
445 | |||
446 | /** |
||
447 | * removes a condition on a cart by condition name, |
||
448 | * this can only remove conditions that are added on cart bases not conditions that are added on an item/product. |
||
449 | * If you wish to remove a condition that has been added for a specific item/product, you may |
||
450 | * use the removeItemCondition(itemId, conditionName) method instead. |
||
451 | * |
||
452 | * @param $conditionName |
||
453 | * @return void |
||
454 | */ |
||
455 | public function removeCartCondition($conditionName) |
||
463 | |||
464 | /** |
||
465 | * remove a condition that has been applied on an item that is already on the cart |
||
466 | * |
||
467 | * @param $itemId |
||
468 | * @param $conditionName |
||
469 | * @return bool |
||
470 | */ |
||
471 | public function removeItemCondition($itemId, $conditionName) |
||
521 | |||
522 | /** |
||
523 | * remove all conditions that has been applied on an item that is already on the cart |
||
524 | * |
||
525 | * @param $itemId |
||
526 | * @return bool |
||
527 | */ |
||
528 | public function clearItemConditions($itemId) |
||
540 | |||
541 | /** |
||
542 | * clears all conditions on a cart, |
||
543 | * this does not remove conditions that has been added specifically to an item/product. |
||
544 | * If you wish to remove a specific condition to a product, you may use the method: removeItemCondition($itemId, $conditionName) |
||
545 | * |
||
546 | * @return void |
||
547 | */ |
||
548 | public function clearCartConditions() |
||
555 | |||
556 | /** |
||
557 | * get cart sub total without conditions |
||
558 | * @param bool $formatted |
||
559 | * @return float |
||
560 | */ |
||
561 | public function getSubTotalWithoutConditions($formatted = true) |
||
571 | |||
572 | /** |
||
573 | * get cart sub total |
||
574 | * @param bool $formatted |
||
575 | * @return float |
||
576 | */ |
||
577 | public function getSubTotal($formatted = true) |
||
613 | |||
614 | /** |
||
615 | * the new total in which conditions are already applied |
||
616 | * |
||
617 | * @return float |
||
618 | */ |
||
619 | public function getTotal() |
||
649 | |||
650 | /** |
||
651 | * get total quantity of items in the cart |
||
652 | * |
||
653 | * @return int |
||
654 | */ |
||
655 | public function getTotalQuantity() |
||
667 | |||
668 | /** |
||
669 | * get the cart |
||
670 | * |
||
671 | * @return CartCollection |
||
672 | */ |
||
673 | public function getContent() |
||
674 | { |
||
675 | return (new CartCollection($this->session->get($this->sessionKeyCartItems)))->reject(function($item) { |
||
676 | return ! ($item instanceof ItemCollection); |
||
677 | }); |
||
678 | } |
||
679 | |||
680 | /** |
||
681 | * check if cart is empty |
||
682 | * |
||
683 | * @return bool |
||
684 | */ |
||
685 | public function isEmpty() |
||
686 | { |
||
687 | return $this->getContent()->isEmpty(); |
||
688 | } |
||
689 | |||
690 | /** |
||
691 | * validate Item data |
||
692 | * |
||
693 | * @param $item |
||
694 | * @return array $item; |
||
695 | * @throws InvalidItemException |
||
696 | */ |
||
697 | View Code Duplication | protected function validate($item) |
|
714 | |||
715 | /** |
||
716 | * add row to cart collection |
||
717 | * |
||
718 | * @param $id |
||
719 | * @param $item |
||
720 | * @return bool |
||
721 | */ |
||
722 | protected function addRow($id, $item) |
||
738 | |||
739 | /** |
||
740 | * save the cart |
||
741 | * |
||
742 | * @param $cart CartCollection |
||
743 | */ |
||
744 | protected function save($cart) |
||
748 | |||
749 | /** |
||
750 | * save the cart conditions |
||
751 | * |
||
752 | * @param $conditions |
||
753 | */ |
||
754 | protected function saveConditions($conditions) |
||
758 | |||
759 | /** |
||
760 | * check if an item has condition |
||
761 | * |
||
762 | * @param $item |
||
763 | * @return bool |
||
764 | */ |
||
765 | protected function itemHasConditions($item) |
||
779 | |||
780 | /** |
||
781 | * update a cart item quantity relative to its current quantity |
||
782 | * |
||
783 | * @param $item |
||
784 | * @param $key |
||
785 | * @param $value |
||
786 | * @return mixed |
||
787 | */ |
||
788 | protected function updateQuantityRelative($item, $key, $value) |
||
806 | |||
807 | /** |
||
808 | * update cart item quantity not relative to its current quantity value |
||
809 | * |
||
810 | * @param $item |
||
811 | * @param $key |
||
812 | * @param $value |
||
813 | * @return mixed |
||
814 | */ |
||
815 | protected function updateQuantityNotRelative($item, $key, $value) |
||
821 | |||
822 | /** |
||
823 | * Setter for decimals. Change value on demand. |
||
824 | * @param $decimals |
||
825 | */ |
||
826 | public function setDecimals($decimals) |
||
830 | |||
831 | /** |
||
832 | * Setter for decimals point. Change value on demand. |
||
833 | * @param $dec_point |
||
834 | */ |
||
835 | public function setDecPoint($dec_point) |
||
839 | |||
840 | public function setThousandsSep($thousands_sep) |
||
844 | |||
845 | /** |
||
846 | * @param $name |
||
847 | * @param $value |
||
848 | * @return mixed |
||
849 | */ |
||
850 | protected function fireEvent($name, $value = []) |
||
854 | |||
855 | /** |
||
856 | * Associate the cart item with the given id with the given model. |
||
857 | * |
||
858 | * @param string $id |
||
859 | * @param mixed $model |
||
860 | * |
||
861 | * @return void |
||
862 | */ |
||
863 | public function associate($model) |
||
881 | } |
||
882 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.