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 | /** |
||
| 75 | * Constructor. |
||
| 76 | * |
||
| 77 | * @param Box $box |
||
| 78 | * @param ItemList $items |
||
| 79 | */ |
||
| 80 | 27 | public function __construct(Box $box, ItemList $items) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Pack as many items as possible into specific given box. |
||
| 99 | * |
||
| 100 | * @return PackedBox packed box |
||
| 101 | */ |
||
| 102 | 27 | public function pack(): PackedBox |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Pack items into an individual vertical layer. |
||
| 124 | * |
||
| 125 | * @param int $startDepth |
||
| 126 | * @param int $widthLeft |
||
| 127 | * @param int $lengthLeft |
||
| 128 | * @param int $depthLeft |
||
| 129 | */ |
||
| 130 | 27 | protected function packLayer(int $startDepth, int $widthLeft, int $lengthLeft, int $depthLeft): void |
|
| 190 | |||
| 191 | /** |
||
| 192 | * During packing, it is quite possible that layers have been created that aren't physically stable |
||
| 193 | * i.e. they overhang the ones below. |
||
| 194 | * |
||
| 195 | * This function reorders them so that the ones with the greatest surface area are placed at the bottom |
||
| 196 | */ |
||
| 197 | 27 | public function stabiliseLayers(): void |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @param Item $itemToPack |
||
| 205 | * @param PackedItem|null $prevItem |
||
| 206 | * @param Item|null $nextItem |
||
| 207 | * @param bool $isLastItem |
||
| 208 | * @param int $maxWidth |
||
| 209 | * @param int $maxLength |
||
| 210 | * @param int $maxDepth |
||
| 211 | * |
||
| 212 | * @return OrientatedItem|null |
||
| 213 | */ |
||
| 214 | 27 | protected function getOrientationForItem( |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Figure out if we can stack the next item vertically on top of this rather than side by side |
||
| 246 | * Used when we've packed a tall item, and have just put a shorter one next to it. |
||
| 247 | * |
||
| 248 | * @param PackedLayer $layer |
||
| 249 | * @param PackedItem|null $prevItem |
||
| 250 | * @param Item|null $nextItem |
||
| 251 | * @param int $maxWidth |
||
| 252 | * @param int $maxLength |
||
| 253 | * @param int $maxDepth |
||
| 254 | * @param int $x |
||
| 255 | * @param int $y |
||
| 256 | * @param int $z |
||
| 257 | */ |
||
| 258 | 27 | protected function tryAndStackItemsIntoSpace( |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Check item generally fits into box. |
||
| 293 | * |
||
| 294 | * @param Item $itemToPack |
||
| 295 | * |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | 27 | protected function checkConstraints( |
|
| 304 | |||
| 305 | /** |
||
| 306 | * As well as purely dimensional constraints, there are other constraints that need to be met |
||
| 307 | * e.g. weight limits or item-specific restrictions (e.g. max <x> batteries per box). |
||
| 308 | * |
||
| 309 | * @param Item $itemToPack |
||
| 310 | * |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | 27 | protected function checkNonDimensionalConstraints(Item $itemToPack): bool |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Check the item physically fits in the box (at all). |
||
| 326 | * |
||
| 327 | * @param Item $itemToPack |
||
| 328 | * |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | 27 | protected function checkDimensionalConstraints(Item $itemToPack): bool |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Reintegrate skipped items into main list. |
||
| 341 | * |
||
| 342 | * @param Item|null $currentItem item from current iteration |
||
| 343 | */ |
||
| 344 | 27 | protected function rebuildItemList(?Item $currentItem = null): void |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Swap back width/length of the packed items to match orientation of the box if needed. |
||
| 358 | */ |
||
| 359 | 7 | protected function rotateLayersNinetyDegrees(): void |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Are there items left to pack? |
||
| 373 | * |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | 27 | protected function hasItemsLeftToPack(): bool |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Return next item in line for packing. |
||
| 383 | * |
||
| 384 | * @return Item|null |
||
| 385 | */ |
||
| 386 | 27 | protected function getNextItem(): ?Item |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Generate a single list of items packed. |
||
| 393 | * |
||
| 394 | * @return PackedItemList |
||
| 395 | */ |
||
| 396 | 27 | protected function getPackedItemList(): PackedItemList |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Return the current packed depth. |
||
| 410 | * |
||
| 411 | * @return int |
||
| 412 | */ |
||
| 413 | 27 | protected function getCurrentPackedDepth(): int |
|
| 422 | } |
||
| 423 |