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:
1 | <?php |
||
23 | class CartModify extends AbstractOperation implements \Countable |
||
24 | { |
||
25 | private $itemCounter = 1; |
||
26 | |||
27 | const ITEM_LIMIT = 10; |
||
28 | |||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | */ |
||
32 | 1 | public function getName() |
|
36 | |||
37 | /** |
||
38 | * Returns the cart id |
||
39 | * |
||
40 | * @return string |
||
41 | */ |
||
42 | 1 | public function getCartId() |
|
46 | |||
47 | /** |
||
48 | * Sets the cart id |
||
49 | * |
||
50 | * @param string $cartId |
||
51 | */ |
||
52 | 2 | public function setCartId($cartId) |
|
56 | |||
57 | /** |
||
58 | * Returns the HMAC |
||
59 | * |
||
60 | * @return mixed |
||
61 | */ |
||
62 | 1 | public function getHMAC() |
|
66 | |||
67 | /** |
||
68 | * Sets the HMAC |
||
69 | * |
||
70 | * @param string $HMAC |
||
71 | */ |
||
72 | 2 | public function setHMAC($HMAC) |
|
76 | |||
77 | /** |
||
78 | * Changes quantity of a CartItem |
||
79 | * |
||
80 | * @param string $cartItemId The CartItemId retrieved from CartCreate | CartAdd result |
||
81 | * @param integer $quantity How many items should be in cart after modification |
||
82 | * |
||
83 | * @return $this |
||
84 | * |
||
85 | * @throws \OverflowException if more then self::ITEM_LIMIT items are to be modified |
||
86 | * @throws \InvalidArgumentException if $quantity is not an integer |
||
87 | * @throws \OutOfRangeException if $quantity is not in the allowed range: 0 <= $quantity <= 999 |
||
88 | */ |
||
89 | 8 | View Code Duplication | public function modifyQuantity($cartItemId, $quantity) |
101 | |||
102 | /** |
||
103 | * Changes an action of CartIteam to move between Cart and SaveForLater lists |
||
104 | * |
||
105 | * @param string $cartItemId The CartItemId retrieved from CartCreate | CartAdd result |
||
106 | * @param string $action Valid Values: MoveToCart | SaveForLater |
||
107 | * |
||
108 | * @return $this |
||
109 | * |
||
110 | * @throws \OutOfRangeException if more then self::ITEM_LIMIT items are to be modified |
||
111 | */ |
||
112 | 5 | View Code Duplication | public function modifyAction($cartItemId, $action) |
124 | |||
125 | /** |
||
126 | * Counts cart modification for this operation |
||
127 | * |
||
128 | * @link http://php.net/manual/en/countable.count.php |
||
129 | * @return int The number of modifications for this operations |
||
130 | * |
||
131 | * @since 5.1.0 |
||
132 | */ |
||
133 | 12 | public function count() |
|
137 | |||
138 | /** |
||
139 | * @param integer $quantity |
||
140 | */ |
||
141 | 8 | private function validateQuantity($quantity) |
|
155 | |||
156 | /** |
||
157 | * Validates ActionTypes |
||
158 | * |
||
159 | * @param string $action |
||
160 | */ |
||
161 | 5 | private function validateAction($action) |
|
173 | |||
174 | /** |
||
175 | * Validates maximum request size |
||
176 | */ |
||
177 | 12 | private function validateModificationCount() |
|
185 | } |
||
186 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.