| Total Complexity | 56 |
| Total Lines | 330 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 18 | ||
| 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 |
||
| 26 | class OrientatedItemFactory implements LoggerAwareInterface |
||
| 27 | { |
||
| 28 | use LoggerAwareTrait; |
||
| 29 | |||
| 30 | /** @var Box */ |
||
| 31 | protected $box; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var OrientatedItem[] |
||
| 35 | */ |
||
| 36 | protected static $emptyBoxCache = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var int[] |
||
| 40 | */ |
||
| 41 | protected static $lookaheadCache = []; |
||
| 42 | |||
| 43 | 55 | public function __construct(Box $box) |
|
| 44 | { |
||
| 45 | 55 | $this->box = $box; |
|
| 46 | 55 | $this->logger = new NullLogger(); |
|
| 47 | 55 | } |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Get the best orientation for an item. |
||
| 51 | */ |
||
| 52 | 53 | public function getBestOrientation( |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Find all possible orientations for an item. |
||
| 140 | * |
||
| 141 | * @return OrientatedItem[] |
||
| 142 | */ |
||
| 143 | 54 | public function getPossibleOrientations( |
|
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return OrientatedItem[] |
||
| 197 | */ |
||
| 198 | 29 | public function getPossibleOrientationsInEmptyBox(Item $item): array |
|
| 199 | { |
||
| 200 | 29 | $cacheKey = $item->getWidth() . |
|
| 201 | 29 | '|' . |
|
| 202 | 29 | $item->getLength() . |
|
| 203 | 29 | '|' . |
|
| 204 | 29 | $item->getDepth() . |
|
| 205 | 29 | '|' . |
|
| 206 | 29 | ($item->getKeepFlat() ? '2D' : '3D') . |
|
| 207 | 29 | '|' . |
|
| 208 | 29 | $this->box->getInnerWidth() . |
|
| 209 | 29 | '|' . |
|
| 210 | 29 | $this->box->getInnerLength() . |
|
| 211 | 29 | '|' . |
|
| 212 | 29 | $this->box->getInnerDepth(); |
|
| 213 | |||
| 214 | 29 | if (isset(static::$emptyBoxCache[$cacheKey])) { |
|
| 215 | 23 | $orientations = static::$emptyBoxCache[$cacheKey]; |
|
| 216 | } else { |
||
| 217 | 25 | $orientations = $this->getPossibleOrientations( |
|
| 218 | 25 | $item, |
|
| 219 | 25 | null, |
|
| 220 | 25 | $this->box->getInnerWidth(), |
|
| 221 | 25 | $this->box->getInnerLength(), |
|
| 222 | 25 | $this->box->getInnerDepth(), |
|
| 223 | 25 | 0, |
|
| 224 | 25 | 0, |
|
| 225 | 25 | 0, |
|
| 226 | 25 | new PackedItemList() |
|
| 227 | ); |
||
| 228 | 25 | static::$emptyBoxCache[$cacheKey] = $orientations; |
|
| 229 | } |
||
| 230 | |||
| 231 | 29 | return $orientations; |
|
|
1 ignored issue
–
show
|
|||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param OrientatedItem[] $possibleOrientations |
||
| 236 | * |
||
| 237 | * @return OrientatedItem[] |
||
| 238 | */ |
||
| 239 | 53 | protected function getUsableOrientations( |
|
| 240 | Item $item, |
||
| 241 | array $possibleOrientations |
||
| 242 | ): array { |
||
| 243 | 53 | $orientationsToUse = $stableOrientations = $unstableOrientations = []; |
|
| 244 | |||
| 245 | // Divide possible orientations into stable (low centre of gravity) and unstable (high centre of gravity) |
||
| 246 | 53 | foreach ($possibleOrientations as $orientation) { |
|
| 247 | 53 | if ($orientation->isStable() || $this->box->getInnerDepth() === $orientation->getDepth()) { |
|
| 248 | 51 | $stableOrientations[] = $orientation; |
|
| 249 | } else { |
||
| 250 | 7 | $unstableOrientations[] = $orientation; |
|
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | /* |
||
| 255 | * We prefer to use stable orientations only, but allow unstable ones if |
||
| 256 | * the item doesn't fit in the box any other way |
||
| 257 | */ |
||
| 258 | 53 | if (count($stableOrientations) > 0) { |
|
| 259 | 51 | $orientationsToUse = $stableOrientations; |
|
| 260 | 47 | } elseif (count($unstableOrientations) > 0) { |
|
| 261 | 7 | $stableOrientationsInEmptyBox = $this->getStableOrientationsInEmptyBox($item); |
|
| 262 | |||
| 263 | 7 | if (count($stableOrientationsInEmptyBox) === 0) { |
|
| 264 | 5 | $orientationsToUse = $unstableOrientations; |
|
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | 53 | return $orientationsToUse; |
|
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Return the orientations for this item if it were to be placed into the box with nothing else. |
||
| 273 | */ |
||
| 274 | 7 | protected function getStableOrientationsInEmptyBox(Item $item): array |
|
| 282 | 7 | } |
|
| 283 | ); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Approximation of a forward-looking packing. |
||
| 288 | * |
||
| 289 | * Not an actual packing, that has additional logic regarding constraints and stackability, this focuses |
||
| 290 | * purely on fit. |
||
| 291 | */ |
||
| 292 | 33 | protected function calculateAdditionalItemsPackedWithThisOrientation( |
|
| 356 | } |
||
| 357 | } |
||
| 358 |