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 |
||
| 513 | * @param bool $formatted |
||
| 514 | * @return float |
||
| 515 | */ |
||
| 516 | public function getSubTotal($formatted = true) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * the new total in which conditions are already applied |
||
| 529 | * |
||
| 530 | * @return float |
||
| 531 | */ |
||
| 532 | public function getTotal() |
||
| 559 | |||
| 560 | /** |
||
| 561 | * get total quantity of items in the cart |
||
| 562 | * |
||
| 563 | * @return int |
||
| 564 | */ |
||
| 565 | public function getTotalQuantity() |
||
| 577 | |||
| 578 | /** |
||
| 579 | * get the cart |
||
| 580 | * |
||
| 581 | * @return CartCollection |
||
| 582 | */ |
||
| 583 | public function getContent() |
||
| 587 | |||
| 588 | /** |
||
| 589 | * check if cart is empty |
||
| 590 | * |
||
| 591 | * @return bool |
||
| 592 | */ |
||
| 593 | public function isEmpty() |
||
| 599 | |||
| 600 | /** |
||
| 601 | * validate Item data |
||
| 602 | * |
||
| 603 | * @param $item |
||
| 604 | * @return array $item; |
||
| 605 | * @throws InvalidItemException |
||
| 606 | */ |
||
| 607 | View Code Duplication | protected function validate($item) |
|
| 624 | |||
| 625 | /** |
||
| 626 | * add row to cart collection |
||
| 627 | * |
||
| 628 | * @param $id |
||
| 629 | * @param $item |
||
| 630 | * @return bool |
||
| 631 | */ |
||
| 632 | protected function addRow($id, $item) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * save the cart |
||
| 651 | * |
||
| 652 | * @param $cart CartCollection |
||
| 653 | */ |
||
| 654 | protected function save($cart) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * save the cart conditions |
||
| 661 | * |
||
| 662 | * @param $conditions |
||
| 663 | */ |
||
| 664 | protected function saveConditions($conditions) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * check if an item has condition |
||
| 671 | * |
||
| 672 | * @param $item |
||
| 673 | * @return bool |
||
| 674 | */ |
||
| 675 | protected function itemHasConditions($item) |
||
| 689 | |||
| 690 | /** |
||
| 691 | * update a cart item quantity relative to its current quantity |
||
| 692 | * |
||
| 693 | * @param $item |
||
| 694 | * @param $key |
||
| 695 | * @param $value |
||
| 696 | * @return mixed |
||
| 697 | */ |
||
| 698 | protected function updateQuantityRelative($item, $key, $value) |
||
| 716 | |||
| 717 | /** |
||
| 718 | * update cart item quantity not relative to its current quantity value |
||
| 719 | * |
||
| 720 | * @param $item |
||
| 721 | * @param $key |
||
| 722 | * @param $value |
||
| 723 | * @return mixed |
||
| 724 | */ |
||
| 725 | protected function updateQuantityNotRelative($item, $key, $value) |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Setter for decimals. Change value on demand. |
||
| 734 | * @param $decimals |
||
| 735 | */ |
||
| 736 | public function setDecimals($decimals) |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Setter for decimals point. Change value on demand. |
||
| 743 | * @param $dec_point |
||
| 744 | */ |
||
| 745 | public function setDecPoint($dec_point) |
||
| 749 | |||
| 750 | public function setThousandsSep($thousands_sep) |
||
| 754 | |||
| 755 | /** |
||
| 756 | * @param $name |
||
| 757 | * @param $value |
||
| 758 | * @return mixed |
||
| 759 | */ |
||
| 760 | protected function fireEvent($name, $value = []) |
||
| 764 | } |
||
| 765 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.