Complex classes like Packer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Packer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Packer implements LoggerAwareInterface |
||
| 20 | { |
||
| 21 | use LoggerAwareTrait; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * List of items to be packed |
||
| 25 | * @var ItemList |
||
| 26 | */ |
||
| 27 | protected $items; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * List of box sizes available to pack items into |
||
| 31 | * @var BoxList |
||
| 32 | */ |
||
| 33 | protected $boxes; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Constructor |
||
| 37 | */ |
||
| 38 | 24 | public function __construct() |
|
| 45 | |||
| 46 | /** |
||
| 47 | * Add item to be packed |
||
| 48 | * @param Item $item |
||
| 49 | * @param int $qty |
||
| 50 | */ |
||
| 51 | 14 | public function addItem(Item $item, $qty = 1) |
|
| 52 | { |
||
| 53 | 14 | for ($i = 0; $i < $qty; $i++) { |
|
| 54 | 14 | $this->items->insert($item); |
|
| 55 | 14 | } |
|
| 56 | 14 | $this->logger->log(LogLevel::INFO, "added {$qty} x {$item->getDescription()}"); |
|
| 57 | 14 | } |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Set a list of items all at once |
||
| 61 | * @param \Traversable|array $items |
||
| 62 | */ |
||
| 63 | 4 | public function setItems($items) |
|
| 64 | { |
||
| 65 | 2 | if ($items instanceof ItemList) { |
|
| 66 | 4 | $this->items = clone $items; |
|
| 67 | 2 | } else { |
|
| 68 | 2 | $this->items = new ItemList(); |
|
| 69 | 2 | foreach ($items as $item) { |
|
| 70 | 2 | $this->items->insert($item); |
|
| 71 | 2 | } |
|
| 72 | } |
||
| 73 | 2 | } |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Add box size |
||
| 77 | * @param Box $box |
||
| 78 | */ |
||
| 79 | 14 | public function addBox(Box $box) |
|
| 80 | { |
||
| 81 | 14 | $this->boxes->insert($box); |
|
| 82 | 14 | $this->logger->log(LogLevel::INFO, "added box {$box->getReference()}"); |
|
| 83 | 14 | } |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Add a pre-prepared set of boxes all at once |
||
| 87 | * @param BoxList $boxList |
||
| 88 | */ |
||
| 89 | 2 | public function setBoxes(BoxList $boxList) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Pack items into boxes |
||
| 96 | * |
||
| 97 | * @throws \RuntimeException |
||
| 98 | * @return PackedBoxList |
||
| 99 | */ |
||
| 100 | 14 | public function pack() |
|
| 101 | { |
||
| 102 | 14 | $packedBoxes = $this->doVolumePacking(); |
|
| 103 | |||
| 104 | //If we have multiple boxes, try and optimise/even-out weight distribution |
||
| 105 | 12 | if ($packedBoxes->count() > 1) { |
|
| 106 | 3 | $packedBoxes = $this->redistributeWeight($packedBoxes); |
|
| 107 | 3 | } |
|
| 108 | |||
| 109 | 12 | $this->logger->log(LogLevel::INFO, "packing completed, {$packedBoxes->count()} boxes"); |
|
| 110 | 12 | return $packedBoxes; |
|
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Pack items into boxes using the principle of largest volume item first |
||
| 115 | * |
||
| 116 | * @throws \RuntimeException |
||
| 117 | * @return PackedBoxList |
||
| 118 | */ |
||
| 119 | 15 | public function doVolumePacking() |
|
| 120 | { |
||
| 121 | |||
| 122 | 15 | $packedBoxes = new PackedBoxList; |
|
| 123 | |||
| 124 | //Keep going until everything packed |
||
| 125 | 15 | while ($this->items->count()) { |
|
| 126 | 15 | $boxesToEvaluate = clone $this->boxes; |
|
| 127 | 15 | $packedBoxesIteration = new PackedBoxList; |
|
| 128 | |||
| 129 | //Loop through boxes starting with smallest, see what happens |
||
| 130 | 15 | while (!$boxesToEvaluate->isEmpty()) { |
|
| 131 | 14 | $box = $boxesToEvaluate->extract(); |
|
| 132 | 14 | $packedBox = $this->packIntoBox($box, clone $this->items); |
|
| 133 | 14 | if ($packedBox->getItems()->count()) { |
|
| 134 | 14 | $packedBoxesIteration->insert($packedBox); |
|
| 135 | |||
| 136 | //Have we found a single box that contains everything? |
||
| 137 | 14 | if ($packedBox->getItems()->count() === $this->items->count()) { |
|
| 138 | 13 | break; |
|
| 139 | } |
||
| 140 | 6 | } |
|
| 141 | 7 | } |
|
| 142 | |||
| 143 | //Check iteration was productive |
||
| 144 | 15 | if ($packedBoxesIteration->isEmpty()) { |
|
| 145 | 2 | throw new \RuntimeException('Item ' . $this->items->top()->getDescription() . ' is too large to fit into any box'); |
|
| 146 | } |
||
| 147 | |||
| 148 | //Find best box of iteration, and remove packed items from unpacked list |
||
| 149 | 14 | $bestBox = $packedBoxesIteration->top(); |
|
| 150 | 14 | $unPackedItems = $this->items->asArray(); |
|
| 151 | 14 | foreach (clone $bestBox->getItems() as $packedItem) { |
|
| 152 | 14 | foreach ($unPackedItems as $unpackedKey => $unpackedItem) { |
|
| 153 | 14 | if ($packedItem === $unpackedItem) { |
|
| 154 | 14 | unset($unPackedItems[$unpackedKey]); |
|
| 155 | 14 | break; |
|
| 156 | } |
||
| 157 | 14 | } |
|
| 158 | 14 | } |
|
| 159 | 14 | $unpackedItemList = new ItemList(); |
|
| 160 | 14 | foreach ($unPackedItems as $unpackedItem) { |
|
| 161 | 4 | $unpackedItemList->insert($unpackedItem); |
|
| 162 | 14 | } |
|
| 163 | 14 | $this->items = $unpackedItemList; |
|
| 164 | 14 | $packedBoxes->insert($bestBox); |
|
| 165 | |||
| 166 | 14 | } |
|
| 167 | |||
| 168 | 13 | return $packedBoxes; |
|
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Given a solution set of packed boxes, repack them to achieve optimum weight distribution |
||
| 173 | * |
||
| 174 | * @param PackedBoxList $originalBoxes |
||
| 175 | * @return PackedBoxList |
||
| 176 | */ |
||
| 177 | 4 | public function redistributeWeight(PackedBoxList $originalBoxes) |
|
| 178 | { |
||
| 179 | |||
| 180 | 4 | $targetWeight = $originalBoxes->getMeanWeight(); |
|
| 181 | 4 | $this->logger->log(LogLevel::DEBUG, "repacking for weight distribution, weight variance {$originalBoxes->getWeightVariance()}, target weight {$targetWeight}"); |
|
| 182 | |||
| 183 | 4 | $packedBoxes = new PackedBoxList; |
|
| 184 | |||
| 185 | 4 | $overWeightBoxes = []; |
|
| 186 | 4 | $underWeightBoxes = []; |
|
| 187 | 4 | foreach (clone $originalBoxes as $packedBox) { |
|
| 188 | 4 | $boxWeight = $packedBox->getWeight(); |
|
| 189 | 4 | if ($boxWeight > $targetWeight) { |
|
| 190 | 3 | $overWeightBoxes[] = $packedBox; |
|
| 191 | 4 | } elseif ($boxWeight < $targetWeight) { |
|
| 192 | 3 | $underWeightBoxes[] = $packedBox; |
|
| 193 | 3 | } else { |
|
| 194 | 1 | $packedBoxes->insert($packedBox); //target weight, so we'll keep these |
|
| 195 | } |
||
| 196 | 4 | } |
|
| 197 | |||
| 198 | do { //Keep moving items from most overweight box to most underweight box |
||
| 199 | 4 | $tryRepack = false; |
|
| 200 | 4 | $this->logger->log(LogLevel::DEBUG, 'boxes under/over target: ' . count($underWeightBoxes) . '/' . count($overWeightBoxes)); |
|
| 201 | |||
| 202 | 4 | foreach ($underWeightBoxes as $u => $underWeightBox) { |
|
| 203 | 3 | $this->logger->log(LogLevel::DEBUG, 'Underweight Box ' . $u); |
|
| 204 | 3 | foreach ($overWeightBoxes as $o => $overWeightBox) { |
|
| 205 | 3 | $this->logger->log(LogLevel::DEBUG, 'Overweight Box ' . $o); |
|
| 206 | 3 | $overWeightBoxItems = $overWeightBox->getItems()->asArray(); |
|
| 207 | |||
| 208 | //For each item in the heavier box, try and move it to the lighter one |
||
| 209 | 3 | foreach ($overWeightBoxItems as $oi => $overWeightBoxItem) { |
|
| 210 | 3 | $this->logger->log(LogLevel::DEBUG, 'Overweight Item ' . $oi); |
|
| 211 | 3 | if ($underWeightBox->getWeight() + $overWeightBoxItem->getWeight() > $targetWeight) { |
|
| 212 | 3 | $this->logger->log(LogLevel::DEBUG, 'Skipping item for hindering weight distribution'); |
|
| 213 | 3 | continue; //skip if moving this item would hinder rather than help weight distribution |
|
| 214 | } |
||
| 215 | |||
| 216 | 2 | $newItemsForLighterBox = clone $underWeightBox->getItems(); |
|
| 217 | 2 | $newItemsForLighterBox->insert($overWeightBoxItem); |
|
| 218 | |||
| 219 | 2 | $newLighterBoxPacker = new Packer(); //we may need a bigger box |
|
| 220 | 2 | $newLighterBoxPacker->setBoxes($this->boxes); |
|
| 221 | 2 | $newLighterBoxPacker->setItems($newItemsForLighterBox); |
|
| 222 | 2 | $this->logger->log(LogLevel::INFO, "[ATTEMPTING TO PACK LIGHTER BOX]"); |
|
| 223 | 2 | $newLighterBox = $newLighterBoxPacker->doVolumePacking()->extract(); |
|
| 224 | |||
| 225 | 2 | if ($newLighterBox->getItems()->count() === $newItemsForLighterBox->count()) { //new item fits |
|
| 226 | 2 | $this->logger->log(LogLevel::DEBUG, 'New item fits'); |
|
| 227 | 2 | unset($overWeightBoxItems[$oi]); //now packed in different box |
|
| 228 | |||
| 229 | 2 | $newHeavierBoxPacker = new Packer(); //we may be able to use a smaller box |
|
| 230 | 2 | $newHeavierBoxPacker->setBoxes($this->boxes); |
|
| 231 | 2 | $newHeavierBoxPacker->setItems($overWeightBoxItems); |
|
| 232 | |||
| 233 | 2 | $this->logger->log(LogLevel::INFO, "[ATTEMPTING TO PACK HEAVIER BOX]"); |
|
| 234 | 2 | $newHeavierBoxes = $newHeavierBoxPacker->doVolumePacking(); |
|
| 235 | 2 | if (count($newHeavierBoxes) > 1) { //found an edge case in packing algorithm that *increased* box count |
|
| 236 | $this->logger->log(LogLevel::INFO, "[REDISTRIBUTING WEIGHT] Abandoning redistribution, because new packing is less efficient than original"); |
||
| 237 | return $originalBoxes; |
||
| 238 | } |
||
| 239 | |||
| 240 | 2 | $overWeightBoxes[$o] = $newHeavierBoxes->extract(); |
|
| 241 | 2 | $underWeightBoxes[$u] = $newLighterBox; |
|
| 242 | |||
| 243 | 2 | $tryRepack = true; //we did some work, so see if we can do even better |
|
| 244 | 2 | usort($overWeightBoxes, [$packedBoxes, 'reverseCompare']); |
|
| 245 | 2 | usort($underWeightBoxes, [$packedBoxes, 'reverseCompare']); |
|
| 246 | 2 | break 3; |
|
| 247 | } |
||
| 248 | 3 | } |
|
| 249 | 3 | } |
|
| 250 | 4 | } |
|
| 251 | 4 | } while ($tryRepack); |
|
| 252 | |||
| 253 | //Combine back into a single list |
||
| 254 | 4 | $packedBoxes->insertFromArray($overWeightBoxes); |
|
| 255 | 4 | $packedBoxes->insertFromArray($underWeightBoxes); |
|
| 256 | |||
| 257 | 4 | return $packedBoxes; |
|
| 258 | } |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * Pack as many items as possible into specific given box |
||
| 263 | * @param Box $box |
||
| 264 | * @param ItemList $items |
||
| 265 | * @return PackedBox packed box |
||
| 266 | */ |
||
| 267 | 23 | public function packIntoBox(Box $box, ItemList $items) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * @param Item $item |
||
| 348 | * @param int $remainingDepth |
||
| 349 | * @param int $remainingWeight |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | 23 | protected function isItemTooLargeForBox(Item $item, $remainingDepth, $remainingWeight) { |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Figure out space left for next item if we pack this one in it's regular orientation |
||
| 358 | * @param Item $item |
||
| 359 | * @param int $remainingWidth |
||
| 360 | * @param int $remainingLength |
||
| 361 | * @return int |
||
| 362 | */ |
||
| 363 | 23 | protected function fitsSameGap(Item $item, $remainingWidth, $remainingLength) { |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Figure out space left for next item if we pack this one rotated by 90deg |
||
| 369 | * @param Item $item |
||
| 370 | * @param int $remainingWidth |
||
| 371 | * @param int $remainingLength |
||
| 372 | * @return int |
||
| 373 | */ |
||
| 374 | 23 | protected function fitsRotatedGap(Item $item, $remainingWidth, $remainingLength) { |
|
| 377 | |||
| 378 | /** |
||
| 379 | * @param Item $item |
||
| 380 | * @param Item|null $nextItem |
||
| 381 | * @param $remainingWidth |
||
| 382 | * @param $remainingLength |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | 23 | protected function fitsBetterRotated(Item $item, Item $nextItem = null, $remainingWidth, $remainingLength) { |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Does item fit in specified gap |
||
| 397 | * @param Item $item |
||
| 398 | * @param $remainingWidth |
||
| 399 | * @param $remainingLength |
||
| 400 | * @return bool |
||
| 401 | */ |
||
| 402 | 23 | protected function fitsGap(Item $item, $remainingWidth, $remainingLength) { |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Figure out if we can stack the next item vertically on top of this rather than side by side |
||
| 409 | * Used when we've packed a tall item, and have just put a shorter one next to it |
||
| 410 | * @param Item $item |
||
| 411 | * @param Item $nextItem |
||
| 412 | * @param $maxStackDepth |
||
| 413 | * @param $remainingWeight |
||
| 414 | * @return bool |
||
| 415 | */ |
||
| 416 | 22 | protected function canStackItemInLayer(Item $item, Item $nextItem, $maxStackDepth, $remainingWeight) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @param $layerWidth |
||
| 426 | * @param $layerLength |
||
| 427 | * @param $layerDepth |
||
| 428 | * @return bool |
||
| 429 | */ |
||
| 430 | 19 | protected function isLayerStarted($layerWidth, $layerLength, $layerDepth) { |
|
| 433 | } |
||
| 434 |