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 |
||
| 21 | class Packer implements LoggerAwareInterface |
||
| 22 | { |
||
| 23 | use LoggerAwareTrait; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Number of boxes at which balancing weight is deemed not worth it |
||
| 27 | * @var int |
||
| 28 | */ |
||
| 29 | protected $maxBoxesToBalanceWeight = 12; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * List of items to be packed |
||
| 33 | * @var ItemList |
||
| 34 | */ |
||
| 35 | protected $items; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * List of box sizes available to pack items into |
||
| 39 | * @var BoxList |
||
| 40 | */ |
||
| 41 | protected $boxes; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Constructor |
||
| 45 | */ |
||
| 46 | 27 | public function __construct() |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Add item to be packed |
||
| 56 | * @param Item $item |
||
| 57 | * @param int $qty |
||
| 58 | */ |
||
| 59 | 26 | public function addItem(Item $item, int $qty = 1): void |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Set a list of items all at once |
||
| 69 | * @param \Traversable|array $items |
||
| 70 | */ |
||
| 71 | 2 | public function setItems($items): void |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Add box size |
||
| 85 | * @param Box $box |
||
| 86 | */ |
||
| 87 | 25 | public function addBox(Box $box): void |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Add a pre-prepared set of boxes all at once |
||
| 95 | * @param BoxList $boxList |
||
| 96 | */ |
||
| 97 | 2 | public function setBoxes(BoxList $boxList): void |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Number of boxes at which balancing weight is deemed not worth the extra computation time |
||
| 104 | * @return int |
||
| 105 | */ |
||
| 106 | 1 | public function getMaxBoxesToBalanceWeight(): int |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Number of boxes at which balancing weight is deemed not worth the extra computation time |
||
| 113 | * @param int $maxBoxesToBalanceWeight |
||
| 114 | */ |
||
| 115 | 2 | public function setMaxBoxesToBalanceWeight(int $maxBoxesToBalanceWeight) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Pack items into boxes |
||
| 122 | * |
||
| 123 | * @return PackedBoxList |
||
| 124 | */ |
||
| 125 | 26 | public function pack(): PackedBoxList |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Pack items into boxes using the principle of largest volume item first |
||
| 142 | * |
||
| 143 | * @throws ItemTooLargeException |
||
| 144 | * @return PackedBoxList |
||
| 145 | */ |
||
| 146 | 26 | public function doVolumePacking(): PackedBoxList |
|
| 197 | |||
| 198 | /** |
||
| 199 | * @param PackedBox[] $packedBoxes |
||
| 200 | * |
||
| 201 | * @return PackedBox |
||
| 202 | */ |
||
| 203 | 25 | private function findBestBoxFromIteration($packedBoxes): PackedBox |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @param PackedBox $boxA |
||
| 211 | * @param PackedBox $boxB |
||
| 212 | * |
||
| 213 | * @return int |
||
| 214 | */ |
||
| 215 | 3 | View Code Duplication | private static function compare(PackedBox $boxA, PackedBox $boxB): int |
| 226 | } |
||
| 227 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.