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 implements ItemInterface |
||
| 32 | { |
||
| 33 | /** @var string $identifier */ |
||
| 34 | protected $identifier; |
||
| 35 | |||
| 36 | /** @var Tax $tax */ |
||
| 37 | protected $tax; |
||
| 38 | |||
| 39 | /** @var array $data */ |
||
| 40 | protected $data = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Construct the item. |
||
| 44 | * |
||
| 45 | * @param array $item |
||
| 46 | */ |
||
| 47 | 25 | public function __construct(array $item) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Set identifier. |
||
| 60 | * |
||
| 61 | * @param mixed $identifier |
||
| 62 | * |
||
| 63 | * @return mixed|void |
||
| 64 | */ |
||
| 65 | 18 | public function setIdentifier($identifier) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Return the value of protected methods. |
||
| 72 | * |
||
| 73 | * @param mixed $param |
||
| 74 | * |
||
| 75 | * @return mixed |
||
| 76 | */ |
||
| 77 | 23 | public function __get($param) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Update data array using set magic method. |
||
| 84 | * |
||
| 85 | * @param string $param The key to set |
||
| 86 | * @param mixed $value The value to set $param to |
||
| 87 | */ |
||
| 88 | 19 | public function __set($param, $value) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Return the total tax for this item. |
||
| 99 | * |
||
| 100 | * @return float |
||
| 101 | */ |
||
| 102 | 3 | public function tax() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Return the total price for this item. |
||
| 112 | * |
||
| 113 | * @return float |
||
| 114 | */ |
||
| 115 | 11 | View Code Duplication | private function totalPrice() |
| 129 | |||
| 130 | /** |
||
| 131 | * Return the total of the item, with or without tax. |
||
| 132 | * |
||
| 133 | * @param bool $includeTax Whether or not to include tax |
||
| 134 | * |
||
| 135 | * @return float The total, as a float |
||
| 136 | */ |
||
| 137 | 7 | View Code Duplication | public function total($includeTax = true) |
| 147 | |||
| 148 | /** |
||
| 149 | * Return the total weight of the item. |
||
| 150 | * |
||
| 151 | * @return float The weight, as a float |
||
| 152 | */ |
||
| 153 | 4 | View Code Duplication | public function weight() |
| 167 | |||
| 168 | /** |
||
| 169 | * Return the total of the item, with or without tax. |
||
| 170 | * |
||
| 171 | * @param bool $includeTax Whether or not to include tax |
||
| 172 | * |
||
| 173 | * @return float The total, as a float |
||
| 174 | */ |
||
| 175 | 1 | View Code Duplication | public function single($includeTax = true) |
| 185 | |||
| 186 | /** |
||
| 187 | * Update a single key for this item, or multiple. |
||
| 188 | * |
||
| 189 | * @param mixed $key The array key to update, or an array of key-value pairs to update |
||
| 190 | * @param mixed $value |
||
| 191 | * |
||
| 192 | * @return void |
||
| 193 | */ |
||
| 194 | 4 | public function update($key, $value = null) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Check if this item has options. |
||
| 212 | * |
||
| 213 | * @return bool Yes or no? |
||
| 214 | */ |
||
| 215 | 17 | public function hasOptions() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Convert the item into an array. |
||
| 222 | * |
||
| 223 | * @return array The item data |
||
| 224 | */ |
||
| 225 | 18 | public function toArray() |
|
| 229 | } |
||
| 230 |