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 |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Pack items into an individual layer |
||
| 129 | */ |
||
| 130 | 27 | protected function packLayer() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @param Item $itemToPack |
||
| 208 | * @param PackedItem|null $prevItem |
||
| 209 | * @param Item|null $nextItem |
||
| 210 | * @param bool $isLastItem |
||
| 211 | * @param int $maxWidth |
||
| 212 | * @param int $maxLength |
||
| 213 | * @param int $maxDepth |
||
| 214 | * |
||
| 215 | * @return OrientatedItem|null |
||
| 216 | */ |
||
| 217 | 27 | protected function getOrientationForItem( |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Figure out if we can stack the next item vertically on top of this rather than side by side |
||
| 249 | * Used when we've packed a tall item, and have just put a shorter one next to it. |
||
| 250 | * |
||
| 251 | * @param PackedLayer $layer |
||
| 252 | * @param PackedItem|null $prevItem |
||
| 253 | * @param Item|null $nextItem |
||
| 254 | * @param int $maxWidth |
||
| 255 | * @param int $maxLength |
||
| 256 | * @param int $maxDepth |
||
| 257 | * @param int $x |
||
| 258 | * @param int $y |
||
| 259 | * @param int $z |
||
| 260 | */ |
||
| 261 | 27 | protected function tryAndStackItemsIntoSpace( |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Check item generally fits into box. |
||
| 296 | * |
||
| 297 | * @param Item $itemToPack |
||
| 298 | * |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | 27 | protected function checkConstraints( |
|
| 307 | |||
| 308 | /** |
||
| 309 | * As well as purely dimensional constraints, there are other constraints that need to be met |
||
| 310 | * e.g. weight limits or item-specific restrictions (e.g. max <x> batteries per box). |
||
| 311 | * |
||
| 312 | * @param Item $itemToPack |
||
| 313 | * |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | 27 | protected function checkNonDimensionalConstraints(Item $itemToPack): bool |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Check the item physically fits in the box (at all). |
||
| 329 | * |
||
| 330 | * @param Item $itemToPack |
||
| 331 | * |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | 27 | protected function checkDimensionalConstraints(Item $itemToPack): bool |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Reintegrate skipped items into main list. |
||
| 344 | * |
||
| 345 | * @param Item|null $currentItem item from current iteration |
||
| 346 | */ |
||
| 347 | 27 | protected function rebuildItemList(?Item $currentItem = null): void |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Create the final PackedBox object |
||
| 361 | * |
||
| 362 | * @return PackedBox |
||
| 363 | */ |
||
| 364 | 27 | protected function createPackedBox(): PackedBox |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Are there items left to pack? |
||
| 382 | * |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | 27 | protected function hasItemsLeftToPack(): bool |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Return next item in line for packing. |
||
| 392 | * |
||
| 393 | * @return Item|null |
||
| 394 | */ |
||
| 395 | 27 | protected function getNextItem(): ?Item |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Generate a single list of items packed |
||
| 402 | * |
||
| 403 | * @return PackedItemList |
||
| 404 | */ |
||
| 405 | 27 | protected function getPackedItemList(): PackedItemList |
|
| 416 | } |
||
| 417 |