| Conditions | 24 |
| Paths | 128 |
| Total Lines | 80 |
| Code Lines | 48 |
| 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 |
||
| 42 | public function updatecart($data, $form) |
||
| 43 | { |
||
| 44 | $items = $this->cart->Items(); |
||
| 45 | $updatecount = $removecount = 0; |
||
| 46 | |||
| 47 | $request = $this->getRequest(); |
||
| 48 | $order = ShoppingCart::curr(); |
||
| 49 | if ($request && $request->isAjax() && $order) { |
||
| 50 | ShopTools::install_locale($order->Locale); |
||
| 51 | } |
||
| 52 | |||
| 53 | $numericConverter = NumericField::create('_temp'); |
||
| 54 | |||
| 55 | $messages = []; |
||
| 56 | $badMessages = []; |
||
| 57 | if (isset($data['Items']) && is_array($data['Items'])) { |
||
| 58 | foreach ($data['Items'] as $itemid => $fields) { |
||
| 59 | $item = $items->byID($itemid); |
||
| 60 | if (!$item) { |
||
| 61 | continue; |
||
| 62 | } |
||
| 63 | //delete lines |
||
| 64 | if (isset($fields['Remove']) || (isset($fields['Quantity']) && (int)$fields['Quantity'] <= 0)) { |
||
| 65 | if (ShoppingCart::singleton()->removeOrderItem($item)) { |
||
| 66 | $removecount++; |
||
| 67 | } else { |
||
| 68 | $badMessages[] = ShoppingCart::singleton()->getMessage(); |
||
| 69 | } |
||
| 70 | |||
| 71 | continue; |
||
| 72 | } |
||
| 73 | //update quantities |
||
| 74 | if (isset($fields['Quantity']) && $quantity = Convert::raw2sql($fields['Quantity'])) { |
||
| 75 | $numericConverter->setValue($quantity); |
||
| 76 | if (!ShoppingCart::singleton()->updateOrderItemQuantity($item, $numericConverter->dataValue())) { |
||
| 77 | $badMessages[] = ShoppingCart::singleton()->getMessage(); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | //update variations |
||
| 81 | if (isset($fields['ProductVariationID']) && $id = Convert::raw2sql($fields['ProductVariationID'])) { |
||
| 82 | if ($item->ProductVariationID != $id) { |
||
| 83 | $item->ProductVariationID = $id; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | //TODO: make updates through ShoppingCart class |
||
| 87 | //TODO: combine with items that now match exactly |
||
| 88 | //TODO: validate changes |
||
| 89 | if ($item->isChanged()) { |
||
| 90 | $item->write(); |
||
| 91 | $updatecount++; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | if ($removecount) { |
||
| 96 | $messages['remove'] = _t( |
||
| 97 | __CLASS__ . '.REMOVED_ITEMS', |
||
| 98 | 'Removed {count} items.', |
||
| 99 | 'count is the amount that was removed', |
||
| 100 | ['count' => $removecount] |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | if ($updatecount) { |
||
| 104 | $messages['updatecount'] = _t( |
||
| 105 | __CLASS__ . '.UPDATED_ITEMS', |
||
| 106 | 'Updated {count} items.', |
||
| 107 | 'count is the amount that was updated', |
||
| 108 | ['count' => $updatecount] |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | if (count($messages)) { |
||
| 112 | $form->sessionMessage(implode(' ', $messages), 'good'); |
||
| 113 | } |
||
| 114 | |||
| 115 | if (count($badMessages)) { |
||
| 116 | $form->sessionMessage(implode(' ', $badMessages), 'bad'); |
||
| 117 | } |
||
| 118 | |||
| 119 | $this->extend('updateCartFormResponse', $request, $response, $form, $removecount, $updatecount); |
||
| 120 | |||
| 121 | return $response ? $response : $this->controller->redirectBack(); |
||
| 122 | } |
||
| 124 |