| Conditions | 11 |
| Paths | 28 |
| Total Lines | 82 |
| Code Lines | 55 |
| 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 |
||
| 45 | public function redistributeWeight(PackedBoxList $originalBoxes) |
||
| 46 | { |
||
| 47 | |||
| 48 | $targetWeight = $originalBoxes->getMeanWeight(); |
||
| 49 | $this->logger->log(LogLevel::DEBUG, "repacking for weight distribution, weight variance {$originalBoxes->getWeightVariance()}, target weight {$targetWeight}"); |
||
| 50 | |||
| 51 | $packedBoxes = new PackedBoxList; |
||
| 52 | |||
| 53 | $overWeightBoxes = []; |
||
| 54 | $underWeightBoxes = []; |
||
| 55 | foreach (clone $originalBoxes as $packedBox) { |
||
| 56 | $boxWeight = $packedBox->getWeight(); |
||
| 57 | if ($boxWeight > $targetWeight) { |
||
| 58 | $overWeightBoxes[] = $packedBox; |
||
| 59 | } elseif ($boxWeight < $targetWeight) { |
||
| 60 | $underWeightBoxes[] = $packedBox; |
||
| 61 | } else { |
||
| 62 | $packedBoxes->insert($packedBox); //target weight, so we'll keep these |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | do { //Keep moving items from most overweight box to most underweight box |
||
| 67 | $tryRepack = false; |
||
| 68 | $this->logger->log(LogLevel::DEBUG, 'boxes under/over target: ' . count($underWeightBoxes) . '/' . count($overWeightBoxes)); |
||
| 69 | |||
| 70 | foreach ($underWeightBoxes as $u => $underWeightBox) { |
||
| 71 | $this->logger->log(LogLevel::DEBUG, 'Underweight Box ' . $u); |
||
| 72 | foreach ($overWeightBoxes as $o => $overWeightBox) { |
||
| 73 | $this->logger->log(LogLevel::DEBUG, 'Overweight Box ' . $o); |
||
| 74 | $overWeightBoxItems = $overWeightBox->getItems()->asArray(); |
||
| 75 | |||
| 76 | //For each item in the heavier box, try and move it to the lighter one |
||
| 77 | foreach ($overWeightBoxItems as $oi => $overWeightBoxItem) { |
||
| 78 | $this->logger->log(LogLevel::DEBUG, 'Overweight Item ' . $oi); |
||
| 79 | if ($underWeightBox->getWeight() + $overWeightBoxItem->getWeight() > $targetWeight) { |
||
| 80 | $this->logger->log(LogLevel::DEBUG, 'Skipping item for hindering weight distribution'); |
||
| 81 | continue; //skip if moving this item would hinder rather than help weight distribution |
||
| 82 | } |
||
| 83 | |||
| 84 | $newItemsForLighterBox = clone $underWeightBox->getItems(); |
||
| 85 | $newItemsForLighterBox->insert($overWeightBoxItem); |
||
| 86 | |||
| 87 | $newLighterBoxPacker = new Packer(); //we may need a bigger box |
||
| 88 | $newLighterBoxPacker->setBoxes($this->boxes); |
||
| 89 | $newLighterBoxPacker->setItems($newItemsForLighterBox); |
||
| 90 | $this->logger->log(LogLevel::INFO, "[ATTEMPTING TO PACK LIGHTER BOX]"); |
||
| 91 | $newLighterBox = $newLighterBoxPacker->doVolumePacking()->extract(); |
||
| 92 | |||
| 93 | if ($newLighterBox->getItems()->count() === $newItemsForLighterBox->count()) { //new item fits |
||
| 94 | $this->logger->log(LogLevel::DEBUG, 'New item fits'); |
||
| 95 | unset($overWeightBoxItems[$oi]); //now packed in different box |
||
| 96 | |||
| 97 | $newHeavierBoxPacker = new Packer(); //we may be able to use a smaller box |
||
| 98 | $newHeavierBoxPacker->setBoxes($this->boxes); |
||
| 99 | $newHeavierBoxPacker->setItems($overWeightBoxItems); |
||
| 100 | |||
| 101 | $this->logger->log(LogLevel::INFO, "[ATTEMPTING TO PACK HEAVIER BOX]"); |
||
| 102 | $newHeavierBoxes = $newHeavierBoxPacker->doVolumePacking(); |
||
| 103 | if (count($newHeavierBoxes) > 1) { //found an edge case in packing algorithm that *increased* box count |
||
| 104 | $this->logger->log(LogLevel::INFO, "[REDISTRIBUTING WEIGHT] Abandoning redistribution, because new packing is less efficient than original"); |
||
| 105 | return $originalBoxes; |
||
| 106 | } |
||
| 107 | |||
| 108 | $overWeightBoxes[$o] = $newHeavierBoxes->extract(); |
||
| 109 | $underWeightBoxes[$u] = $newLighterBox; |
||
| 110 | |||
| 111 | $tryRepack = true; //we did some work, so see if we can do even better |
||
| 112 | usort($overWeightBoxes, [$packedBoxes, 'reverseCompare']); |
||
| 113 | usort($underWeightBoxes, [$packedBoxes, 'reverseCompare']); |
||
| 114 | break 3; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } while ($tryRepack); |
||
| 120 | |||
| 121 | //Combine back into a single list |
||
| 122 | $packedBoxes->insertFromArray($overWeightBoxes); |
||
| 123 | $packedBoxes->insertFromArray($underWeightBoxes); |
||
| 124 | |||
| 125 | return $packedBoxes; |
||
| 126 | } |
||
| 127 | } |
||
| 128 |