Conditions | 12 |
Paths | 10 |
Total Lines | 82 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Tests | 52 |
CRAP Score | 12 |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
80 | 68 | public function packLayer(ItemList &$items, PackedItemList $packedItemList, int $startX, int $startY, int $startZ, int $widthForLayer, int $lengthForLayer, int $depthForLayer, int $guidelineLayerDepth, bool $considerStability): PackedLayer |
|
81 | { |
||
82 | 68 | $layer = new PackedLayer(); |
|
83 | 68 | $x = $startX; |
|
84 | 68 | $y = $startY; |
|
85 | 68 | $z = $startZ; |
|
86 | 68 | $lengthLeft = $lengthForLayer; |
|
87 | 68 | $rowLength = 0; |
|
88 | 68 | $prevItem = null; |
|
89 | 68 | $skippedItems = []; |
|
90 | 68 | $remainingWeightAllowed = $this->box->getMaxWeight() - $this->box->getEmptyWeight() - $packedItemList->getWeight(); |
|
91 | |||
92 | 68 | while ($items->count() > 0) { |
|
93 | 68 | $itemToPack = $items->extract(); |
|
94 | |||
95 | //skip items that will never fit e.g. too heavy |
||
96 | 68 | if ($itemToPack->getWeight() > $remainingWeightAllowed) { |
|
97 | 8 | continue; |
|
98 | } |
||
99 | |||
100 | 68 | $orientatedItem = $this->orientatedItemFactory->getBestOrientation($itemToPack, $prevItem, $items, $widthForLayer - $x, $lengthLeft, $depthForLayer, $rowLength, $x, $y, $z, $packedItemList, $considerStability); |
|
101 | |||
102 | 68 | if ($orientatedItem instanceof OrientatedItem) { |
|
103 | 68 | $packedItem = PackedItem::fromOrientatedItem($orientatedItem, $x, $y, $z); |
|
104 | 68 | $layer->insert($packedItem); |
|
105 | 68 | $remainingWeightAllowed -= $itemToPack->getWeight(); |
|
106 | 68 | $packedItemList->insert($packedItem); |
|
107 | |||
108 | 68 | $rowLength = max($rowLength, $packedItem->getLength()); |
|
109 | |||
110 | //Figure out if we can stack the next item vertically on top of this rather than side by side |
||
111 | //e.g. when we've packed a tall item, and have just put a shorter one next to it. |
||
112 | 68 | $this->packVerticallyInsideItemFootprint($layer, $packedItem, $packedItemList, $items, $remainingWeightAllowed, $guidelineLayerDepth, $rowLength, $x, $y, $z, $considerStability); |
|
113 | |||
114 | 68 | $prevItem = $orientatedItem; |
|
115 | |||
116 | //Having now placed an item, there is space *within the same row* along the length. Pack into that. |
||
117 | 68 | if (!$this->singlePassMode && $rowLength - $orientatedItem->getLength() > 0) { |
|
118 | 18 | $layer->merge($this->packLayer($items, $packedItemList, $x, $y + $orientatedItem->getLength(), $z, $widthForLayer, $rowLength - $orientatedItem->getLength(), $depthForLayer, $layer->getDepth(), $considerStability)); |
|
119 | } |
||
120 | |||
121 | 68 | $x += $packedItem->getWidth(); |
|
122 | |||
123 | 68 | if ($items->count() === 0 && $skippedItems) { |
|
|
|||
124 | 10 | $items = ItemList::fromArray(array_merge($skippedItems, iterator_to_array($items)), true); |
|
125 | 10 | $skippedItems = []; |
|
126 | } |
||
127 | 68 | continue; |
|
128 | } |
||
129 | |||
130 | 60 | if ($items->count() > 0) { // skip for now, move on to the next item |
|
131 | 50 | $this->logger->debug("doesn't fit, skipping for now"); |
|
132 | 50 | $skippedItems[] = $itemToPack; |
|
133 | // abandon here if next item is the same, no point trying to keep going. Last time is not skipped, need that to trigger appropriate reset logic |
||
134 | 50 | while ($items->count() > 1 && static::isSameDimensions($itemToPack, $items->top())) { |
|
135 | 36 | $skippedItems[] = $items->extract(); |
|
136 | } |
||
137 | 50 | continue; |
|
138 | } |
||
139 | |||
140 | 60 | if ($x > $startX) { |
|
141 | 60 | $this->logger->debug('No more fit in width wise, resetting for new row'); |
|
142 | 60 | $lengthLeft -= $rowLength; |
|
143 | 60 | $y += $rowLength; |
|
144 | 60 | $x = $startX; |
|
145 | 60 | $rowLength = 0; |
|
146 | 60 | $skippedItems[] = $itemToPack; |
|
147 | 60 | $items = ItemList::fromArray($skippedItems, true); |
|
148 | 60 | $skippedItems = []; |
|
149 | 60 | $prevItem = null; |
|
150 | 60 | continue; |
|
151 | } |
||
152 | |||
153 | 52 | $this->logger->debug('no items fit, so starting next vertical layer'); |
|
154 | 52 | $skippedItems[] = $itemToPack; |
|
155 | |||
156 | 52 | $items = ItemList::fromArray(array_merge($skippedItems, iterator_to_array($items)), true); |
|
157 | |||
158 | 52 | return $layer; |
|
159 | } |
||
160 | |||
161 | 68 | return $layer; |
|
162 | } |
||
226 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.