| Conditions | 48 |
| Paths | > 20000 |
| Total Lines | 195 |
| Code Lines | 125 |
| 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 |
||
| 190 | public function save() |
||
| 191 | { |
||
| 192 | $this->load->language('checkout/checkout'); |
||
| 193 | |||
| 194 | $json = array(); |
||
| 195 | |||
| 196 | // Validate if customer is logged in. |
||
| 197 | if ($this->customer->isLogged()) { |
||
| 198 | $json['redirect'] = $this->url->link('checkout/checkout', '', true); |
||
| 199 | } |
||
| 200 | |||
| 201 | // Validate cart has products and has stock. |
||
| 202 | if ((!$this->cart->hasProducts()) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) { |
||
| 203 | $json['redirect'] = $this->url->link('checkout/cart'); |
||
| 204 | } |
||
| 205 | |||
| 206 | // Check if guest checkout is available. |
||
| 207 | if (!$this->config->get('config_checkout_guest') || $this->config->get('config_customer_price') || $this->cart->hasDownload()) { |
||
| 208 | $json['redirect'] = $this->url->link('checkout/checkout', '', true); |
||
| 209 | } |
||
| 210 | |||
| 211 | if (!$json) { |
||
| 212 | if ((\voku\helper\UTF8::strlen(trim($this->request->post['firstname'])) < 1) || (\voku\helper\UTF8::strlen(trim($this->request->post['firstname'])) > 32)) { |
||
| 213 | $json['error']['firstname'] = $this->language->get('error_firstname'); |
||
| 214 | } |
||
| 215 | |||
| 216 | if ((\voku\helper\UTF8::strlen(trim($this->request->post['lastname'])) < 1) || (\voku\helper\UTF8::strlen(trim($this->request->post['lastname'])) > 32)) { |
||
| 217 | $json['error']['lastname'] = $this->language->get('error_lastname'); |
||
| 218 | } |
||
| 219 | |||
| 220 | if ((\voku\helper\UTF8::strlen($this->request->post['email']) > 96) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) { |
||
| 221 | $json['error']['email'] = $this->language->get('error_email'); |
||
| 222 | } |
||
| 223 | |||
| 224 | if ((\voku\helper\UTF8::strlen($this->request->post['telephone']) < 3) || (\voku\helper\UTF8::strlen($this->request->post['telephone']) > 32)) { |
||
| 225 | $json['error']['telephone'] = $this->language->get('error_telephone'); |
||
| 226 | } |
||
| 227 | |||
| 228 | if ((\voku\helper\UTF8::strlen(trim($this->request->post['address_1'])) < 3) || (\voku\helper\UTF8::strlen(trim($this->request->post['address_1'])) > 128)) { |
||
| 229 | $json['error']['address_1'] = $this->language->get('error_address_1'); |
||
| 230 | } |
||
| 231 | |||
| 232 | if ((\voku\helper\UTF8::strlen(trim($this->request->post['city'])) < 2) || (\voku\helper\UTF8::strlen(trim($this->request->post['city'])) > 128)) { |
||
| 233 | $json['error']['city'] = $this->language->get('error_city'); |
||
| 234 | } |
||
| 235 | |||
| 236 | $this->load->model('localisation/country'); |
||
| 237 | |||
| 238 | $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']); |
||
| 239 | |||
| 240 | 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)) { |
||
| 241 | $json['error']['postcode'] = $this->language->get('error_postcode'); |
||
| 242 | } |
||
| 243 | |||
| 244 | if ($this->request->post['country_id'] == '') { |
||
| 245 | $json['error']['country'] = $this->language->get('error_country'); |
||
| 246 | } |
||
| 247 | |||
| 248 | if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '' || !is_numeric($this->request->post['zone_id'])) { |
||
| 249 | $json['error']['zone'] = $this->language->get('error_zone'); |
||
| 250 | } |
||
| 251 | |||
| 252 | // Customer Group |
||
| 253 | if (isset($this->request->post['customer_group_id']) && is_array($this->config->get('config_customer_group_display')) && in_array($this->request->post['customer_group_id'], $this->config->get('config_customer_group_display'))) { |
||
| 254 | $customer_group_id = $this->request->post['customer_group_id']; |
||
| 255 | } else { |
||
| 256 | $customer_group_id = $this->config->get('config_customer_group_id'); |
||
| 257 | } |
||
| 258 | |||
| 259 | // Custom field validation |
||
| 260 | $this->load->model('account/custom_field'); |
||
| 261 | |||
| 262 | $custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id); |
||
| 263 | |||
| 264 | foreach ($custom_fields as $custom_field) { |
||
| 265 | if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']])) { |
||
| 266 | $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); |
||
| 267 | } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) { |
||
| 268 | $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | if (!$json) { |
||
| 274 | $this->session->data['account'] = 'guest'; |
||
| 275 | |||
| 276 | $this->session->data['guest']['customer_group_id'] = $customer_group_id; |
||
| 277 | $this->session->data['guest']['firstname'] = $this->request->post['firstname']; |
||
| 278 | $this->session->data['guest']['lastname'] = $this->request->post['lastname']; |
||
| 279 | $this->session->data['guest']['email'] = $this->request->post['email']; |
||
| 280 | $this->session->data['guest']['telephone'] = $this->request->post['telephone']; |
||
| 281 | $this->session->data['guest']['fax'] = $this->request->post['fax']; |
||
| 282 | |||
| 283 | if (isset($this->request->post['custom_field']['account'])) { |
||
| 284 | $this->session->data['guest']['custom_field'] = $this->request->post['custom_field']['account']; |
||
| 285 | } else { |
||
| 286 | $this->session->data['guest']['custom_field'] = array(); |
||
| 287 | } |
||
| 288 | |||
| 289 | $this->session->data['payment_address']['firstname'] = $this->request->post['firstname']; |
||
| 290 | $this->session->data['payment_address']['lastname'] = $this->request->post['lastname']; |
||
| 291 | $this->session->data['payment_address']['company'] = $this->request->post['company']; |
||
| 292 | $this->session->data['payment_address']['address_1'] = $this->request->post['address_1']; |
||
| 293 | $this->session->data['payment_address']['address_2'] = $this->request->post['address_2']; |
||
| 294 | $this->session->data['payment_address']['postcode'] = $this->request->post['postcode']; |
||
| 295 | $this->session->data['payment_address']['city'] = $this->request->post['city']; |
||
| 296 | $this->session->data['payment_address']['country_id'] = $this->request->post['country_id']; |
||
| 297 | $this->session->data['payment_address']['zone_id'] = $this->request->post['zone_id']; |
||
| 298 | |||
| 299 | $this->load->model('localisation/country'); |
||
| 300 | |||
| 301 | $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']); |
||
| 302 | |||
| 303 | if ($country_info) { |
||
| 304 | $this->session->data['payment_address']['country'] = $country_info['name']; |
||
| 305 | $this->session->data['payment_address']['iso_code_2'] = $country_info['iso_code_2']; |
||
| 306 | $this->session->data['payment_address']['iso_code_3'] = $country_info['iso_code_3']; |
||
| 307 | $this->session->data['payment_address']['address_format'] = $country_info['address_format']; |
||
| 308 | } else { |
||
| 309 | $this->session->data['payment_address']['country'] = ''; |
||
| 310 | $this->session->data['payment_address']['iso_code_2'] = ''; |
||
| 311 | $this->session->data['payment_address']['iso_code_3'] = ''; |
||
| 312 | $this->session->data['payment_address']['address_format'] = ''; |
||
| 313 | } |
||
| 314 | |||
| 315 | if (isset($this->request->post['custom_field']['address'])) { |
||
| 316 | $this->session->data['payment_address']['custom_field'] = $this->request->post['custom_field']['address']; |
||
| 317 | } else { |
||
| 318 | $this->session->data['payment_address']['custom_field'] = array(); |
||
| 319 | } |
||
| 320 | |||
| 321 | $this->load->model('localisation/zone'); |
||
| 322 | |||
| 323 | $zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']); |
||
| 324 | |||
| 325 | if ($zone_info) { |
||
| 326 | $this->session->data['payment_address']['zone'] = $zone_info['name']; |
||
| 327 | $this->session->data['payment_address']['zone_code'] = $zone_info['code']; |
||
| 328 | } else { |
||
| 329 | $this->session->data['payment_address']['zone'] = ''; |
||
| 330 | $this->session->data['payment_address']['zone_code'] = ''; |
||
| 331 | } |
||
| 332 | |||
| 333 | if (!empty($this->request->post['shipping_address'])) { |
||
| 334 | $this->session->data['guest']['shipping_address'] = $this->request->post['shipping_address']; |
||
| 335 | } else { |
||
| 336 | $this->session->data['guest']['shipping_address'] = false; |
||
| 337 | } |
||
| 338 | |||
| 339 | if ($this->session->data['guest']['shipping_address']) { |
||
| 340 | $this->session->data['shipping_address']['firstname'] = $this->request->post['firstname']; |
||
| 341 | $this->session->data['shipping_address']['lastname'] = $this->request->post['lastname']; |
||
| 342 | $this->session->data['shipping_address']['company'] = $this->request->post['company']; |
||
| 343 | $this->session->data['shipping_address']['address_1'] = $this->request->post['address_1']; |
||
| 344 | $this->session->data['shipping_address']['address_2'] = $this->request->post['address_2']; |
||
| 345 | $this->session->data['shipping_address']['postcode'] = $this->request->post['postcode']; |
||
| 346 | $this->session->data['shipping_address']['city'] = $this->request->post['city']; |
||
| 347 | $this->session->data['shipping_address']['country_id'] = $this->request->post['country_id']; |
||
| 348 | $this->session->data['shipping_address']['zone_id'] = $this->request->post['zone_id']; |
||
| 349 | |||
| 350 | if ($country_info) { |
||
| 351 | $this->session->data['shipping_address']['country'] = $country_info['name']; |
||
| 352 | $this->session->data['shipping_address']['iso_code_2'] = $country_info['iso_code_2']; |
||
| 353 | $this->session->data['shipping_address']['iso_code_3'] = $country_info['iso_code_3']; |
||
| 354 | $this->session->data['shipping_address']['address_format'] = $country_info['address_format']; |
||
| 355 | } else { |
||
| 356 | $this->session->data['shipping_address']['country'] = ''; |
||
| 357 | $this->session->data['shipping_address']['iso_code_2'] = ''; |
||
| 358 | $this->session->data['shipping_address']['iso_code_3'] = ''; |
||
| 359 | $this->session->data['shipping_address']['address_format'] = ''; |
||
| 360 | } |
||
| 361 | |||
| 362 | if ($zone_info) { |
||
| 363 | $this->session->data['shipping_address']['zone'] = $zone_info['name']; |
||
| 364 | $this->session->data['shipping_address']['zone_code'] = $zone_info['code']; |
||
| 365 | } else { |
||
| 366 | $this->session->data['shipping_address']['zone'] = ''; |
||
| 367 | $this->session->data['shipping_address']['zone_code'] = ''; |
||
| 368 | } |
||
| 369 | |||
| 370 | if (isset($this->request->post['custom_field']['address'])) { |
||
| 371 | $this->session->data['shipping_address']['custom_field'] = $this->request->post['custom_field']['address']; |
||
| 372 | } else { |
||
| 373 | $this->session->data['shipping_address']['custom_field'] = array(); |
||
| 374 | } |
||
| 375 | } |
||
| 376 | |||
| 377 | unset($this->session->data['shipping_method']); |
||
| 378 | unset($this->session->data['shipping_methods']); |
||
| 379 | unset($this->session->data['payment_method']); |
||
| 380 | unset($this->session->data['payment_methods']); |
||
| 381 | } |
||
| 382 | |||
| 383 | $this->response->addHeader('Content-Type: application/json'); |
||
| 384 | $this->response->setOutput(json_encode($json)); |
||
| 385 | } |
||
| 387 |
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.