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 |
||
| 14 | class PackedBoxList implements \Countable, \IteratorAggregate |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Average (mean) weight of boxes |
||
| 19 | * @var float |
||
| 20 | */ |
||
| 21 | protected $meanWeight; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $list = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var bool |
||
| 30 | */ |
||
| 31 | protected $isSorted = true; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @return int |
||
| 35 | */ |
||
| 36 | 19 | public function count() |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @return \ArrayIterator |
||
| 43 | */ |
||
| 44 | 19 | public function getIterator() |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Insert a box choice into the list |
||
| 52 | * |
||
| 53 | * @param PackedBox $box |
||
| 54 | */ |
||
| 55 | 19 | public function insert(PackedBox $box) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Sort the boxes into order (smallest volume first) |
||
| 63 | */ |
||
| 64 | 19 | protected function sort() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Reversed version of compare |
||
| 86 | * |
||
| 87 | * @return int |
||
| 88 | */ |
||
| 89 | 2 | View Code Duplication | public function reverseCompare(PackedBox $boxA, PackedBox $boxB) |
| 100 | |||
| 101 | /** |
||
| 102 | * Calculate the average (mean) weight of the boxes |
||
| 103 | * |
||
| 104 | * @return float |
||
| 105 | */ |
||
| 106 | 5 | public function getMeanWeight() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Calculate the variance in weight between these boxes |
||
| 123 | * |
||
| 124 | * @return float |
||
| 125 | */ |
||
| 126 | 5 | public function getWeightVariance() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Get volume utilisation of the set of packed boxes |
||
| 141 | * |
||
| 142 | * @return float |
||
| 143 | */ |
||
| 144 | 1 | public function getVolumeUtilisation() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Do a bulk insert |
||
| 164 | * |
||
| 165 | * @param array $boxes |
||
| 166 | */ |
||
| 167 | 4 | public function insertFromArray(array $boxes) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @deprecated |
||
| 176 | * |
||
| 177 | * @return PackedBox |
||
| 178 | */ |
||
| 179 | 2 | public function extract() { |
|
| 185 | } |
||
| 186 |
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.