| Conditions | 12 |
| Paths | 18 |
| Total Lines | 98 |
| Code Lines | 65 |
| 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 |
||
| 101 | public function pack() |
||
| 102 | { |
||
| 103 | $this->logger->debug("[EVALUATING BOX] {$this->box->getReference()}"); |
||
| 104 | |||
| 105 | $packedItems = new ItemList; |
||
| 106 | |||
| 107 | $layerWidth = $layerLength = $layerDepth = 0; |
||
| 108 | |||
| 109 | $prevItem = null; |
||
| 110 | |||
| 111 | while (!$this->items->isEmpty()) { |
||
| 112 | |||
| 113 | $itemToPack = $this->items->extract(); |
||
| 114 | |||
| 115 | //skip items that are simply too heavy |
||
| 116 | if ($itemToPack->getWeight() > $this->remainingWeight) { |
||
| 117 | continue; |
||
| 118 | } |
||
| 119 | |||
| 120 | $this->logger->debug( |
||
| 121 | "evaluating item {$itemToPack->getDescription()}", |
||
| 122 | [ |
||
| 123 | 'item' => $itemToPack, |
||
| 124 | 'space' => [ |
||
| 125 | 'widthLeft' => $this->widthLeft, |
||
| 126 | 'lengthLeft' => $this->lengthLeft, |
||
| 127 | 'depthLeft' => $this->depthLeft, |
||
| 128 | 'layerWidth' => $layerWidth, |
||
| 129 | 'layerLength' => $layerLength, |
||
| 130 | 'layerDepth' => $layerDepth |
||
| 131 | ] |
||
| 132 | ] |
||
| 133 | ); |
||
| 134 | |||
| 135 | $nextItem = !$this->items->isEmpty() ? $this->items->top() : null; |
||
| 136 | $orientatedItem = $this->findBestOrientation($itemToPack, $prevItem, $nextItem, $this->widthLeft, $this->lengthLeft, $this->depthLeft); |
||
| 137 | |||
| 138 | if ($orientatedItem) { |
||
| 139 | |||
| 140 | $packedItems->insert($orientatedItem->getItem()); |
||
| 141 | $this->remainingWeight -= $itemToPack->getWeight(); |
||
| 142 | |||
| 143 | $this->lengthLeft -= $orientatedItem->getLength(); |
||
| 144 | $layerLength += $orientatedItem->getLength(); |
||
| 145 | $layerWidth = max($orientatedItem->getWidth(), $layerWidth); |
||
| 146 | |||
| 147 | $layerDepth = max($layerDepth, $orientatedItem->getDepth()); //greater than 0, items will always be less deep |
||
| 148 | |||
| 149 | $this->usedLength = max($this->usedLength, $layerLength); |
||
| 150 | $this->usedWidth = max($this->usedWidth, $layerWidth); |
||
| 151 | |||
| 152 | //allow items to be stacked in place within the same footprint up to current layerdepth |
||
| 153 | $stackableDepth = $layerDepth - $orientatedItem->getDepth(); |
||
| 154 | $this->tryAndStackItemsIntoSpace($packedItems, $prevItem, $nextItem, $orientatedItem->getWidth(), $orientatedItem->getLength(), $stackableDepth); |
||
| 155 | |||
| 156 | $prevItem = $orientatedItem; |
||
| 157 | |||
| 158 | if (!$nextItem) { |
||
| 159 | $this->usedDepth += $layerDepth; |
||
| 160 | } |
||
| 161 | } else { |
||
| 162 | |||
| 163 | $prevItem = null; |
||
| 164 | |||
| 165 | if ($this->widthLeft >= min($itemToPack->getWidth(), $itemToPack->getLength()) && $this->isLayerStarted($layerWidth, $layerLength, $layerDepth)) { |
||
| 166 | $this->logger->debug("No more fit in lengthwise, resetting for new row"); |
||
| 167 | $this->lengthLeft += $layerLength; |
||
| 168 | $this->widthLeft -= $layerWidth; |
||
| 169 | $layerWidth = $layerLength = 0; |
||
| 170 | $this->items->insert($itemToPack); |
||
| 171 | continue; |
||
| 172 | } elseif ($this->lengthLeft < min($itemToPack->getWidth(), $itemToPack->getLength()) || $layerDepth == 0) { |
||
| 173 | $this->logger->debug("doesn't fit on layer even when empty"); |
||
| 174 | continue; |
||
| 175 | } |
||
| 176 | |||
| 177 | $this->widthLeft = $layerWidth ? min(floor($layerWidth * 1.1), $this->box->getInnerWidth()) : $this->box->getInnerWidth(); |
||
|
|
|||
| 178 | $this->lengthLeft = $layerLength ? min(floor($layerLength * 1.1), $this->box->getInnerLength()) : $this->box->getInnerLength(); |
||
| 179 | $this->depthLeft -= $layerDepth; |
||
| 180 | $this->usedDepth += $layerDepth; |
||
| 181 | |||
| 182 | $layerWidth = $layerLength = $layerDepth = 0; |
||
| 183 | $this->logger->debug("doesn't fit, so starting next vertical layer"); |
||
| 184 | $this->items->insert($itemToPack); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | $this->logger->debug("done with this box"); |
||
| 188 | return new PackedBox( |
||
| 189 | $this->box, |
||
| 190 | $packedItems, |
||
| 191 | $this->widthLeft, |
||
| 192 | $this->lengthLeft, |
||
| 193 | $this->depthLeft, |
||
| 194 | $this->remainingWeight, |
||
| 195 | $this->usedWidth, |
||
| 196 | $this->usedLength, |
||
| 197 | $this->usedDepth); |
||
| 198 | } |
||
| 199 | |||
| 312 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.