Conditions | 13 |
Paths | 17 |
Total Lines | 94 |
Code Lines | 63 |
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 |
||
220 | protected function workoutActualQuantity() |
||
221 | { |
||
222 | $actualQuantity = 0; |
||
223 | if ($buyable = $this->getBuyable()) { |
||
224 | $query = Order::get() |
||
225 | ->where(' |
||
226 | "OrderItem"."BuyableID" = '.(intval($this->BuyableID) - 0).' |
||
227 | AND |
||
228 | "OrderItem"."BuyableClassName" = \''.$this->BuyableClassName.'\' |
||
229 | AND |
||
230 | "OrderStep"."CustomerCanEdit" = 0 |
||
231 | AND |
||
232 | "Order"."ID" <> '.ShoppingCart::current_order()->ID.' |
||
233 | ') |
||
234 | ->innerJoin('OrderAttribute', '"OrderAttribute"."OrderID" = "Order"."ID"') |
||
235 | ->innerJoin('OrderItem', '"OrderAttribute"."ID" = "OrderItem"."ID"') |
||
236 | ->innerJoin('OrderStep', '"OrderStep"."ID" = "Order"."StatusID"'); |
||
237 | $amountPerOrder = array(); |
||
238 | if($query->count()) { |
||
239 | foreach ($query as $row) { |
||
240 | if(!isset($amountPerOrder[$row->OrderID])) { |
||
241 | $amountPerOrder[$row->OrderID] = 0; |
||
242 | } |
||
243 | $amountPerOrder[$row->OrderID] += $row->Quantity; |
||
244 | } |
||
245 | foreach($amountPerOrder as $orderID => $sum) { |
||
246 | if ($orderID && $sum) { |
||
247 | $buyableStockOrderEntry = BuyableStockOrderEntry::get() |
||
248 | ->filter( |
||
249 | array( |
||
250 | 'OrderID' => $orderID, |
||
251 | 'ParentID' => $this->ID |
||
252 | ) |
||
253 | ) |
||
254 | ->First(); |
||
255 | if ($buyableStockOrderEntry) { |
||
256 | //do nothing |
||
257 | } else { |
||
258 | $buyableStockOrderEntry = new BuyableStockOrderEntry(); |
||
259 | $buyableStockOrderEntry->OrderID = $orderID; |
||
260 | $buyableStockOrderEntry->ParentID = $this->ID; |
||
261 | $buyableStockOrderEntry->IncludeInCurrentCalculation = 1; |
||
262 | $buyableStockOrderEntry->Quantity = 0; |
||
263 | } |
||
264 | if ($buyableStockOrderEntry->Quantity != $sum) { |
||
265 | $buyableStockOrderEntry->Quantity = $sum; |
||
266 | $buyableStockOrderEntry->write(); |
||
267 | } |
||
268 | } |
||
269 | } |
||
270 | } |
||
271 | //find last adjustment |
||
272 | $latestManualUpdate = BuyableStockManualUpdate::get() |
||
273 | ->filter(array('ParentID' => $this->ID)) |
||
274 | ->sort(array('LastEdited' => 'DESC')) |
||
275 | ->First(); |
||
276 | //nullify order quantities that were entered before last adjustment |
||
277 | if ($latestManualUpdate) { |
||
278 | $latestManualUpdateQuantity = $latestManualUpdate->Quantity; |
||
279 | DB::query(" |
||
280 | UPDATE \"BuyableStockOrderEntry\" |
||
281 | SET \"IncludeInCurrentCalculation\" = 0 |
||
282 | WHERE |
||
283 | \"LastEdited\" < '".$latestManualUpdate->LastEdited."' |
||
284 | AND |
||
285 | \"ParentID\" = ".$this->ID |
||
286 | ); |
||
287 | } else { |
||
288 | $latestManualUpdateQuantity = 0; |
||
289 | } |
||
290 | //work out additional purchases |
||
291 | $orderQuantityToDeduct = BuyableStockOrderEntry::get() |
||
292 | ->filter( |
||
293 | array( |
||
294 | 'ParentID' => $this->ID, |
||
295 | 'IncludeInCurrentCalculation' => 1 |
||
296 | ) |
||
297 | )->sum('Quantity'); |
||
298 | if (!$orderQuantityToDeduct) { |
||
299 | $orderQuantityToDeduct = 0; |
||
300 | } |
||
301 | //work out base total |
||
302 | $actualQuantity = $latestManualUpdateQuantity - $orderQuantityToDeduct; |
||
303 | if (isset($_GET["debug"])) { |
||
304 | echo "<hr />"; |
||
305 | echo $this->Name; |
||
306 | echo " | Manual SUM: ".$latestManualUpdateQuantity; |
||
307 | echo " | Order SUM: ".$orderQuantityToDeduct; |
||
308 | echo " | Total SUM: ".$this->BaseQuantity; |
||
309 | echo "<hr />"; |
||
310 | } |
||
311 | } |
||
312 | return $actualQuantity; |
||
313 | } |
||
314 | } |
||
315 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.