| Total Complexity | 59 |
| Total Lines | 371 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 23 | ||
| Bugs | 3 | Features | 0 |
Complex classes like OrientatedItemFactory 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.
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 OrientatedItemFactory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class OrientatedItemFactory implements LoggerAwareInterface |
||
| 26 | { |
||
| 27 | use LoggerAwareTrait; |
||
| 28 | |||
| 29 | /** @var Box */ |
||
| 30 | protected $box; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Whether the packer is in single-pass mode. |
||
| 34 | * |
||
| 35 | * @var bool |
||
| 36 | */ |
||
| 37 | protected $singlePassMode = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var OrientatedItem[] |
||
| 41 | */ |
||
| 42 | protected static $emptyBoxCache = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var int[] |
||
| 46 | */ |
||
| 47 | protected static $lookaheadCache = []; |
||
| 48 | |||
| 49 | 58 | public function __construct(Box $box) |
|
| 53 | 58 | } |
|
| 54 | |||
| 55 | 51 | public function setSinglePassMode(bool $singlePassMode): void |
|
| 58 | 51 | } |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Get the best orientation for an item. |
||
| 62 | */ |
||
| 63 | 56 | public function getBestOrientation( |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Find all possible orientations for an item. |
||
| 122 | * |
||
| 123 | * @return OrientatedItem[] |
||
| 124 | */ |
||
| 125 | 57 | public function getPossibleOrientations( |
|
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return OrientatedItem[] |
||
| 179 | */ |
||
| 180 | 32 | public function getPossibleOrientationsInEmptyBox(Item $item): array |
|
| 181 | { |
||
| 182 | 32 | $cacheKey = $item->getWidth() . |
|
| 183 | 32 | '|' . |
|
| 184 | 32 | $item->getLength() . |
|
| 185 | 32 | '|' . |
|
| 186 | 32 | $item->getDepth() . |
|
| 187 | 32 | '|' . |
|
| 188 | 32 | ($item->getKeepFlat() ? '2D' : '3D') . |
|
| 189 | 32 | '|' . |
|
| 190 | 32 | $this->box->getInnerWidth() . |
|
| 191 | 32 | '|' . |
|
| 192 | 32 | $this->box->getInnerLength() . |
|
| 193 | 32 | '|' . |
|
| 194 | 32 | $this->box->getInnerDepth(); |
|
| 195 | |||
| 196 | 32 | if (isset(static::$emptyBoxCache[$cacheKey])) { |
|
| 197 | 26 | $orientations = static::$emptyBoxCache[$cacheKey]; |
|
| 198 | } else { |
||
| 199 | 28 | $orientations = $this->getPossibleOrientations( |
|
| 200 | 28 | $item, |
|
| 201 | 28 | null, |
|
| 202 | 28 | $this->box->getInnerWidth(), |
|
| 203 | 28 | $this->box->getInnerLength(), |
|
| 204 | 28 | $this->box->getInnerDepth(), |
|
| 205 | 28 | 0, |
|
| 206 | 28 | 0, |
|
| 207 | 28 | 0, |
|
| 208 | 28 | new PackedItemList() |
|
| 209 | ); |
||
| 210 | 28 | static::$emptyBoxCache[$cacheKey] = $orientations; |
|
| 211 | } |
||
| 212 | |||
| 213 | 32 | return $orientations; |
|
|
1 ignored issue
–
show
|
|||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param OrientatedItem[] $possibleOrientations |
||
| 218 | * |
||
| 219 | * @return OrientatedItem[] |
||
| 220 | */ |
||
| 221 | 56 | protected function getUsableOrientations( |
|
| 222 | Item $item, |
||
| 223 | array $possibleOrientations |
||
| 224 | ): array { |
||
| 225 | 56 | $orientationsToUse = $stableOrientations = $unstableOrientations = []; |
|
| 226 | |||
| 227 | // Divide possible orientations into stable (low centre of gravity) and unstable (high centre of gravity) |
||
| 228 | 56 | foreach ($possibleOrientations as $orientation) { |
|
| 229 | 56 | if ($orientation->isStable() || $this->box->getInnerDepth() === $orientation->getDepth()) { |
|
| 230 | 54 | $stableOrientations[] = $orientation; |
|
| 231 | } else { |
||
| 232 | 9 | $unstableOrientations[] = $orientation; |
|
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | /* |
||
| 237 | * We prefer to use stable orientations only, but allow unstable ones if |
||
| 238 | * the item doesn't fit in the box any other way |
||
| 239 | */ |
||
| 240 | 56 | if (count($stableOrientations) > 0) { |
|
| 241 | 54 | $orientationsToUse = $stableOrientations; |
|
| 242 | 50 | } elseif (count($unstableOrientations) > 0) { |
|
| 243 | 9 | $stableOrientationsInEmptyBox = $this->getStableOrientationsInEmptyBox($item); |
|
| 244 | |||
| 245 | 9 | if (count($stableOrientationsInEmptyBox) === 0) { |
|
| 246 | 6 | $orientationsToUse = $unstableOrientations; |
|
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | 56 | return $orientationsToUse; |
|
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Return the orientations for this item if it were to be placed into the box with nothing else. |
||
| 255 | */ |
||
| 256 | 9 | protected function getStableOrientationsInEmptyBox(Item $item): array |
|
| 264 | 9 | } |
|
| 265 | ); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Approximation of a forward-looking packing. |
||
| 270 | * |
||
| 271 | * Not an actual packing, that has additional logic regarding constraints and stackability, this focuses |
||
| 272 | * purely on fit. |
||
| 273 | */ |
||
| 274 | 44 | protected function calculateAdditionalItemsPackedWithThisOrientation( |
|
| 342 | } |
||
| 343 | |||
| 344 | 52 | private function exactFitDecider(OrientatedItem $a, OrientatedItem $b, int $widthLeft, int $lengthLeft, int $depthLeft): int |
|
| 345 | { |
||
| 346 | 52 | $orientationAWidthLeft = $widthLeft - $a->getWidth(); |
|
| 347 | 52 | $orientationBWidthLeft = $widthLeft - $b->getWidth(); |
|
| 348 | 52 | if ($orientationAWidthLeft === 0 && $orientationBWidthLeft > 0) { |
|
| 349 | 7 | return -1; |
|
| 350 | } |
||
| 351 | 50 | if ($orientationBWidthLeft === 0 && $orientationAWidthLeft > 0) { |
|
| 352 | 5 | return 1; |
|
| 353 | } |
||
| 354 | |||
| 355 | 50 | $orientationALengthLeft = $lengthLeft - $a->getLength(); |
|
| 356 | 50 | $orientationBLengthLeft = $lengthLeft - $b->getLength(); |
|
| 357 | 50 | if ($orientationALengthLeft === 0 && $orientationBLengthLeft > 0) { |
|
| 358 | 14 | return -1; |
|
| 359 | } |
||
| 360 | 50 | if ($orientationBLengthLeft === 0 && $orientationALengthLeft > 0) { |
|
| 361 | 15 | return 1; |
|
| 362 | } |
||
| 363 | |||
| 364 | 50 | $orientationADepthLeft = $depthLeft - $a->getDepth(); |
|
| 365 | 50 | $orientationBDepthLeft = $depthLeft - $b->getDepth(); |
|
| 366 | 50 | if ($orientationADepthLeft === 0 && $orientationBDepthLeft > 0) { |
|
| 367 | 4 | return -1; |
|
| 368 | } |
||
| 369 | 50 | if ($orientationBDepthLeft === 0 && $orientationADepthLeft > 0) { |
|
| 370 | 2 | return 1; |
|
| 371 | } |
||
| 372 | |||
| 373 | 49 | return 0; |
|
| 374 | } |
||
| 375 | |||
| 376 | 49 | private function lookAheadDecider(ItemList $nextItems, OrientatedItem $a, OrientatedItem $b, int $widthLeft, int $lengthLeft, int $depthLeft, int $rowLength, int $x, int $y, int $z, PackedItemList $prevPackedItemList): int |
|
| 396 | } |
||
| 397 | } |
||
| 398 |