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 | /** @var string $identifier */ |
||
| 34 | protected $identifier; |
||
| 35 | |||
| 36 | /** @var StorageInterface $store */ |
||
| 37 | protected $store; |
||
| 38 | |||
| 39 | /** @var Tax $tax */ |
||
| 40 | protected $tax; |
||
| 41 | |||
| 42 | /** @var array $data */ |
||
| 43 | protected $data = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Construct the item. |
||
| 47 | * |
||
| 48 | * @param string $identifier |
||
| 49 | * @param array $item |
||
| 50 | * @param StorageInterface $store |
||
| 51 | */ |
||
| 52 | 26 | public function __construct($identifier, array $item, StorageInterface $store) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Return the value of protected methods. |
||
| 69 | * |
||
| 70 | * @param string $param |
||
| 71 | * |
||
| 72 | * @return mixed |
||
| 73 | */ |
||
| 74 | 24 | public function __get($param) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Update data array using set magic method. |
||
| 81 | * |
||
| 82 | * @param string $param The key to set |
||
| 83 | * @param mixed $value The value to set $param to |
||
| 84 | */ |
||
| 85 | 2 | public function __set($param, $value) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Removes the current item from the cart. |
||
| 95 | * |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | 1 | public function remove() |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Return the total tax for this item. |
||
| 105 | * |
||
| 106 | * @return float |
||
| 107 | */ |
||
| 108 | 3 | public function tax() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Return the total price for this item. |
||
| 118 | * |
||
| 119 | * @return float |
||
| 120 | */ |
||
| 121 | 11 | View Code Duplication | private function totalPrice() |
| 135 | |||
| 136 | /** |
||
| 137 | * Return the total of the item, with or without tax. |
||
| 138 | * |
||
| 139 | * @param bool $includeTax Whether or not to include tax |
||
| 140 | * |
||
| 141 | * @return float The total, as a float |
||
| 142 | */ |
||
| 143 | 7 | View Code Duplication | public function total($includeTax = true) |
| 153 | |||
| 154 | /** |
||
| 155 | * Return the total weight of the item. |
||
| 156 | * |
||
| 157 | * @return float The weight, as a float |
||
| 158 | */ |
||
| 159 | 4 | View Code Duplication | public function weight() |
| 173 | |||
| 174 | /** |
||
| 175 | * Return the total of the item, with or without tax. |
||
| 176 | * |
||
| 177 | * @param bool $includeTax Whether or not to include tax |
||
| 178 | * |
||
| 179 | * @return float The total, as a float |
||
| 180 | */ |
||
| 181 | 1 | View Code Duplication | public function single($includeTax = true) |
| 191 | |||
| 192 | /** |
||
| 193 | * Update a single key for this item, or multiple. |
||
| 194 | * |
||
| 195 | * @param array|string $key The array key to update, or an array of key-value pairs to update |
||
| 196 | * @param null $value |
||
| 197 | * |
||
| 198 | * @return void |
||
| 199 | */ |
||
| 200 | 4 | public function update($key, $value = null) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Check if this item has options. |
||
| 217 | * |
||
| 218 | * @return bool Yes or no? |
||
| 219 | */ |
||
| 220 | 17 | public function hasOptions() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Convert the item into an array. |
||
| 227 | * |
||
| 228 | * @return array The item data |
||
| 229 | */ |
||
| 230 | 2 | public function toArray() |
|
| 234 | } |
||
| 235 |