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 |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Pack items into an individual vertical layer. |
||
| 123 | * |
||
| 124 | * @param int $startDepth |
||
| 125 | */ |
||
| 126 | 27 | protected function packLayer(int $startDepth): void |
|
| 194 | |||
| 195 | /** |
||
| 196 | * During packing, it is quite possible that layers have been created that aren't physically stable |
||
| 197 | * i.e. they overhang the ones below. |
||
| 198 | * |
||
| 199 | * This function reorders them so that the ones with the greatest surface area are placed at the bottom |
||
| 200 | */ |
||
| 201 | 27 | public function stabiliseLayers(): void |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @param Item $itemToPack |
||
| 209 | * @param PackedItem|null $prevItem |
||
| 210 | * @param Item|null $nextItem |
||
| 211 | * @param bool $isLastItem |
||
| 212 | * @param int $maxWidth |
||
| 213 | * @param int $maxLength |
||
| 214 | * @param int $maxDepth |
||
| 215 | * |
||
| 216 | * @return OrientatedItem|null |
||
| 217 | */ |
||
| 218 | 27 | protected function getOrientationForItem( |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Figure out if we can stack the next item vertically on top of this rather than side by side |
||
| 250 | * Used when we've packed a tall item, and have just put a shorter one next to it. |
||
| 251 | * |
||
| 252 | * @param PackedLayer $layer |
||
| 253 | * @param PackedItem|null $prevItem |
||
| 254 | * @param Item|null $nextItem |
||
| 255 | * @param int $maxWidth |
||
| 256 | * @param int $maxLength |
||
| 257 | * @param int $maxDepth |
||
| 258 | * @param int $x |
||
| 259 | * @param int $y |
||
| 260 | * @param int $z |
||
| 261 | */ |
||
| 262 | 27 | protected function tryAndStackItemsIntoSpace( |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Check item generally fits into box. |
||
| 297 | * |
||
| 298 | * @param Item $itemToPack |
||
| 299 | * |
||
| 300 | * @return bool |
||
| 301 | */ |
||
| 302 | 27 | protected function checkConstraints( |
|
| 308 | |||
| 309 | /** |
||
| 310 | * As well as purely dimensional constraints, there are other constraints that need to be met |
||
| 311 | * e.g. weight limits or item-specific restrictions (e.g. max <x> batteries per box). |
||
| 312 | * |
||
| 313 | * @param Item $itemToPack |
||
| 314 | * |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | 27 | protected function checkNonDimensionalConstraints(Item $itemToPack): bool |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Check the item physically fits in the box (at all). |
||
| 330 | * |
||
| 331 | * @param Item $itemToPack |
||
| 332 | * |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | 27 | protected function checkDimensionalConstraints(Item $itemToPack): bool |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Reintegrate skipped items into main list. |
||
| 345 | * |
||
| 346 | * @param Item|null $currentItem item from current iteration |
||
| 347 | */ |
||
| 348 | 27 | protected function rebuildItemList(?Item $currentItem = null): void |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Swap back width/length of the packed items to match orientation of the box if needed |
||
| 362 | */ |
||
| 363 | 7 | protected function rotateLayersNinetyDegrees(): void |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Are there items left to pack? |
||
| 377 | * |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | 27 | protected function hasItemsLeftToPack(): bool |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Return next item in line for packing. |
||
| 387 | * |
||
| 388 | * @return Item|null |
||
| 389 | */ |
||
| 390 | 27 | protected function getNextItem(): ?Item |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Generate a single list of items packed. |
||
| 397 | * |
||
| 398 | * @return PackedItemList |
||
| 399 | */ |
||
| 400 | 27 | protected function getPackedItemList(): PackedItemList |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Return the current packed depth. |
||
| 414 | * |
||
| 415 | * @return int |
||
| 416 | */ |
||
| 417 | 27 | protected function getCurrentPackedDepth(): int |
|
| 425 | } |
||
| 426 |