| Conditions | 15 |
| Paths | 336 |
| Total Lines | 58 |
| Code Lines | 28 |
| 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 |
||
| 98 | public function save() |
||
| 99 | { |
||
| 100 | $this->load->language('checkout/checkout'); |
||
| 101 | |||
| 102 | $json = array(); |
||
| 103 | |||
| 104 | // Validate if shipping is required. If not the customer should not have reached this page. |
||
| 105 | if (!$this->cart->hasShipping()) { |
||
| 106 | $json['redirect'] = $this->url->link('checkout/checkout', '', true); |
||
| 107 | } |
||
| 108 | |||
| 109 | // Validate if shipping address has been set. |
||
| 110 | if (!isset($this->session->data['shipping_address'])) { |
||
| 111 | $json['redirect'] = $this->url->link('checkout/checkout', '', true); |
||
| 112 | } |
||
| 113 | |||
| 114 | // Validate cart has products and has stock. |
||
| 115 | if ((!$this->cart->hasProducts()) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) { |
||
| 116 | $json['redirect'] = $this->url->link('checkout/cart'); |
||
| 117 | } |
||
| 118 | |||
| 119 | // Validate minimum quantity requirements. |
||
| 120 | $products = $this->cart->getProducts(); |
||
| 121 | |||
| 122 | foreach ($products as $product) { |
||
| 123 | $product_total = 0; |
||
| 124 | |||
| 125 | foreach ($products as $product_2) { |
||
| 126 | if ($product_2['product_id'] == $product['product_id']) { |
||
| 127 | $product_total += $product_2['quantity']; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($product['minimum'] > $product_total) { |
||
| 132 | $json['redirect'] = $this->url->link('checkout/cart'); |
||
| 133 | |||
| 134 | break; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | if (!isset($this->request->post['shipping_method'])) { |
||
| 139 | $json['error']['warning'] = $this->language->get('error_shipping'); |
||
| 140 | } else { |
||
| 141 | $shipping = explode('.', $this->request->post['shipping_method']); |
||
| 142 | |||
| 143 | if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) { |
||
| 144 | $json['error']['warning'] = $this->language->get('error_shipping'); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | if (!$json) { |
||
| 149 | $this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]]; |
||
| 150 | |||
| 151 | $this->session->data['comment'] = strip_tags($this->request->post['comment']); |
||
| 152 | } |
||
| 153 | |||
| 154 | $this->response->addHeader('Content-Type: application/json'); |
||
| 155 | $this->response->setOutput(json_encode($json)); |
||
| 156 | } |
||
| 158 |