| Total Complexity | 40 |
| Total Lines | 321 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 16 | ||
| 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 |
||
| 27 | class OrientatedItemFactory implements LoggerAwareInterface |
||
| 28 | { |
||
| 29 | use LoggerAwareTrait; |
||
| 30 | |||
| 31 | /** @var Box */ |
||
| 32 | protected $box; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var OrientatedItem[] |
||
| 36 | */ |
||
| 37 | protected static $emptyBoxCache = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var int[] |
||
| 41 | */ |
||
| 42 | protected static $lookaheadCache = []; |
||
| 43 | |||
| 44 | 54 | public function __construct(Box $box) |
|
| 48 | 54 | } |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Get the best orientation for an item. |
||
| 52 | */ |
||
| 53 | 52 | public function getBestOrientation( |
|
| 54 | Item $item, |
||
| 55 | ?OrientatedItem $prevItem, |
||
| 56 | ItemList $nextItems, |
||
| 57 | bool $isLastItem, |
||
| 58 | int $widthLeft, |
||
| 59 | int $lengthLeft, |
||
| 60 | int $depthLeft, |
||
| 61 | int $rowLength, |
||
| 62 | int $x, |
||
| 63 | int $y, |
||
| 64 | int $z, |
||
| 65 | PackedItemList $prevPackedItemList |
||
| 66 | ): ?OrientatedItem { |
||
| 67 | 52 | $possibleOrientations = $this->getPossibleOrientations($item, $prevItem, $widthLeft, $lengthLeft, $depthLeft, $x, $y, $z, $prevPackedItemList); |
|
| 68 | 52 | $usableOrientations = $this->getUsableOrientations($item, $possibleOrientations, $isLastItem); |
|
| 69 | |||
| 70 | 52 | if (empty($usableOrientations)) { |
|
| 71 | 49 | return null; |
|
| 72 | } |
||
| 73 | |||
| 74 | usort($usableOrientations, function (OrientatedItem $a, OrientatedItem $b) use ($widthLeft, $lengthLeft, $depthLeft, $nextItems, $rowLength, $x, $y, $z, $prevPackedItemList) { |
||
| 75 | 28 | $orientationAWidthLeft = $widthLeft - $a->getWidth(); |
|
| 76 | 28 | $orientationALengthLeft = $lengthLeft - $a->getLength(); |
|
| 77 | 28 | $orientationBWidthLeft = $widthLeft - $b->getWidth(); |
|
| 78 | 28 | $orientationBLengthLeft = $lengthLeft - $b->getLength(); |
|
| 79 | |||
| 80 | 28 | $orientationAMinGap = min($orientationAWidthLeft, $orientationALengthLeft); |
|
| 81 | 28 | $orientationBMinGap = min($orientationBWidthLeft, $orientationBLengthLeft); |
|
| 82 | |||
| 83 | 28 | if ($orientationAMinGap === 0 && $orientationBMinGap === 0) { |
|
| 84 | 12 | return $a->getDepth() <=> $b->getDepth(); |
|
| 85 | } |
||
| 86 | 25 | if ($orientationAMinGap === 0) { // prefer A if it leaves no gap |
|
| 87 | 11 | return -1; |
|
| 88 | } |
||
| 89 | 25 | if ($orientationBMinGap === 0) { // prefer B if it leaves no gap |
|
| 90 | 16 | return 1; |
|
| 91 | } |
||
| 92 | |||
| 93 | // prefer leaving room for next item in current row |
||
| 94 | 24 | if ($nextItems->count()) { |
|
| 95 | 21 | $nextItemFitA = $this->getPossibleOrientations($nextItems->top(), $a, $orientationAWidthLeft, $lengthLeft, $depthLeft, $x, $y, $z, $prevPackedItemList); |
|
| 96 | 21 | $nextItemFitB = $this->getPossibleOrientations($nextItems->top(), $b, $orientationBWidthLeft, $lengthLeft, $depthLeft, $x, $y, $z, $prevPackedItemList); |
|
| 97 | 21 | if ($nextItemFitA && !$nextItemFitB) { |
|
| 98 | 9 | return -1; |
|
| 99 | } |
||
| 100 | 19 | if ($nextItemFitB && !$nextItemFitA) { |
|
| 101 | 9 | return 1; |
|
| 102 | } |
||
| 103 | |||
| 104 | // if not an easy either/or, do a partial lookahead |
||
| 105 | 17 | $additionalPackedA = $this->calculateAdditionalItemsPackedWithThisOrientation($a, $nextItems, $widthLeft, $lengthLeft, $depthLeft, $rowLength); |
|
| 106 | 17 | $additionalPackedB = $this->calculateAdditionalItemsPackedWithThisOrientation($b, $nextItems, $widthLeft, $lengthLeft, $depthLeft, $rowLength); |
|
| 107 | 17 | if ($additionalPackedA !== $additionalPackedB) { |
|
| 108 | 8 | return $additionalPackedB <=> $additionalPackedA; |
|
| 109 | } |
||
| 110 | } |
||
| 111 | // otherwise prefer leaving minimum possible gap, or the greatest footprint |
||
| 112 | 20 | return $orientationAMinGap <=> $orientationBMinGap ?: $a->getSurfaceFootprint() <=> $b->getSurfaceFootprint(); |
|
| 113 | 52 | }); |
|
| 114 | |||
| 115 | 52 | $bestFit = reset($usableOrientations); |
|
| 116 | 52 | $this->logger->debug('Selected best fit orientation', ['orientation' => $bestFit]); |
|
| 117 | |||
| 118 | 52 | return $bestFit; |
|
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Find all possible orientations for an item. |
||
| 123 | * |
||
| 124 | * @return OrientatedItem[] |
||
| 125 | */ |
||
| 126 | 53 | public function getPossibleOrientations( |
|
| 127 | Item $item, |
||
| 128 | ?OrientatedItem $prevItem, |
||
| 129 | int $widthLeft, |
||
| 130 | int $lengthLeft, |
||
| 131 | int $depthLeft, |
||
| 132 | int $x, |
||
| 133 | int $y, |
||
| 134 | int $z, |
||
| 135 | PackedItemList $prevPackedItemList |
||
| 136 | ): array { |
||
| 137 | 53 | $orientations = $orientationsDimensions = []; |
|
| 138 | |||
| 139 | 53 | $isSame = false; |
|
| 140 | 53 | if ($prevItem) { |
|
| 141 | 49 | $itemADimensions = [$item->getWidth(), $item->getLength(), $item->getDepth()]; |
|
| 142 | 49 | $itemBDimensions = [$prevItem->getWidth(), $prevItem->getLength(), $prevItem->getDepth()]; |
|
| 143 | 49 | sort($itemADimensions); |
|
| 144 | 49 | sort($itemBDimensions); |
|
| 145 | 49 | $isSame = ($itemADimensions === $itemBDimensions); |
|
| 146 | } |
||
| 147 | |||
| 148 | //Special case items that are the same as what we just packed - keep orientation |
||
| 149 | 53 | if ($isSame && $prevItem) { |
|
| 150 | 42 | $orientationsDimensions[] = [$prevItem->getWidth(), $prevItem->getLength(), $prevItem->getDepth()]; |
|
| 151 | } else { |
||
| 152 | //simple 2D rotation |
||
| 153 | 53 | $orientationsDimensions[] = [$item->getWidth(), $item->getLength(), $item->getDepth()]; |
|
| 154 | 53 | $orientationsDimensions[] = [$item->getLength(), $item->getWidth(), $item->getDepth()]; |
|
| 155 | |||
| 156 | //add 3D rotation if we're allowed |
||
| 157 | 53 | if (!$item->getKeepFlat()) { |
|
| 158 | 34 | $orientationsDimensions[] = [$item->getWidth(), $item->getDepth(), $item->getLength()]; |
|
| 159 | 34 | $orientationsDimensions[] = [$item->getLength(), $item->getDepth(), $item->getWidth()]; |
|
| 160 | 34 | $orientationsDimensions[] = [$item->getDepth(), $item->getWidth(), $item->getLength()]; |
|
| 161 | 34 | $orientationsDimensions[] = [$item->getDepth(), $item->getLength(), $item->getWidth()]; |
|
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | //remove any that simply don't fit |
||
| 166 | 53 | $orientationsDimensions = array_unique($orientationsDimensions, SORT_REGULAR); |
|
| 167 | $orientationsDimensions = array_filter($orientationsDimensions, static function (array $i) use ($widthLeft, $lengthLeft, $depthLeft) { |
||
| 168 | 53 | return $i[0] <= $widthLeft && $i[1] <= $lengthLeft && $i[2] <= $depthLeft; |
|
| 169 | 53 | }); |
|
| 170 | |||
| 171 | 53 | foreach ($orientationsDimensions as $dimensions) { |
|
| 172 | 52 | $orientations[] = new OrientatedItem($item, $dimensions[0], $dimensions[1], $dimensions[2]); |
|
| 173 | } |
||
| 174 | |||
| 175 | 53 | if ($item instanceof ConstrainedPlacementItem) { |
|
| 176 | 2 | $box = $this->box; |
|
| 177 | $orientations = array_filter($orientations, static function (OrientatedItem $i) use ($box, $x, $y, $z, $prevPackedItemList) { |
||
| 178 | 2 | return $i->getItem()->canBePacked($box, $prevPackedItemList, $x, $y, $z, $i->getWidth(), $i->getLength(), $i->getDepth()); |
|
|
1 ignored issue
–
show
|
|||
| 179 | 2 | }); |
|
| 180 | } |
||
| 181 | |||
| 182 | 53 | return $orientations; |
|
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return OrientatedItem[] |
||
| 187 | */ |
||
| 188 | 29 | public function getPossibleOrientationsInEmptyBox(Item $item): array |
|
| 189 | { |
||
| 190 | 29 | $cacheKey = $item->getWidth() . |
|
| 191 | 29 | '|' . |
|
| 192 | 29 | $item->getLength() . |
|
| 193 | 29 | '|' . |
|
| 194 | 29 | $item->getDepth() . |
|
| 195 | 29 | '|' . |
|
| 196 | 29 | ($item->getKeepFlat() ? '2D' : '3D') . |
|
| 197 | 29 | '|' . |
|
| 198 | 29 | $this->box->getInnerWidth() . |
|
| 199 | 29 | '|' . |
|
| 200 | 29 | $this->box->getInnerLength() . |
|
| 201 | 29 | '|' . |
|
| 202 | 29 | $this->box->getInnerDepth(); |
|
| 203 | |||
| 204 | 29 | if (isset(static::$emptyBoxCache[$cacheKey])) { |
|
| 205 | 24 | $orientations = static::$emptyBoxCache[$cacheKey]; |
|
| 206 | } else { |
||
| 207 | 25 | $orientations = $this->getPossibleOrientations( |
|
| 208 | 25 | $item, |
|
| 209 | 25 | null, |
|
| 210 | 25 | $this->box->getInnerWidth(), |
|
| 211 | 25 | $this->box->getInnerLength(), |
|
| 212 | 25 | $this->box->getInnerDepth(), |
|
| 213 | 25 | 0, |
|
| 214 | 25 | 0, |
|
| 215 | 25 | 0, |
|
| 216 | 25 | new PackedItemList() |
|
| 217 | ); |
||
| 218 | 25 | static::$emptyBoxCache[$cacheKey] = $orientations; |
|
| 219 | } |
||
| 220 | |||
| 221 | 29 | return $orientations; |
|
|
1 ignored issue
–
show
|
|||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param OrientatedItem[] $possibleOrientations |
||
| 226 | * |
||
| 227 | * @return OrientatedItem[] |
||
| 228 | */ |
||
| 229 | 52 | protected function getUsableOrientations( |
|
| 230 | Item $item, |
||
| 231 | $possibleOrientations, |
||
| 232 | bool $isLastItem |
||
| 233 | ): array { |
||
| 234 | 52 | $orientationsToUse = $stableOrientations = $unstableOrientations = []; |
|
| 235 | |||
| 236 | // Divide possible orientations into stable (low centre of gravity) and unstable (high centre of gravity) |
||
| 237 | 52 | foreach ($possibleOrientations as $orientation) { |
|
| 238 | 52 | if ($orientation->isStable()) { |
|
| 239 | 50 | $stableOrientations[] = $orientation; |
|
| 240 | } else { |
||
| 241 | 52 | $unstableOrientations[] = $orientation; |
|
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | /* |
||
| 246 | * We prefer to use stable orientations only, but allow unstable ones if either |
||
| 247 | * the item is the last one left to pack OR |
||
| 248 | * the item doesn't fit in the box any other way |
||
| 249 | */ |
||
| 250 | 52 | if (count($stableOrientations) > 0) { |
|
| 251 | 50 | $orientationsToUse = $stableOrientations; |
|
| 252 | 49 | } elseif (count($unstableOrientations) > 0) { |
|
| 253 | 6 | $stableOrientationsInEmptyBox = $this->getStableOrientationsInEmptyBox($item); |
|
| 254 | |||
| 255 | 6 | if ($isLastItem || count($stableOrientationsInEmptyBox) === 0) { |
|
| 256 | 4 | $orientationsToUse = $unstableOrientations; |
|
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | 52 | return $orientationsToUse; |
|
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Return the orientations for this item if it were to be placed into the box with nothing else. |
||
| 265 | */ |
||
| 266 | 6 | protected function getStableOrientationsInEmptyBox(Item $item): array |
|
| 274 | 6 | } |
|
| 275 | ); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Approximation of a forward-looking packing. |
||
| 280 | * |
||
| 281 | * Not an actual packing, that has additional logic regarding constraints and stackability, this focuses |
||
| 282 | * purely on fit. |
||
| 283 | */ |
||
| 284 | 17 | protected function calculateAdditionalItemsPackedWithThisOrientation( |
|
| 350 |