| Conditions | 10 |
| Paths | 37 |
| Total Lines | 51 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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:
| 1 | <?php |
||
| 146 | public function doVolumePacking(): PackedBoxList |
||
| 147 | { |
||
| 148 | |||
| 149 | $packedBoxes = new PackedBoxList; |
||
| 150 | |||
| 151 | //Keep going until everything packed |
||
| 152 | while ($this->items->count()) { |
||
| 153 | $packedBoxesIteration = []; |
||
| 154 | |||
| 155 | //Loop through boxes starting with smallest, see what happens |
||
| 156 | foreach ($this->boxes as $box) { |
||
| 157 | $volumePacker = new VolumePacker($box, clone $this->items); |
||
| 158 | $volumePacker->setLogger($this->logger); |
||
| 159 | $packedBox = $volumePacker->pack(); |
||
| 160 | if ($packedBox->getItems()->count()) { |
||
| 161 | $packedBoxesIteration[] = $packedBox; |
||
| 162 | |||
| 163 | //Have we found a single box that contains everything? |
||
| 164 | if ($packedBox->getItems()->count() === $this->items->count()) { |
||
| 165 | break; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | //Check iteration was productive |
||
| 171 | if (!$packedBoxesIteration) { |
||
|
|
|||
| 172 | throw new ItemTooLargeException('Item ' . $this->items->top()->getDescription() . ' is too large to fit into any box', $this->items->top()); |
||
| 173 | } |
||
| 174 | |||
| 175 | //Find best box of iteration, and remove packed items from unpacked list |
||
| 176 | $bestBox = $this->findBestBoxFromIteration($packedBoxesIteration); |
||
| 177 | $unPackedItems = iterator_to_array($this->items, false); |
||
| 178 | foreach ($bestBox->getItems() as $packedItem) { |
||
| 179 | foreach ($unPackedItems as $unpackedKey => $unpackedItem) { |
||
| 180 | if ($packedItem->getItem() === $unpackedItem) { |
||
| 181 | unset($unPackedItems[$unpackedKey]); |
||
| 182 | break; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | } |
||
| 186 | $unpackedItemList = new ItemList(); |
||
| 187 | foreach ($unPackedItems as $unpackedItem) { |
||
| 188 | $unpackedItemList->insert($unpackedItem); |
||
| 189 | } |
||
| 190 | $this->items = $unpackedItemList; |
||
| 191 | $packedBoxes->insert($bestBox); |
||
| 192 | |||
| 193 | } |
||
| 194 | |||
| 195 | return $packedBoxes; |
||
| 196 | } |
||
| 197 | |||
| 227 |
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.