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 |
||
30 | class Item |
||
31 | { |
||
32 | protected $identifier; |
||
33 | protected $store; |
||
34 | protected $tax; |
||
35 | |||
36 | protected $data = []; |
||
37 | |||
38 | /** |
||
39 | * Construct the item. |
||
40 | * |
||
41 | * @param string $identifier |
||
42 | * @param array $item |
||
43 | * @param StorageInterface $store |
||
44 | */ |
||
45 | 24 | public function __construct($identifier, array $item, StorageInterface $store) |
|
58 | |||
59 | /** |
||
60 | * Return the value of protected methods. |
||
61 | * |
||
62 | * @param any $param |
||
63 | * |
||
64 | * @return mixed |
||
65 | */ |
||
66 | 22 | public function __get($param) |
|
70 | |||
71 | /** |
||
72 | * Update data array using set magic method. |
||
73 | * |
||
74 | * @param string $param The key to set |
||
75 | * @param mixed $value The value to set $param to |
||
76 | */ |
||
77 | 1 | public function __set($param, $value) |
|
84 | |||
85 | /** |
||
86 | * Removes the current item from the cart. |
||
87 | * |
||
88 | * @return void |
||
89 | */ |
||
90 | 1 | public function remove() |
|
94 | |||
95 | /** |
||
96 | * Return the total tax for this item. |
||
97 | * |
||
98 | * @return float |
||
99 | */ |
||
100 | 2 | public function tax() |
|
107 | |||
108 | /** |
||
109 | * Return the total price for this item. |
||
110 | * |
||
111 | * @return float |
||
112 | */ |
||
113 | 9 | View Code Duplication | private function totalPrice() |
127 | |||
128 | /** |
||
129 | * Return the total of the item, with or without tax. |
||
130 | * |
||
131 | * @param bool $includeTax Whether or not to include tax |
||
132 | * |
||
133 | * @return float The total, as a float |
||
134 | */ |
||
135 | 7 | View Code Duplication | public function total($includeTax = true) |
1 ignored issue
–
show
|
|||
136 | { |
||
137 | 7 | $price = $this->totalPrice(); |
|
138 | |||
139 | 7 | if ($includeTax) { |
|
140 | 7 | $price = $this->tax->add($price); |
|
141 | } |
||
142 | |||
143 | 7 | return (float) ($price * $this->quantity); |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * Return the total weight of the item. |
||
148 | * |
||
149 | * @return float The weight, as a float |
||
150 | */ |
||
151 | 4 | View Code Duplication | public function weight() |
165 | |||
166 | /** |
||
167 | * Return the total of the item, with or without tax. |
||
168 | * |
||
169 | * @param bool $includeTax Whether or not to include tax |
||
170 | * |
||
171 | * @return float The total, as a float |
||
172 | */ |
||
173 | View Code Duplication | public function single($includeTax = true) |
|
183 | |||
184 | /** |
||
185 | * Update a single key for this item, or multiple. |
||
186 | * |
||
187 | * @param array|string $key The array key to update, or an array of key-value pairs to update |
||
188 | * @param null $value |
||
189 | * |
||
190 | * @return void |
||
191 | */ |
||
192 | 4 | public function update($key, $value = null) |
|
206 | |||
207 | /** |
||
208 | * Check if this item has options. |
||
209 | * |
||
210 | * @return bool Yes or no? |
||
211 | */ |
||
212 | 15 | public function hasOptions() |
|
216 | |||
217 | /** |
||
218 | * Convert the item into an array. |
||
219 | * |
||
220 | * @return array The item data |
||
221 | */ |
||
222 | 2 | public function toArray() |
|
226 | } |
||
227 |
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.