| Conditions | 14 |
| Paths | 16 |
| Total Lines | 78 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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:
| 1 | <?php |
||
| 49 | public function pack() |
||
| 50 | { |
||
| 51 | $this->logger->log(LogLevel::DEBUG, "[EVALUATING BOX] {$this->box->getReference()}"); |
||
| 52 | |||
| 53 | $packedItems = new ItemList; |
||
| 54 | $remainingDepth = $this->box->getInnerDepth(); |
||
| 55 | $remainingWeight = $this->box->getMaxWeight() - $this->box->getEmptyWeight(); |
||
| 56 | $remainingWidth = $this->box->getInnerWidth(); |
||
| 57 | $remainingLength = $this->box->getInnerLength(); |
||
| 58 | |||
| 59 | $layerWidth = $layerLength = $layerDepth = 0; |
||
| 60 | while (!$this->items->isEmpty()) { |
||
| 61 | |||
| 62 | $itemToPack = $this->items->top(); |
||
| 63 | |||
| 64 | //skip items that are simply too large |
||
| 65 | if ($this->isItemTooLargeForBox($itemToPack, $remainingDepth, $remainingWeight)) { |
||
| 66 | $this->items->extract(); |
||
| 67 | continue; |
||
| 68 | } |
||
| 69 | |||
| 70 | $this->logger->log(LogLevel::DEBUG, "evaluating item {$itemToPack->getDescription()}"); |
||
| 71 | $this->logger->log(LogLevel::DEBUG, "remaining width: {$remainingWidth}, length: {$remainingLength}, depth: {$remainingDepth}"); |
||
| 72 | $this->logger->log(LogLevel::DEBUG, "layerWidth: {$layerWidth}, layerLength: {$layerLength}, layerDepth: {$layerDepth}"); |
||
| 73 | |||
| 74 | $itemWidth = $itemToPack->getWidth(); |
||
| 75 | $itemLength = $itemToPack->getLength(); |
||
| 76 | |||
| 77 | if ($this->fitsGap($itemToPack, $remainingWidth, $remainingLength)) { |
||
| 78 | |||
| 79 | $packedItems->insert($this->items->extract()); |
||
| 80 | $remainingWeight -= $itemToPack->getWeight(); |
||
| 81 | |||
| 82 | $nextItem = !$this->items->isEmpty() ? $this->items->top() : null; |
||
| 83 | if ($this->fitsBetterRotated($itemToPack, $nextItem, $remainingWidth, $remainingLength)) { |
||
| 84 | $this->logger->log(LogLevel::DEBUG, "fits (better) unrotated"); |
||
| 85 | $remainingLength -= $itemLength; |
||
| 86 | $layerLength += $itemLength; |
||
| 87 | $layerWidth = max($itemWidth, $layerWidth); |
||
| 88 | } else { |
||
| 89 | $this->logger->log(LogLevel::DEBUG, "fits (better) rotated"); |
||
| 90 | $remainingLength -= $itemWidth; |
||
| 91 | $layerLength += $itemWidth; |
||
| 92 | $layerWidth = max($itemLength, $layerWidth); |
||
| 93 | } |
||
| 94 | $layerDepth = max($layerDepth, $itemToPack->getDepth()); //greater than 0, items will always be less deep |
||
| 95 | |||
| 96 | //allow items to be stacked in place within the same footprint up to current layerdepth |
||
| 97 | $maxStackDepth = $layerDepth - $itemToPack->getDepth(); |
||
| 98 | while (!$this->items->isEmpty() && $this->canStackItemInLayer($itemToPack, $this->items->top(), $maxStackDepth, $remainingWeight)) { |
||
| 99 | $remainingWeight -= $this->items->top()->getWeight(); |
||
| 100 | $maxStackDepth -= $this->items->top()->getDepth(); |
||
| 101 | $packedItems->insert($this->items->extract()); |
||
| 102 | } |
||
| 103 | } else { |
||
| 104 | if ($remainingWidth >= min($itemWidth, $itemLength) && $this->isLayerStarted($layerWidth, $layerLength, $layerDepth)) { |
||
| 105 | $this->logger->log(LogLevel::DEBUG, "No more fit in lengthwise, resetting for new row"); |
||
| 106 | $remainingLength += $layerLength; |
||
| 107 | $remainingWidth -= $layerWidth; |
||
| 108 | $layerWidth = $layerLength = 0; |
||
| 109 | continue; |
||
| 110 | } elseif ($remainingLength < min($itemWidth, $itemLength) || $layerDepth == 0) { |
||
| 111 | $this->logger->log(LogLevel::DEBUG, "doesn't fit on layer even when empty"); |
||
| 112 | $this->items->extract(); |
||
| 113 | continue; |
||
| 114 | } |
||
| 115 | |||
| 116 | $remainingWidth = $layerWidth ? min(floor($layerWidth * 1.1), $this->box->getInnerWidth()) : $this->box->getInnerWidth(); |
||
| 117 | $remainingLength = $layerLength ? min(floor($layerLength * 1.1), $this->box->getInnerLength()) : $this->box->getInnerLength(); |
||
| 118 | $remainingDepth -= $layerDepth; |
||
| 119 | |||
| 120 | $layerWidth = $layerLength = $layerDepth = 0; |
||
| 121 | $this->logger->log(LogLevel::DEBUG, "doesn't fit, so starting next vertical layer"); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | $this->logger->log(LogLevel::DEBUG, "done with this box"); |
||
| 125 | return new PackedBox($this->box, $packedItems, $remainingWidth, $remainingLength, $remainingDepth, $remainingWeight); |
||
| 126 | } |
||
| 127 | |||
| 216 |