| Total Complexity | 52 |
| Total Lines | 355 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 24 | ||
| 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( |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Find all possible orientations for an item. |
||
| 144 | * |
||
| 145 | * @return OrientatedItem[] |
||
| 146 | */ |
||
| 147 | 57 | public function getPossibleOrientations( |
|
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return OrientatedItem[] |
||
| 195 | */ |
||
| 196 | 32 | public function getPossibleOrientationsInEmptyBox(Item $item): array |
|
| 197 | { |
||
| 198 | 32 | $cacheKey = $item->getWidth() . |
|
| 199 | 32 | '|' . |
|
| 200 | 32 | $item->getLength() . |
|
| 201 | 32 | '|' . |
|
| 202 | 32 | $item->getDepth() . |
|
| 203 | 32 | '|' . |
|
| 204 | 32 | ($item->getKeepFlat() ? '2D' : '3D') . |
|
| 205 | 32 | '|' . |
|
| 206 | 32 | $this->box->getInnerWidth() . |
|
| 207 | 32 | '|' . |
|
| 208 | 32 | $this->box->getInnerLength() . |
|
| 209 | 32 | '|' . |
|
| 210 | 32 | $this->box->getInnerDepth(); |
|
| 211 | |||
| 212 | 32 | if (isset(static::$emptyBoxCache[$cacheKey])) { |
|
| 213 | 26 | $orientations = static::$emptyBoxCache[$cacheKey]; |
|
| 214 | } else { |
||
| 215 | 28 | $orientations = $this->getPossibleOrientations( |
|
| 216 | 28 | $item, |
|
| 217 | 28 | null, |
|
| 218 | 28 | $this->box->getInnerWidth(), |
|
| 219 | 28 | $this->box->getInnerLength(), |
|
| 220 | 28 | $this->box->getInnerDepth(), |
|
| 221 | 28 | 0, |
|
| 222 | 28 | 0, |
|
| 223 | 28 | 0, |
|
| 224 | 28 | new PackedItemList() |
|
| 225 | ); |
||
| 226 | 28 | static::$emptyBoxCache[$cacheKey] = $orientations; |
|
| 227 | } |
||
| 228 | |||
| 229 | 32 | return $orientations; |
|
|
1 ignored issue
–
show
|
|||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param OrientatedItem[] $possibleOrientations |
||
| 234 | * |
||
| 235 | * @return OrientatedItem[] |
||
| 236 | */ |
||
| 237 | 56 | protected function getUsableOrientations( |
|
| 238 | Item $item, |
||
| 239 | array $possibleOrientations |
||
| 240 | ): array { |
||
| 241 | 56 | $orientationsToUse = $stableOrientations = $unstableOrientations = []; |
|
| 242 | |||
| 243 | // Divide possible orientations into stable (low centre of gravity) and unstable (high centre of gravity) |
||
| 244 | 56 | foreach ($possibleOrientations as $orientation) { |
|
| 245 | 56 | if ($orientation->isStable() || $this->box->getInnerDepth() === $orientation->getDepth()) { |
|
| 246 | 54 | $stableOrientations[] = $orientation; |
|
| 247 | } else { |
||
| 248 | 9 | $unstableOrientations[] = $orientation; |
|
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | /* |
||
| 253 | * We prefer to use stable orientations only, but allow unstable ones if |
||
| 254 | * the item doesn't fit in the box any other way |
||
| 255 | */ |
||
| 256 | 56 | if (count($stableOrientations) > 0) { |
|
| 257 | 54 | $orientationsToUse = $stableOrientations; |
|
| 258 | 50 | } elseif (count($unstableOrientations) > 0) { |
|
| 259 | 9 | $stableOrientationsInEmptyBox = $this->getStableOrientationsInEmptyBox($item); |
|
| 260 | |||
| 261 | 9 | if (count($stableOrientationsInEmptyBox) === 0) { |
|
| 262 | 6 | $orientationsToUse = $unstableOrientations; |
|
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | 56 | return $orientationsToUse; |
|
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Return the orientations for this item if it were to be placed into the box with nothing else. |
||
| 271 | */ |
||
| 272 | 9 | protected function getStableOrientationsInEmptyBox(Item $item): array |
|
| 280 | 9 | } |
|
| 281 | ); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Approximation of a forward-looking packing. |
||
| 286 | * |
||
| 287 | * Not an actual packing, that has additional logic regarding constraints and stackability, this focuses |
||
| 288 | * purely on fit. |
||
| 289 | */ |
||
| 290 | 44 | protected function calculateAdditionalItemsPackedWithThisOrientation( |
|
| 358 | } |
||
| 359 | |||
| 360 | 49 | private function lookAheadDecider(ItemList $nextItems, OrientatedItem $a, OrientatedItem $b, int $orientationAWidthLeft, int $orientationBWidthLeft, int $widthLeft, int $lengthLeft, int $depthLeft, int $rowLength, int $x, int $y, int $z, PackedItemList $prevPackedItemList): int |
|
| 380 | } |
||
| 381 | } |
||
| 382 |