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