Complex classes like VolumePacker 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 VolumePacker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class VolumePacker implements LoggerAwareInterface |
||
| 21 | { |
||
| 22 | use LoggerAwareTrait; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Box to pack items into. |
||
| 26 | * |
||
| 27 | * @var Box |
||
| 28 | */ |
||
| 29 | protected $box; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | protected $boxWidth; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | protected $boxLength; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * List of items to be packed. |
||
| 43 | * |
||
| 44 | * @var ItemList |
||
| 45 | */ |
||
| 46 | protected $items; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * List of items to be packed. |
||
| 50 | * |
||
| 51 | * @var ItemList |
||
| 52 | */ |
||
| 53 | protected $skippedItems; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Remaining weight capacity of the box. |
||
| 57 | * |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | protected $remainingWeight; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Whether the box was rotated for packing. |
||
| 64 | * |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | protected $boxRotated = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var PackedLayer[] |
||
| 71 | */ |
||
| 72 | protected $layers = []; |
||
| 73 | |||
| 74 | private $z = 0; |
||
| 75 | private $packingWidthLeft; |
||
| 76 | private $packingLengthLeft; |
||
| 77 | private $packingDepthLeft; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Constructor. |
||
| 81 | * |
||
| 82 | * @param Box $box |
||
| 83 | * @param ItemList $items |
||
| 84 | */ |
||
| 85 | 27 | public function __construct(Box $box, ItemList $items) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Pack as many items as possible into specific given box. |
||
| 104 | * |
||
| 105 | * @return PackedBox packed box |
||
| 106 | */ |
||
| 107 | 27 | public function pack(): PackedBox |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Pack items into an individual vertical layer. |
||
| 132 | */ |
||
| 133 | 27 | protected function packLayer(): void |
|
| 207 | |||
| 208 | /** |
||
| 209 | * During packing, it is quite possible that layers have been created that aren't physically stable |
||
| 210 | * i.e. they overhang the ones below. |
||
| 211 | * |
||
| 212 | * This function reorders them so that the ones with the greatest surface area are placed at the bottom |
||
| 213 | */ |
||
| 214 | 27 | public function stabiliseLayers(): void |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @param Item $itemToPack |
||
| 222 | * @param PackedItem|null $prevItem |
||
| 223 | * @param Item|null $nextItem |
||
| 224 | * @param bool $isLastItem |
||
| 225 | * @param int $maxWidth |
||
| 226 | * @param int $maxLength |
||
| 227 | * @param int $maxDepth |
||
| 228 | * |
||
| 229 | * @return OrientatedItem|null |
||
| 230 | */ |
||
| 231 | 27 | protected function getOrientationForItem( |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Figure out if we can stack the next item vertically on top of this rather than side by side |
||
| 263 | * Used when we've packed a tall item, and have just put a shorter one next to it. |
||
| 264 | * |
||
| 265 | * @param PackedLayer $layer |
||
| 266 | * @param PackedItem|null $prevItem |
||
| 267 | * @param Item|null $nextItem |
||
| 268 | * @param int $maxWidth |
||
| 269 | * @param int $maxLength |
||
| 270 | * @param int $maxDepth |
||
| 271 | * @param int $x |
||
| 272 | * @param int $y |
||
| 273 | * @param int $z |
||
| 274 | */ |
||
| 275 | 27 | protected function tryAndStackItemsIntoSpace( |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Check item generally fits into box. |
||
| 310 | * |
||
| 311 | * @param Item $itemToPack |
||
| 312 | * |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | 27 | protected function checkConstraints( |
|
| 321 | |||
| 322 | /** |
||
| 323 | * As well as purely dimensional constraints, there are other constraints that need to be met |
||
| 324 | * e.g. weight limits or item-specific restrictions (e.g. max <x> batteries per box). |
||
| 325 | * |
||
| 326 | * @param Item $itemToPack |
||
| 327 | * |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | 27 | protected function checkNonDimensionalConstraints(Item $itemToPack): bool |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Check the item physically fits in the box (at all). |
||
| 343 | * |
||
| 344 | * @param Item $itemToPack |
||
| 345 | * |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | 27 | protected function checkDimensionalConstraints(Item $itemToPack): bool |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Reintegrate skipped items into main list. |
||
| 358 | * |
||
| 359 | * @param Item|null $currentItem item from current iteration |
||
| 360 | */ |
||
| 361 | 27 | protected function rebuildItemList(?Item $currentItem = null): void |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Swap back width/length of the packed items to match orientation of the box if needed |
||
| 375 | */ |
||
| 376 | 7 | protected function rotateLayersNinetyDegrees(): void |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Are there items left to pack? |
||
| 390 | * |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | 27 | protected function hasItemsLeftToPack(): bool |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Return next item in line for packing. |
||
| 400 | * |
||
| 401 | * @return Item|null |
||
| 402 | */ |
||
| 403 | 27 | protected function getNextItem(): ?Item |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Generate a single list of items packed. |
||
| 410 | * |
||
| 411 | * @return PackedItemList |
||
| 412 | */ |
||
| 413 | 27 | protected function getPackedItemList(): PackedItemList |
|
| 424 | } |
||
| 425 |