| Total Complexity | 40 |
| Total Lines | 337 |
| Duplicated Lines | 0 % |
| Coverage | 96.86% |
| Changes | 14 | ||
| 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 | 23 | public function __construct(Box $box) |
|
| 45 | { |
||
| 46 | 23 | $this->box = $box; |
|
| 47 | 23 | $this->logger = new NullLogger(); |
|
| 48 | 23 | } |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Get the best orientation for an item. |
||
| 52 | */ |
||
| 53 | 22 | public function getBestOrientation( |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Find all possible orientations for an item. |
||
| 122 | * |
||
| 123 | * @return OrientatedItem[] |
||
| 124 | */ |
||
| 125 | 22 | public function getPossibleOrientations( |
|
| 126 | Item $item, |
||
| 127 | ?OrientatedItem $prevItem, |
||
| 128 | int $widthLeft, |
||
| 129 | int $lengthLeft, |
||
| 130 | int $depthLeft, |
||
| 131 | int $x, |
||
| 132 | int $y, |
||
| 133 | int $z, |
||
| 134 | PackedItemList $prevPackedItemList |
||
| 135 | ): array { |
||
| 136 | 22 | $orientations = $orientationsDimensions = []; |
|
| 137 | |||
| 138 | 22 | $isSame = false; |
|
| 139 | 22 | if ($prevItem) { |
|
| 140 | 21 | $itemADimensions = [$item->getWidth(), $item->getLength(), $item->getDepth()]; |
|
| 141 | 21 | $itemBDimensions = [$prevItem->getWidth(), $prevItem->getLength(), $prevItem->getDepth()]; |
|
| 142 | 21 | sort($itemADimensions); |
|
| 143 | 21 | sort($itemBDimensions); |
|
| 144 | 21 | $isSame = ($itemADimensions === $itemBDimensions); |
|
| 145 | } |
||
| 146 | |||
| 147 | //Special case items that are the same as what we just packed - keep orientation |
||
| 148 | 22 | if ($isSame && $prevItem) { |
|
| 149 | 17 | $orientationsDimensions[] = [$prevItem->getWidth(), $prevItem->getLength(), $prevItem->getDepth()]; |
|
| 150 | } else { |
||
| 151 | //simple 2D rotation |
||
| 152 | 22 | $orientationsDimensions[] = [$item->getWidth(), $item->getLength(), $item->getDepth()]; |
|
| 153 | 22 | $orientationsDimensions[] = [$item->getLength(), $item->getWidth(), $item->getDepth()]; |
|
| 154 | |||
| 155 | //add 3D rotation if we're allowed |
||
| 156 | 22 | if (!$item->getKeepFlat()) { |
|
| 157 | 14 | $orientationsDimensions[] = [$item->getWidth(), $item->getDepth(), $item->getLength()]; |
|
| 158 | 14 | $orientationsDimensions[] = [$item->getLength(), $item->getDepth(), $item->getWidth()]; |
|
| 159 | 14 | $orientationsDimensions[] = [$item->getDepth(), $item->getWidth(), $item->getLength()]; |
|
| 160 | 14 | $orientationsDimensions[] = [$item->getDepth(), $item->getLength(), $item->getWidth()]; |
|
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | //remove any that simply don't fit |
||
| 165 | 22 | $orientationsDimensions = array_unique($orientationsDimensions, SORT_REGULAR); |
|
| 166 | $orientationsDimensions = array_filter($orientationsDimensions, static function (array $i) use ($widthLeft, $lengthLeft, $depthLeft) { |
||
| 167 | 22 | return $i[0] <= $widthLeft && $i[1] <= $lengthLeft && $i[2] <= $depthLeft; |
|
| 168 | 22 | }); |
|
| 169 | |||
| 170 | 22 | foreach ($orientationsDimensions as $dimensions) { |
|
| 171 | 22 | $orientations[] = new OrientatedItem($item, $dimensions[0], $dimensions[1], $dimensions[2]); |
|
| 172 | } |
||
| 173 | |||
| 174 | 22 | if ($item instanceof ConstrainedPlacementItem) { |
|
| 175 | $box = $this->box; |
||
| 176 | $orientations = array_filter($orientations, static function (OrientatedItem $i) use ($box, $x, $y, $z, $prevPackedItemList) { |
||
| 177 | /** @var ConstrainedPlacementItem $constrainedItem */ |
||
| 178 | $constrainedItem = $i->getItem(); |
||
| 179 | |||
| 180 | return $constrainedItem->canBePacked($box, $prevPackedItemList, $x, $y, $z, $i->getWidth(), $i->getLength(), $i->getDepth()); |
||
| 181 | }); |
||
| 182 | } |
||
| 183 | |||
| 184 | 22 | return $orientations; |
|
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return OrientatedItem[] |
||
| 189 | */ |
||
| 190 | 8 | public function getPossibleOrientationsInEmptyBox(Item $item): array |
|
| 191 | { |
||
| 192 | 8 | $cacheKey = $item->getWidth() . |
|
| 193 | 8 | '|' . |
|
| 194 | 8 | $item->getLength() . |
|
| 195 | 8 | '|' . |
|
| 196 | 8 | $item->getDepth() . |
|
| 197 | 8 | '|' . |
|
| 198 | 8 | ($item->getKeepFlat() ? '2D' : '3D') . |
|
| 199 | 8 | '|' . |
|
| 200 | 8 | $this->box->getInnerWidth() . |
|
| 201 | 8 | '|' . |
|
| 202 | 8 | $this->box->getInnerLength() . |
|
| 203 | 8 | '|' . |
|
| 204 | 8 | $this->box->getInnerDepth(); |
|
| 205 | |||
| 206 | 8 | if (isset(static::$emptyBoxCache[$cacheKey])) { |
|
| 207 | 8 | $orientations = static::$emptyBoxCache[$cacheKey]; |
|
| 208 | } else { |
||
| 209 | 7 | $orientations = $this->getPossibleOrientations( |
|
| 210 | 7 | $item, |
|
| 211 | 7 | null, |
|
| 212 | 7 | $this->box->getInnerWidth(), |
|
| 213 | 7 | $this->box->getInnerLength(), |
|
| 214 | 7 | $this->box->getInnerDepth(), |
|
| 215 | 7 | 0, |
|
| 216 | 7 | 0, |
|
| 217 | 7 | 0, |
|
| 218 | 7 | new PackedItemList() |
|
| 219 | ); |
||
| 220 | 7 | static::$emptyBoxCache[$cacheKey] = $orientations; |
|
| 221 | } |
||
| 222 | |||
| 223 | 8 | return $orientations; |
|
|
1 ignored issue
–
show
|
|||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param OrientatedItem[] $possibleOrientations |
||
| 228 | * |
||
| 229 | * @return OrientatedItem[] |
||
| 230 | */ |
||
| 231 | 22 | protected function getUsableOrientations( |
|
| 232 | Item $item, |
||
| 233 | $possibleOrientations, |
||
| 234 | bool $isLastItem |
||
| 235 | ): array { |
||
| 236 | 22 | $orientationsToUse = $stableOrientations = $unstableOrientations = []; |
|
| 237 | |||
| 238 | // Divide possible orientations into stable (low centre of gravity) and unstable (high centre of gravity) |
||
| 239 | 22 | foreach ($possibleOrientations as $orientation) { |
|
| 240 | 22 | if ($orientation->isStable()) { |
|
| 241 | 22 | $stableOrientations[] = $orientation; |
|
| 242 | } else { |
||
| 243 | 1 | $unstableOrientations[] = $orientation; |
|
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | /* |
||
| 248 | * We prefer to use stable orientations only, but allow unstable ones if either |
||
| 249 | * the item is the last one left to pack OR |
||
| 250 | * the item doesn't fit in the box any other way |
||
| 251 | */ |
||
| 252 | 22 | if (count($stableOrientations) > 0) { |
|
| 253 | 22 | $orientationsToUse = $stableOrientations; |
|
| 254 | 21 | } elseif (count($unstableOrientations) > 0) { |
|
| 255 | 1 | $stableOrientationsInEmptyBox = $this->getStableOrientationsInEmptyBox($item); |
|
| 256 | |||
| 257 | 1 | if ($isLastItem || count($stableOrientationsInEmptyBox) === 0) { |
|
| 258 | $orientationsToUse = $unstableOrientations; |
||
| 259 | } |
||
| 260 | } |
||
| 261 | |||
| 262 | 22 | return $orientationsToUse; |
|
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Return the orientations for this item if it were to be placed into the box with nothing else. |
||
| 267 | */ |
||
| 268 | 1 | protected function getStableOrientationsInEmptyBox(Item $item): array |
|
| 276 | 1 | } |
|
| 277 | ); |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Compare two items to see if they have same dimensions. |
||
| 282 | */ |
||
| 283 | 4 | public function isSameDimensions(Item $itemA, Item $itemB): bool |
|
| 284 | { |
||
| 285 | 4 | $itemADimensions = [$itemA->getWidth(), $itemA->getLength(), $itemA->getDepth()]; |
|
| 286 | 4 | $itemBDimensions = [$itemB->getWidth(), $itemB->getLength(), $itemB->getDepth()]; |
|
| 287 | 4 | sort($itemADimensions); |
|
| 288 | 4 | sort($itemBDimensions); |
|
| 289 | |||
| 290 | 4 | return $itemADimensions === $itemBDimensions; |
|
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Approximation of a forward-looking packing. |
||
| 295 | * |
||
| 296 | * Not an actual packing, that has additional logic regarding constraints and stackability, this focuses |
||
| 297 | * purely on fit. |
||
| 298 | */ |
||
| 299 | 3 | protected function calculateAdditionalItemsPackedWithThisOrientation( |
|
| 364 | } |
||
| 365 | } |
||
| 366 |