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 |
||
28 | class Item |
||
29 | { |
||
30 | protected $identifier; |
||
31 | protected $store; |
||
32 | protected $tax; |
||
33 | |||
34 | protected $data = []; |
||
35 | |||
36 | /** |
||
37 | * Construct the item. |
||
38 | * |
||
39 | * @param string $identifier |
||
40 | * @param array $item |
||
41 | * @param StorageInterface $store |
||
42 | */ |
||
43 | public function __construct($identifier, array $item, StorageInterface $store) |
||
56 | |||
57 | /** |
||
58 | * Return the value of protected methods. |
||
59 | * |
||
60 | * @param any $param |
||
61 | * |
||
62 | * @return mixed |
||
63 | */ |
||
64 | public function __get($param) |
||
68 | |||
69 | /** |
||
70 | * Update data array using set magic method. |
||
71 | * |
||
72 | * @param string $param The key to set |
||
73 | * @param mixed $value The value to set $param to |
||
74 | */ |
||
75 | public function __set($param, $value) |
||
82 | |||
83 | /** |
||
84 | * Removes the current item from the cart. |
||
85 | * |
||
86 | * @return void |
||
87 | */ |
||
88 | public function remove() |
||
92 | |||
93 | /** |
||
94 | * Return the total tax for this item. |
||
95 | * |
||
96 | * @return float |
||
97 | */ |
||
98 | public function tax() |
||
104 | |||
105 | /** |
||
106 | * Return the total price for this item. |
||
107 | * |
||
108 | * @return float |
||
109 | */ |
||
110 | View Code Duplication | private function totalPrice() |
|
124 | |||
125 | /** |
||
126 | * Return the total of the item, with or without tax. |
||
127 | * |
||
128 | * @param bool $includeTax Whether or not to include tax |
||
129 | * |
||
130 | * @return float The total, as a float |
||
131 | */ |
||
132 | public function total($includeTax = true) |
||
142 | |||
143 | /** |
||
144 | * Return the total weight of the item. |
||
145 | * |
||
146 | * @return float The weight, as a float |
||
147 | */ |
||
148 | View Code Duplication | public function weight() |
|
162 | |||
163 | /** |
||
164 | * Return the total of the item, with or without tax. |
||
165 | * |
||
166 | * @param bool $includeTax Whether or not to include tax |
||
167 | * |
||
168 | * @return float The total, as a float |
||
169 | */ |
||
170 | public function single($includeTax = true) |
||
188 | |||
189 | /** |
||
190 | * Update a single key for this item, or multiple. |
||
191 | * |
||
192 | * @param array|string $key The array key to update, or an array of key-value pairs to update |
||
193 | * @param null $value |
||
194 | * |
||
195 | * @return void |
||
196 | */ |
||
197 | public function update($key, $value = null) |
||
211 | |||
212 | /** |
||
213 | * Check if this item has options. |
||
214 | * |
||
215 | * @return bool Yes or no? |
||
216 | */ |
||
217 | public function hasOptions() |
||
221 | |||
222 | /** |
||
223 | * Convert the item into an array. |
||
224 | * |
||
225 | * @return array The item data |
||
226 | */ |
||
227 | public function toArray() |
||
231 | } |
||
232 |
Let’s take a look at an example: