Conditions | 7 |
Paths | 24 |
Total Lines | 67 |
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 declare(strict_types=1); |
||
99 | private function adjustRestOfCart(array $foundItems, LineItemFlatCollection $restOfCart): LineItemFlatCollection |
||
100 | { |
||
101 | // a holder for all foundItems indexed by lineItemId |
||
102 | /** @var LineItemQuantity[] $lineItemsToRemove */ |
||
103 | $lineItemsToRemove = []; |
||
104 | |||
105 | // we prepare the removeLineItemIds array with all LineItemQuantity objects indexed by lineItemId |
||
106 | foreach ($foundItems as $itemToRemove) { |
||
107 | if (isset($lineItemsToRemove[$itemToRemove->getLineItemId()])) { |
||
108 | $quantity = $lineItemsToRemove[$itemToRemove->getLineItemId()]; |
||
109 | $lineItemsToRemove[$itemToRemove->getLineItemId()]->setQuantity($quantity->getQuantity() + $itemToRemove->getQuantity()); |
||
110 | |||
111 | continue; |
||
112 | } |
||
113 | $lineItemsToRemove[$itemToRemove->getLineItemId()] = $itemToRemove; |
||
114 | } |
||
115 | |||
116 | /** @var array $lineItemsToRemoveIDs */ |
||
117 | $lineItemsToRemoveIDs = array_keys($lineItemsToRemove); |
||
118 | |||
119 | $newRestOfCart = new LineItemFlatCollection(); |
||
120 | |||
121 | // this is our running buffer |
||
122 | // for the items that need to be removed |
||
123 | $deleteBuffer = []; |
||
124 | |||
125 | // make sure we have an ID index for |
||
126 | // all our delete-items with a qty of 0 |
||
127 | foreach (array_keys($lineItemsToRemove) as $id) { |
||
128 | $deleteBuffer[$id] = 0; |
||
129 | } |
||
130 | |||
131 | foreach ($restOfCart as $item) { |
||
132 | // if its a totally different item |
||
133 | // just add it to the rest of our cart |
||
134 | if (!in_array($item->getId(), $lineItemsToRemoveIDs, true)) { |
||
135 | $newRestOfCart->add($item); |
||
136 | } else { |
||
137 | // we have an item that should be removed |
||
138 | // now we have to calculate how many of the item position (qty diff) |
||
139 | // or if we have even reached our max amount of quantities to remove for this item |
||
140 | $maxRemoveMeta = $lineItemsToRemove[$item->getId()]->getQuantity(); |
||
141 | |||
142 | $alreadyDeletedCount = $deleteBuffer[$item->getId()]; |
||
143 | |||
144 | // now check if we can remove our current item completely |
||
145 | // or if we have a sub quantity that still needs to be |
||
146 | // added to the rest of the cart |
||
147 | if ($alreadyDeletedCount + $item->getQuantity() <= $maxRemoveMeta) { |
||
148 | // remove completely |
||
149 | $deleteBuffer[$item->getId()] += $item->getQuantity(); |
||
150 | } else { |
||
151 | $toDeleteCount = $maxRemoveMeta - $alreadyDeletedCount; |
||
152 | $keepCount = $item->getQuantity() - $toDeleteCount; |
||
153 | |||
154 | // mark our diff as "deleted" |
||
155 | $deleteBuffer[$item->getId()] += $toDeleteCount; |
||
156 | |||
157 | // add the keep count to our item |
||
158 | // and the item to the rest of our cart |
||
159 | $item->setQuantity($keepCount); |
||
160 | $newRestOfCart->add($item); |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | |||
165 | return $newRestOfCart; |
||
166 | } |
||
192 |