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 |
||
| 31 | class Item |
||
| 32 | { |
||
| 33 | protected $identifier; |
||
| 34 | protected $store; |
||
| 35 | protected $tax; |
||
| 36 | |||
| 37 | protected $data = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Construct the item. |
||
| 41 | * |
||
| 42 | * @param string $identifier |
||
| 43 | * @param array $item |
||
| 44 | * @param StorageInterface $store |
||
| 45 | */ |
||
| 46 | 26 | public function __construct($identifier, array $item, StorageInterface $store) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Return the value of protected methods. |
||
| 62 | * |
||
| 63 | * @param any $param |
||
| 64 | * |
||
| 65 | * @return mixed |
||
| 66 | */ |
||
| 67 | 24 | public function __get($param) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Update data array using set magic method. |
||
| 74 | * |
||
| 75 | * @param string $param The key to set |
||
| 76 | * @param mixed $value The value to set $param to |
||
| 77 | */ |
||
| 78 | 2 | public function __set($param, $value) |
|
| 79 | { |
||
| 80 | 2 | $this->data[$param] = $value; |
|
| 81 | 2 | if ($param == 'tax') { |
|
| 82 | 1 | $this->tax = new Tax($value); |
|
| 83 | } |
||
| 84 | 2 | } |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Removes the current item from the cart. |
||
| 88 | * |
||
| 89 | * @return void |
||
| 90 | */ |
||
| 91 | 1 | public function remove() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Return the total tax for this item. |
||
| 98 | * |
||
| 99 | * @return float |
||
| 100 | */ |
||
| 101 | 3 | public function tax() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Return the total price for this item. |
||
| 111 | * |
||
| 112 | * @return float |
||
| 113 | */ |
||
| 114 | 11 | View Code Duplication | private function totalPrice() |
| 128 | |||
| 129 | /** |
||
| 130 | * Return the total of the item, with or without tax. |
||
| 131 | * |
||
| 132 | * @param bool $includeTax Whether or not to include tax |
||
| 133 | * |
||
| 134 | * @return float The total, as a float |
||
| 135 | */ |
||
| 136 | 7 | View Code Duplication | public function total($includeTax = true) |
| 146 | |||
| 147 | /** |
||
| 148 | * Return the total weight of the item. |
||
| 149 | * |
||
| 150 | * @return float The weight, as a float |
||
| 151 | */ |
||
| 152 | 4 | View Code Duplication | public function weight() |
| 166 | |||
| 167 | /** |
||
| 168 | * Return the total of the item, with or without tax. |
||
| 169 | * |
||
| 170 | * @param bool $includeTax Whether or not to include tax |
||
| 171 | * |
||
| 172 | * @return float The total, as a float |
||
| 173 | */ |
||
| 174 | 1 | View Code Duplication | public function single($includeTax = true) |
| 175 | { |
||
| 176 | 1 | $price = $this->totalPrice(); |
|
| 177 | |||
| 178 | 1 | if ($includeTax) { |
|
| 179 | 1 | $price = $this->tax->add($price); |
|
| 180 | } |
||
| 181 | |||
| 182 | 1 | return (float) $price; |
|
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Update a single key for this item, or multiple. |
||
| 187 | * |
||
| 188 | * @param array|string $key The array key to update, or an array of key-value pairs to update |
||
| 189 | * @param null $value |
||
| 190 | * |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | 4 | public function update($key, $value = null) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Check if this item has options. |
||
| 210 | * |
||
| 211 | * @return bool Yes or no? |
||
| 212 | */ |
||
| 213 | 17 | public function hasOptions() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Convert the item into an array. |
||
| 220 | * |
||
| 221 | * @return array The item data |
||
| 222 | */ |
||
| 223 | 2 | public function toArray() |
|
| 227 | } |
||
| 228 |