| Total Complexity | 57 |
| Total Lines | 361 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 22 | ||
| 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( |
|
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @return OrientatedItem[] |
||
| 201 | */ |
||
| 202 | 32 | public function getPossibleOrientationsInEmptyBox(Item $item): array |
|
| 203 | { |
||
| 204 | 32 | $cacheKey = $item->getWidth() . |
|
| 205 | 32 | '|' . |
|
| 206 | 32 | $item->getLength() . |
|
| 207 | 32 | '|' . |
|
| 208 | 32 | $item->getDepth() . |
|
| 209 | 32 | '|' . |
|
| 210 | 32 | ($item->getKeepFlat() ? '2D' : '3D') . |
|
| 211 | 32 | '|' . |
|
| 212 | 32 | $this->box->getInnerWidth() . |
|
| 213 | 32 | '|' . |
|
| 214 | 32 | $this->box->getInnerLength() . |
|
| 215 | 32 | '|' . |
|
| 216 | 32 | $this->box->getInnerDepth(); |
|
| 217 | |||
| 218 | 32 | if (isset(static::$emptyBoxCache[$cacheKey])) { |
|
| 219 | 26 | $orientations = static::$emptyBoxCache[$cacheKey]; |
|
| 220 | } else { |
||
| 221 | 28 | $orientations = $this->getPossibleOrientations( |
|
| 222 | 28 | $item, |
|
| 223 | 28 | null, |
|
| 224 | 28 | $this->box->getInnerWidth(), |
|
| 225 | 28 | $this->box->getInnerLength(), |
|
| 226 | 28 | $this->box->getInnerDepth(), |
|
| 227 | 28 | 0, |
|
| 228 | 28 | 0, |
|
| 229 | 28 | 0, |
|
| 230 | 28 | new PackedItemList() |
|
| 231 | ); |
||
| 232 | 28 | static::$emptyBoxCache[$cacheKey] = $orientations; |
|
| 233 | } |
||
| 234 | |||
| 235 | 32 | return $orientations; |
|
|
1 ignored issue
–
show
|
|||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param OrientatedItem[] $possibleOrientations |
||
| 240 | * |
||
| 241 | * @return OrientatedItem[] |
||
| 242 | */ |
||
| 243 | 56 | protected function getUsableOrientations( |
|
| 244 | Item $item, |
||
| 245 | array $possibleOrientations |
||
| 246 | ): array { |
||
| 247 | 56 | $orientationsToUse = $stableOrientations = $unstableOrientations = []; |
|
| 248 | |||
| 249 | // Divide possible orientations into stable (low centre of gravity) and unstable (high centre of gravity) |
||
| 250 | 56 | foreach ($possibleOrientations as $orientation) { |
|
| 251 | 56 | if ($orientation->isStable() || $this->box->getInnerDepth() === $orientation->getDepth()) { |
|
| 252 | 54 | $stableOrientations[] = $orientation; |
|
| 253 | } else { |
||
| 254 | 9 | $unstableOrientations[] = $orientation; |
|
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | /* |
||
| 259 | * We prefer to use stable orientations only, but allow unstable ones if |
||
| 260 | * the item doesn't fit in the box any other way |
||
| 261 | */ |
||
| 262 | 56 | if (count($stableOrientations) > 0) { |
|
| 263 | 54 | $orientationsToUse = $stableOrientations; |
|
| 264 | 50 | } elseif (count($unstableOrientations) > 0) { |
|
| 265 | 9 | $stableOrientationsInEmptyBox = $this->getStableOrientationsInEmptyBox($item); |
|
| 266 | |||
| 267 | 9 | if (count($stableOrientationsInEmptyBox) === 0) { |
|
| 268 | 6 | $orientationsToUse = $unstableOrientations; |
|
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | 56 | return $orientationsToUse; |
|
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Return the orientations for this item if it were to be placed into the box with nothing else. |
||
| 277 | */ |
||
| 278 | 9 | protected function getStableOrientationsInEmptyBox(Item $item): array |
|
| 286 | 9 | } |
|
| 287 | ); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Approximation of a forward-looking packing. |
||
| 292 | * |
||
| 293 | * Not an actual packing, that has additional logic regarding constraints and stackability, this focuses |
||
| 294 | * purely on fit. |
||
| 295 | */ |
||
| 296 | 44 | protected function calculateAdditionalItemsPackedWithThisOrientation( |
|
| 364 | } |
||
| 365 | |||
| 366 | 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 |
|
| 386 | } |
||
| 387 | } |
||
| 388 |