| Total Complexity | 55 |
| Total Lines | 351 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 20 | ||
| 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) |
|
| 50 | { |
||
| 51 | 58 | $this->box = $box; |
|
| 52 | 58 | $this->logger = new NullLogger(); |
|
| 53 | 58 | } |
|
| 54 | |||
| 55 | 51 | public function setSinglePassMode(bool $singlePassMode): void |
|
| 56 | { |
||
| 57 | 51 | $this->singlePassMode = $singlePassMode; |
|
| 58 | 51 | } |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Get the best orientation for an item. |
||
| 62 | */ |
||
| 63 | 56 | public function getBestOrientation( |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Find all possible orientations for an item. |
||
| 156 | * |
||
| 157 | * @return OrientatedItem[] |
||
| 158 | */ |
||
| 159 | 57 | public function getPossibleOrientations( |
|
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return OrientatedItem[] |
||
| 213 | */ |
||
| 214 | 32 | public function getPossibleOrientationsInEmptyBox(Item $item): array |
|
| 215 | { |
||
| 216 | 32 | $cacheKey = $item->getWidth() . |
|
| 217 | 32 | '|' . |
|
| 218 | 32 | $item->getLength() . |
|
| 219 | 32 | '|' . |
|
| 220 | 32 | $item->getDepth() . |
|
| 221 | 32 | '|' . |
|
| 222 | 32 | ($item->getKeepFlat() ? '2D' : '3D') . |
|
| 223 | 32 | '|' . |
|
| 224 | 32 | $this->box->getInnerWidth() . |
|
| 225 | 32 | '|' . |
|
| 226 | 32 | $this->box->getInnerLength() . |
|
| 227 | 32 | '|' . |
|
| 228 | 32 | $this->box->getInnerDepth(); |
|
| 229 | |||
| 230 | 32 | if (isset(static::$emptyBoxCache[$cacheKey])) { |
|
| 231 | 26 | $orientations = static::$emptyBoxCache[$cacheKey]; |
|
| 232 | } else { |
||
| 233 | 28 | $orientations = $this->getPossibleOrientations( |
|
| 234 | 28 | $item, |
|
| 235 | 28 | null, |
|
| 236 | 28 | $this->box->getInnerWidth(), |
|
| 237 | 28 | $this->box->getInnerLength(), |
|
| 238 | 28 | $this->box->getInnerDepth(), |
|
| 239 | 28 | 0, |
|
| 240 | 28 | 0, |
|
| 241 | 28 | 0, |
|
| 242 | 28 | new PackedItemList() |
|
| 243 | ); |
||
| 244 | 28 | static::$emptyBoxCache[$cacheKey] = $orientations; |
|
| 245 | } |
||
| 246 | |||
| 247 | 32 | return $orientations; |
|
|
1 ignored issue
–
show
|
|||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param OrientatedItem[] $possibleOrientations |
||
| 252 | * |
||
| 253 | * @return OrientatedItem[] |
||
| 254 | */ |
||
| 255 | 56 | protected function getUsableOrientations( |
|
| 256 | Item $item, |
||
| 257 | array $possibleOrientations |
||
| 258 | ): array { |
||
| 259 | 56 | $orientationsToUse = $stableOrientations = $unstableOrientations = []; |
|
| 260 | |||
| 261 | // Divide possible orientations into stable (low centre of gravity) and unstable (high centre of gravity) |
||
| 262 | 56 | foreach ($possibleOrientations as $orientation) { |
|
| 263 | 56 | if ($orientation->isStable() || $this->box->getInnerDepth() === $orientation->getDepth()) { |
|
| 264 | 54 | $stableOrientations[] = $orientation; |
|
| 265 | } else { |
||
| 266 | 9 | $unstableOrientations[] = $orientation; |
|
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | /* |
||
| 271 | * We prefer to use stable orientations only, but allow unstable ones if |
||
| 272 | * the item doesn't fit in the box any other way |
||
| 273 | */ |
||
| 274 | 56 | if (count($stableOrientations) > 0) { |
|
| 275 | 54 | $orientationsToUse = $stableOrientations; |
|
| 276 | 50 | } elseif (count($unstableOrientations) > 0) { |
|
| 277 | 9 | $stableOrientationsInEmptyBox = $this->getStableOrientationsInEmptyBox($item); |
|
| 278 | |||
| 279 | 9 | if (count($stableOrientationsInEmptyBox) === 0) { |
|
| 280 | 6 | $orientationsToUse = $unstableOrientations; |
|
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | 56 | return $orientationsToUse; |
|
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Return the orientations for this item if it were to be placed into the box with nothing else. |
||
| 289 | */ |
||
| 290 | 9 | protected function getStableOrientationsInEmptyBox(Item $item): array |
|
| 298 | 9 | } |
|
| 299 | ); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Approximation of a forward-looking packing. |
||
| 304 | * |
||
| 305 | * Not an actual packing, that has additional logic regarding constraints and stackability, this focuses |
||
| 306 | * purely on fit. |
||
| 307 | */ |
||
| 308 | 44 | protected function calculateAdditionalItemsPackedWithThisOrientation( |
|
| 376 | } |
||
| 377 | } |
||
| 378 |
This check looks for imports that have been defined, but are not used in the scope.