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