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 to persist cart items |
||
38 | * |
||
39 | * @var |
||
40 | */ |
||
41 | protected $sessionKeyCartItems; |
||
42 | |||
43 | /** |
||
44 | * the session key use to persist cart conditions |
||
45 | * |
||
46 | * @var |
||
47 | */ |
||
48 | protected $sessionKeyCartConditions; |
||
49 | |||
50 | /** |
||
51 | * Configuration to pass to ItemCollection |
||
52 | * |
||
53 | * @var |
||
54 | */ |
||
55 | protected $config; |
||
56 | |||
57 | /** |
||
58 | * our object constructor |
||
59 | * |
||
60 | * @param $session |
||
61 | * @param $events |
||
62 | * @param $instanceName |
||
63 | * @param $session_key |
||
64 | * @param $config |
||
65 | */ |
||
66 | public function __construct($session, $events, $instanceName, $session_key, $config) |
||
76 | |||
77 | /** |
||
78 | * get instance name of the cart |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getInstanceName() |
||
86 | |||
87 | /** |
||
88 | * get an item on a cart by item ID |
||
89 | * |
||
90 | * @param $itemId |
||
91 | * @return mixed |
||
92 | */ |
||
93 | public function get($itemId) |
||
97 | |||
98 | /** |
||
99 | * check if an item exists by item ID |
||
100 | * |
||
101 | * @param $itemId |
||
102 | * @return bool |
||
103 | */ |
||
104 | public function has($itemId) |
||
108 | |||
109 | /** |
||
110 | * add item to the cart, it can be an array or multi dimensional array |
||
111 | * |
||
112 | * @param string|array $id |
||
113 | * @param string $name |
||
114 | * @param float $price |
||
115 | * @param int $quantity |
||
116 | * @param array $attributes |
||
117 | * @param CartCondition|array $conditions |
||
118 | * @return $this |
||
119 | * @throws InvalidItemException |
||
120 | */ |
||
121 | public function add($id, $name = null, $price = null, $quantity = null, $attributes = array(), $conditions = array()) |
||
178 | |||
179 | /** |
||
180 | * update a cart |
||
181 | * |
||
182 | * @param $id |
||
183 | * @param $data |
||
184 | * |
||
185 | * the $data will be an associative array, you don't need to pass all the data, only the key value |
||
186 | * of the item you want to update on it |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function update($id, $data) |
||
234 | |||
235 | /** |
||
236 | * add condition on an existing item on the cart |
||
237 | * |
||
238 | * @param int|string $productId |
||
239 | * @param CartCondition $itemCondition |
||
240 | * @return $this |
||
241 | */ |
||
242 | public function addItemCondition($productId, $itemCondition) |
||
268 | |||
269 | /** |
||
270 | * removes an item on cart by item ID |
||
271 | * |
||
272 | * @param $id |
||
273 | * @return bool |
||
274 | */ |
||
275 | public function remove($id) |
||
290 | |||
291 | /** |
||
292 | * clear cart |
||
293 | * @return bool |
||
294 | */ |
||
295 | public function clear() |
||
309 | |||
310 | /** |
||
311 | * add a condition on the cart |
||
312 | * |
||
313 | * @param CartCondition|array $condition |
||
314 | * @return $this |
||
315 | * @throws InvalidConditionException |
||
316 | */ |
||
317 | public function condition($condition) |
||
347 | |||
348 | /** |
||
349 | * get conditions applied on the cart |
||
350 | * |
||
351 | * @return CartConditionCollection |
||
352 | */ |
||
353 | public function getConditions() |
||
357 | |||
358 | /** |
||
359 | * get condition applied on the cart by its name |
||
360 | * |
||
361 | * @param $conditionName |
||
362 | * @return CartCondition |
||
363 | */ |
||
364 | public function getCondition($conditionName) |
||
368 | |||
369 | /** |
||
370 | * Get all the condition filtered by Type |
||
371 | * Please Note that this will only return condition added on cart bases, not those conditions added |
||
372 | * specifically on an per item bases |
||
373 | * |
||
374 | * @param $type |
||
375 | * @return CartConditionCollection |
||
376 | */ |
||
377 | public function getConditionsByType($type) |
||
383 | |||
384 | |||
385 | /** |
||
386 | * Remove all the condition with the $type specified |
||
387 | * Please Note that this will only remove condition added on cart bases, not those conditions added |
||
388 | * specifically on an per item bases |
||
389 | * |
||
390 | * @param $type |
||
391 | * @return $this |
||
392 | */ |
||
393 | public function removeConditionsByType($type) |
||
399 | |||
400 | |||
401 | /** |
||
402 | * removes a condition on a cart by condition name, |
||
403 | * this can only remove conditions that are added on cart bases not conditions that are added on an item/product. |
||
404 | * If you wish to remove a condition that has been added for a specific item/product, you may |
||
405 | * use the removeItemCondition(itemId, conditionName) method instead. |
||
406 | * |
||
407 | * @param $conditionName |
||
408 | * @return void |
||
409 | */ |
||
410 | public function removeCartCondition($conditionName) |
||
418 | |||
419 | /** |
||
420 | * remove a condition that has been applied on an item that is already on the cart |
||
421 | * |
||
422 | * @param $itemId |
||
423 | * @param $conditionName |
||
424 | * @return bool |
||
425 | */ |
||
426 | public function removeItemCondition($itemId, $conditionName) |
||
476 | |||
477 | /** |
||
478 | * remove all conditions that has been applied on an item that is already on the cart |
||
479 | * |
||
480 | * @param $itemId |
||
481 | * @return bool |
||
482 | */ |
||
483 | public function clearItemConditions($itemId) |
||
495 | |||
496 | /** |
||
497 | * clears all conditions on a cart, |
||
498 | * this does not remove conditions that has been added specifically to an item/product. |
||
499 | * If you wish to remove a specific condition to a product, you may use the method: removeItemCondition($itemId, $conditionName) |
||
500 | * |
||
501 | * @return void |
||
502 | */ |
||
503 | public function clearCartConditions() |
||
510 | |||
511 | /** |
||
512 | * get cart sub total without conditions |
||
513 | * @param bool $formatted |
||
514 | * @return float |
||
515 | */ |
||
516 | View Code Duplication | public function getSubTotalWithoutConditions($formatted = true) |
|
526 | |||
527 | /** |
||
528 | * get cart sub total |
||
529 | * @param bool $formatted |
||
530 | * @return float |
||
531 | */ |
||
532 | View Code Duplication | public function getSubTotal($formatted = true) |
|
542 | |||
543 | /** |
||
544 | * the new total in which conditions are already applied |
||
545 | * |
||
546 | * @return float |
||
547 | */ |
||
548 | public function getTotal() |
||
579 | |||
580 | /** |
||
581 | * get total quantity of items in the cart |
||
582 | * |
||
583 | * @return int |
||
584 | */ |
||
585 | public function getTotalQuantity() |
||
597 | |||
598 | /** |
||
599 | * get the cart |
||
600 | * |
||
601 | * @return CartCollection |
||
602 | */ |
||
603 | public function getContent() |
||
607 | |||
608 | /** |
||
609 | * check if cart is empty |
||
610 | * |
||
611 | * @return bool |
||
612 | */ |
||
613 | public function isEmpty() |
||
619 | |||
620 | /** |
||
621 | * validate Item data |
||
622 | * |
||
623 | * @param $item |
||
624 | * @return array $item; |
||
625 | * @throws InvalidItemException |
||
626 | */ |
||
627 | View Code Duplication | protected function validate($item) |
|
644 | |||
645 | /** |
||
646 | * add row to cart collection |
||
647 | * |
||
648 | * @param $id |
||
649 | * @param $item |
||
650 | * @return bool |
||
651 | */ |
||
652 | protected function addRow($id, $item) |
||
668 | |||
669 | /** |
||
670 | * save the cart |
||
671 | * |
||
672 | * @param $cart CartCollection |
||
673 | */ |
||
674 | protected function save($cart) |
||
678 | |||
679 | /** |
||
680 | * save the cart conditions |
||
681 | * |
||
682 | * @param $conditions |
||
683 | */ |
||
684 | protected function saveConditions($conditions) |
||
688 | |||
689 | /** |
||
690 | * check if an item has condition |
||
691 | * |
||
692 | * @param $item |
||
693 | * @return bool |
||
694 | */ |
||
695 | protected function itemHasConditions($item) |
||
709 | |||
710 | /** |
||
711 | * update a cart item quantity relative to its current quantity |
||
712 | * |
||
713 | * @param $item |
||
714 | * @param $key |
||
715 | * @param $value |
||
716 | * @return mixed |
||
717 | */ |
||
718 | protected function updateQuantityRelative($item, $key, $value) |
||
736 | |||
737 | /** |
||
738 | * update cart item quantity not relative to its current quantity value |
||
739 | * |
||
740 | * @param $item |
||
741 | * @param $key |
||
742 | * @param $value |
||
743 | * @return mixed |
||
744 | */ |
||
745 | protected function updateQuantityNotRelative($item, $key, $value) |
||
751 | |||
752 | /** |
||
753 | * Setter for decimals. Change value on demand. |
||
754 | * @param $decimals |
||
755 | */ |
||
756 | public function setDecimals($decimals) |
||
760 | |||
761 | /** |
||
762 | * Setter for decimals point. Change value on demand. |
||
763 | * @param $dec_point |
||
764 | */ |
||
765 | public function setDecPoint($dec_point) |
||
769 | |||
770 | public function setThousandsSep($thousands_sep) |
||
774 | |||
775 | /** |
||
776 | * @param $name |
||
777 | * @param $value |
||
778 | * @return mixed |
||
779 | */ |
||
780 | protected function fireEvent($name, $value = []) |
||
784 | } |
||
785 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.