Conditions | 37 |
Paths | 9288 |
Total Lines | 119 |
Code Lines | 70 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
118 | public function save() |
||
119 | { |
||
120 | $this->load->language('checkout/checkout'); |
||
121 | |||
122 | $json = array(); |
||
123 | |||
124 | // Validate if customer is logged in. |
||
125 | if ($this->customer->isLogged()) { |
||
126 | $json['redirect'] = $this->url->link('checkout/checkout', '', true); |
||
127 | } |
||
128 | |||
129 | // Validate cart has products and has stock. |
||
130 | if ((!$this->cart->hasProducts()) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) { |
||
131 | $json['redirect'] = $this->url->link('checkout/cart'); |
||
132 | } |
||
133 | |||
134 | // Check if guest checkout is available. |
||
135 | if (!$this->config->get('config_checkout_guest') || $this->config->get('config_customer_price') || $this->cart->hasDownload()) { |
||
136 | $json['redirect'] = $this->url->link('checkout/checkout', '', true); |
||
137 | } |
||
138 | |||
139 | if (!$json) { |
||
140 | if ((\voku\helper\UTF8::strlen(trim($this->request->post['firstname'])) < 1) || (\voku\helper\UTF8::strlen(trim($this->request->post['firstname'])) > 32)) { |
||
141 | $json['error']['firstname'] = $this->language->get('error_firstname'); |
||
142 | } |
||
143 | |||
144 | if ((\voku\helper\UTF8::strlen(trim($this->request->post['lastname'])) < 1) || (\voku\helper\UTF8::strlen(trim($this->request->post['lastname'])) > 32)) { |
||
145 | $json['error']['lastname'] = $this->language->get('error_lastname'); |
||
146 | } |
||
147 | |||
148 | if ((\voku\helper\UTF8::strlen(trim($this->request->post['address_1'])) < 3) || (\voku\helper\UTF8::strlen(trim($this->request->post['address_1'])) > 128)) { |
||
149 | $json['error']['address_1'] = $this->language->get('error_address_1'); |
||
150 | } |
||
151 | |||
152 | if ((\voku\helper\UTF8::strlen(trim($this->request->post['city'])) < 2) || (\voku\helper\UTF8::strlen(trim($this->request->post['city'])) > 128)) { |
||
153 | $json['error']['city'] = $this->language->get('error_city'); |
||
154 | } |
||
155 | |||
156 | $this->load->model('localisation/country'); |
||
157 | |||
158 | $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']); |
||
159 | |||
160 | if ($country_info && $country_info['postcode_required'] && (\voku\helper\UTF8::strlen(trim($this->request->post['postcode'])) < 2 || \voku\helper\UTF8::strlen(trim($this->request->post['postcode'])) > 10)) { |
||
161 | $json['error']['postcode'] = $this->language->get('error_postcode'); |
||
162 | } |
||
163 | |||
164 | if ($this->request->post['country_id'] == '') { |
||
165 | $json['error']['country'] = $this->language->get('error_country'); |
||
166 | } |
||
167 | |||
168 | if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '' || !is_numeric($this->request->post['zone_id'])) { |
||
169 | $json['error']['zone'] = $this->language->get('error_zone'); |
||
170 | } |
||
171 | |||
172 | // Custom field validation |
||
173 | $this->load->model('account/custom_field'); |
||
174 | |||
175 | $custom_fields = $this->model_account_custom_field->getCustomFields($this->session->data['guest']['customer_group_id']); |
||
176 | |||
177 | foreach ($custom_fields as $custom_field) { |
||
178 | if (($custom_field['location'] == 'address') && $custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) { |
||
179 | $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); |
||
180 | } elseif (($custom_field['location'] == 'address') && ($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) { |
||
181 | $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | |||
186 | if (!$json) { |
||
187 | $this->session->data['shipping_address']['firstname'] = $this->request->post['firstname']; |
||
188 | $this->session->data['shipping_address']['lastname'] = $this->request->post['lastname']; |
||
189 | $this->session->data['shipping_address']['company'] = $this->request->post['company']; |
||
190 | $this->session->data['shipping_address']['address_1'] = $this->request->post['address_1']; |
||
191 | $this->session->data['shipping_address']['address_2'] = $this->request->post['address_2']; |
||
192 | $this->session->data['shipping_address']['postcode'] = $this->request->post['postcode']; |
||
193 | $this->session->data['shipping_address']['city'] = $this->request->post['city']; |
||
194 | $this->session->data['shipping_address']['country_id'] = $this->request->post['country_id']; |
||
195 | $this->session->data['shipping_address']['zone_id'] = $this->request->post['zone_id']; |
||
196 | |||
197 | $this->load->model('localisation/country'); |
||
198 | |||
199 | $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']); |
||
200 | |||
201 | if ($country_info) { |
||
202 | $this->session->data['shipping_address']['country'] = $country_info['name']; |
||
203 | $this->session->data['shipping_address']['iso_code_2'] = $country_info['iso_code_2']; |
||
204 | $this->session->data['shipping_address']['iso_code_3'] = $country_info['iso_code_3']; |
||
205 | $this->session->data['shipping_address']['address_format'] = $country_info['address_format']; |
||
206 | } else { |
||
207 | $this->session->data['shipping_address']['country'] = ''; |
||
208 | $this->session->data['shipping_address']['iso_code_2'] = ''; |
||
209 | $this->session->data['shipping_address']['iso_code_3'] = ''; |
||
210 | $this->session->data['shipping_address']['address_format'] = ''; |
||
211 | } |
||
212 | |||
213 | $this->load->model('localisation/zone'); |
||
214 | |||
215 | $zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']); |
||
216 | |||
217 | if ($zone_info) { |
||
218 | $this->session->data['shipping_address']['zone'] = $zone_info['name']; |
||
219 | $this->session->data['shipping_address']['zone_code'] = $zone_info['code']; |
||
220 | } else { |
||
221 | $this->session->data['shipping_address']['zone'] = ''; |
||
222 | $this->session->data['shipping_address']['zone_code'] = ''; |
||
223 | } |
||
224 | |||
225 | if (isset($this->request->post['custom_field'])) { |
||
226 | $this->session->data['shipping_address']['custom_field'] = $this->request->post['custom_field']; |
||
227 | } else { |
||
228 | $this->session->data['shipping_address']['custom_field'] = array(); |
||
229 | } |
||
230 | |||
231 | unset($this->session->data['shipping_method']); |
||
232 | unset($this->session->data['shipping_methods']); |
||
233 | } |
||
234 | |||
235 | $this->response->addHeader('Content-Type: application/json'); |
||
236 | $this->response->setOutput(json_encode($json)); |
||
237 | } |
||
239 |
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.