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 | 4582 | */ |
|
| 38 | 4582 | public function __construct() |
|
| 45 | |||
| 46 | /** |
||
| 47 | * Add item to be packed |
||
| 48 | * @param Item $item |
||
| 49 | 4572 | * @param int $qty |
|
| 50 | 4572 | */ |
|
| 51 | 4572 | public function addItem(Item $item, $qty = 1) |
|
| 58 | |||
| 59 | /** |
||
| 60 | 1323 | * Set a list of items all at once |
|
| 61 | 1233 | * @param \Traversable $items |
|
| 62 | 1233 | */ |
|
| 63 | 1233 | public function setItems($items) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Add box size |
||
| 79 | 4572 | * @param Box $box |
|
| 80 | 4572 | */ |
|
| 81 | 4572 | public function addBox(Box $box) |
|
| 86 | |||
| 87 | /** |
||
| 88 | 1233 | * Add a pre-prepared set of boxes all at once |
|
| 89 | 1233 | * @param BoxList $boxList |
|
| 90 | 1233 | */ |
|
| 91 | public function setBoxes(BoxList $boxList) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Pack items into boxes |
||
| 98 | 4572 | * |
|
| 99 | 4572 | * @throws \RuntimeException |
|
| 100 | * @return PackedBoxList |
||
| 101 | */ |
||
| 102 | 4570 | public function pack() |
|
| 114 | |||
| 115 | /** |
||
| 116 | 4573 | * Pack items into boxes using the principle of largest volume item first |
|
| 117 | * |
||
| 118 | 4573 | * @throws \RuntimeException |
|
| 119 | * @return PackedBoxList |
||
| 120 | */ |
||
| 121 | 4573 | public function doVolumePacking() |
|
| 172 | |||
| 173 | 1323 | /** |
|
| 174 | * Given a solution set of packed boxes, repack them to achieve optimum weight distribution |
||
| 175 | 1323 | * |
|
| 176 | 1323 | * @param PackedBoxList $originalBoxes |
|
| 177 | * @return PackedBoxList |
||
| 178 | 1323 | */ |
|
| 179 | public function redistributeWeight(PackedBoxList $originalBoxes) |
||
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | 4581 | * Pack as many items as possible into specific given box |
|
| 265 | 4581 | * @param Box $box |
|
| 266 | * @param ItemList $items |
||
| 267 | 4581 | * @return PackedBox packed box |
|
| 268 | 4581 | */ |
|
| 269 | 4581 | public function packIntoBox(Box $box, ItemList $items) |
|
| 270 | 4581 | { |
|
| 271 | 4581 | $this->logger->log(LogLevel::DEBUG, "[EVALUATING BOX] {$box->getReference()}"); |
|
| 272 | |||
| 273 | 4581 | $packedItems = new ItemList; |
|
| 274 | 4581 | $remainingDepth = $box->getInnerDepth(); |
|
| 275 | $remainingWeight = $box->getMaxWeight() - $box->getEmptyWeight(); |
||
| 276 | 4581 | $remainingWidth = $box->getInnerWidth(); |
|
| 277 | $remainingLength = $box->getInnerLength(); |
||
| 278 | 4581 | ||
| 279 | 3473 | $layerWidth = $layerLength = $layerDepth = 0; |
|
| 280 | 3473 | while (!$items->isEmpty()) { |
|
| 281 | |||
| 282 | $itemToPack = $items->top(); |
||
| 283 | 4581 | ||
| 284 | 4581 | //skip items that are simply too large |
|
| 285 | 4581 | if ($this->isItemTooLargeForBox($itemToPack, $remainingDepth, $remainingWeight)) { |
|
| 286 | $items->extract(); |
||
| 287 | 4581 | continue; |
|
| 288 | 4581 | } |
|
| 289 | |||
| 290 | 4581 | $this->logger->log(LogLevel::DEBUG, "evaluating item {$itemToPack->getDescription()}"); |
|
| 291 | 4581 | $this->logger->log(LogLevel::DEBUG, "remaining width: {$remainingWidth}, length: {$remainingLength}, depth: {$remainingDepth}"); |
|
| 292 | $this->logger->log(LogLevel::DEBUG, "layerWidth: {$layerWidth}, layerLength: {$layerLength}, layerDepth: {$layerDepth}"); |
||
| 293 | 4581 | ||
| 294 | $itemWidth = $itemToPack->getWidth(); |
||
| 295 | 4581 | $itemLength = $itemToPack->getLength(); |
|
| 296 | 4581 | ||
| 297 | $fitsSameGap = $this->fitsSameGap($itemToPack, $remainingWidth, $remainingLength); |
||
| 298 | 4581 | $fitsRotatedGap = $this->fitsRotatedGap($itemToPack, $remainingWidth, $remainingLength); |
|
| 299 | 4572 | ||
| 300 | 4581 | if ($fitsSameGap >= 0 || $fitsRotatedGap >= 0) { |
|
| 301 | 3158 | ||
| 302 | 3158 | $packedItems->insert($items->extract()); |
|
| 303 | 3158 | $remainingWeight -= $itemToPack->getWeight(); |
|
| 304 | 3158 | ||
| 305 | 3158 | $nextItem = !$items->isEmpty() ? $items->top() : null; |
|
| 306 | if ($this->fitsBetterRotated($itemToPack, $nextItem, $remainingWidth, $remainingLength)) { |
||
| 307 | 4536 | $this->logger->log(LogLevel::DEBUG, "fits (better) unrotated"); |
|
| 308 | 4536 | $remainingLength -= $itemLength; |
|
| 309 | 4536 | $layerLength += $itemLength; |
|
| 310 | 4536 | $layerWidth = max($itemWidth, $layerWidth); |
|
| 311 | } else { |
||
| 312 | 4581 | $this->logger->log(LogLevel::DEBUG, "fits (better) rotated"); |
|
| 313 | $remainingLength -= $itemWidth; |
||
| 314 | $layerLength += $itemWidth; |
||
| 315 | 4581 | $layerWidth = max($itemLength, $layerWidth); |
|
| 316 | 4581 | } |
|
| 317 | 4550 | $layerDepth = max($layerDepth, $itemToPack->getDepth()); //greater than 0, items will always be less deep |
|
| 318 | 4550 | ||
| 319 | 4550 | //allow items to be stacked in place within the same footprint up to current layerdepth |
|
| 320 | 4550 | $maxStackDepth = $layerDepth - $itemToPack->getDepth(); |
|
| 321 | 4550 | while (!$items->isEmpty() && $this->canStackItemInLayer($itemToPack, $items->top(), $maxStackDepth, $remainingWeight)) { |
|
| 322 | 285 | $remainingWeight -= $items->top()->getWeight(); |
|
| 323 | 285 | $maxStackDepth -= $items->top()->getDepth(); |
|
| 324 | 285 | $packedItems->insert($items->extract()); |
|
| 325 | 285 | } |
|
| 326 | } else { |
||
| 327 | 4550 | if ($remainingWidth >= min($itemWidth, $itemLength) && $this->isLayerStarted($layerWidth, $layerLength, $layerDepth)) { |
|
| 328 | $this->logger->log(LogLevel::DEBUG, "No more fit in lengthwise, resetting for new row"); |
||
| 329 | 285 | $remainingLength += $layerLength; |
|
| 330 | 4581 | $remainingWidth -= $layerWidth; |
|
| 331 | $layerWidth = $layerLength = 0; |
||
| 332 | 4562 | continue; |
|
| 333 | 4502 | } elseif ($remainingLength < min($itemWidth, $itemLength) || $layerDepth == 0) { |
|
| 334 | 4502 | $this->logger->log(LogLevel::DEBUG, "doesn't fit on layer even when empty"); |
|
| 335 | 4502 | $items->extract(); |
|
| 336 | 4502 | continue; |
|
| 337 | 4502 | } |
|
| 338 | |||
| 339 | $remainingWidth = $layerWidth ? min(floor($layerWidth * 1.1), $box->getInnerWidth()) : $box->getInnerWidth(); |
||
| 340 | 4549 | $remainingLength = $layerLength ? min(floor($layerLength * 1.1), $box->getInnerLength()) : $box->getInnerLength(); |
|
| 341 | 4153 | $remainingDepth -= $layerDepth; |
|
| 342 | 4153 | ||
| 343 | 4153 | $layerWidth = $layerLength = $layerDepth = 0; |
|
| 344 | $this->logger->log(LogLevel::DEBUG, "doesn't fit, so starting next vertical layer"); |
||
| 345 | } |
||
| 346 | 4487 | } |
|
| 347 | 4487 | $this->logger->log(LogLevel::DEBUG, "done with this box"); |
|
| 348 | 4487 | return new PackedBox($box, $packedItems, $remainingWidth, $remainingLength, $remainingDepth, $remainingWeight); |
|
| 349 | } |
||
| 350 | 4487 | ||
| 351 | 4487 | /** |
|
| 352 | * @param Item $item |
||
| 353 | 4581 | * @param int $remainingDepth |
|
| 354 | 4581 | * @param int $remainingWeight |
|
| 355 | 4581 | * @return bool |
|
| 356 | */ |
||
| 357 | protected function isItemTooLargeForBox(Item $item, $remainingDepth, $remainingWeight) { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Figure out space left for next item if we pack this one in it's regular orientation |
||
| 363 | * @param Item $item |
||
| 364 | * @param int $remainingWidth |
||
| 365 | * @param int $remainingLength |
||
| 366 | * @return int |
||
| 367 | */ |
||
| 368 | protected function fitsSameGap(Item $item, $remainingWidth, $remainingLength) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Figure out space left for next item if we pack this one rotated by 90deg |
||
| 374 | * @param Item $item |
||
| 375 | * @param int $remainingWidth |
||
| 376 | * @param int $remainingLength |
||
| 377 | * @return int |
||
| 378 | */ |
||
| 379 | protected function fitsRotatedGap(Item $item, $remainingWidth, $remainingLength) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param Item $item |
||
| 385 | * @param Item|null $nextItem |
||
| 386 | * @param $remainingWidth |
||
| 387 | * @param $remainingLength |
||
| 388 | * @return bool |
||
| 389 | */ |
||
| 390 | protected function fitsBetterRotated(Item $item, Item $nextItem = null, $remainingWidth, $remainingLength) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Figure out if we can stack the next item vertically on top of this rather than side by side |
||
| 402 | * Used when we've packed a tall item, and have just put a shorter one next to it |
||
| 403 | * @param Item $item |
||
| 404 | * @param Item $nextItem |
||
| 405 | * @param $maxStackDepth |
||
| 406 | * @param $remainingWeight |
||
| 407 | * @return bool |
||
| 408 | */ |
||
| 409 | protected function canStackItemInLayer(Item $item, Item $nextItem, $maxStackDepth, $remainingWeight) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param $layerWidth |
||
| 419 | * @param $layerLength |
||
| 420 | * @param $layerDepth |
||
| 421 | * @return bool |
||
| 422 | */ |
||
| 423 | protected function isLayerStarted($layerWidth, $layerLength, $layerDepth) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Pack as many items as possible into specific given box |
||
| 429 | * @deprecated |
||
| 430 | * @param Box $box |
||
| 431 | * @param ItemList $items |
||
| 432 | * @return ItemList items packed into box |
||
| 433 | */ |
||
| 434 | public function packBox(Box $box, ItemList $items) |
||
| 439 | } |
||
| 440 |