Conditions | 37 |
Paths | 8067 |
Total Lines | 176 |
Lines | 39 |
Ratio | 22.16 % |
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 |
||
338 | public function addLocationsFromCsvFile($csvFileHandle, $locationsFieldsMapping) |
||
339 | { |
||
340 | $max_line_length = 512; |
||
341 | $delemietr = ','; |
||
342 | |||
343 | $results = array(); |
||
344 | $results['fail'] = array(); |
||
345 | $results['success'] = array(); |
||
346 | |||
347 | $columns = fgetcsv($csvFileHandle, $max_line_length, $delemietr); |
||
348 | |||
349 | $addressBookFields = array("cached_lat","cached_lng","curbside_lat","curbside_lng","address_alias","address_1","address_2","address_city", |
||
350 | "address_state_id","address_zip","address_phone_number","schedule","address_group","first_name","last_name","local_time_window_start", |
||
351 | "local_time_window_end","local_time_window_start_2","local_time_window_end_2","address_email","address_country_id","address_custom_data", |
||
352 | "schedule_blacklist","service_time","local_timezone_string","color","address_icon","address_stop_type","address_cube","address_pieces", |
||
353 | "address_reference_no","address_revenue","address_weight","address_priority","address_customer_po"); |
||
354 | |||
355 | if (empty($columns)) { |
||
356 | array_push($results['fail'], 'Empty CSV table'); |
||
357 | return ($results); |
||
358 | } |
||
359 | |||
360 | $iRow = 1; |
||
361 | |||
362 | while (($rows = fgetcsv($csvFileHandle, $max_line_length, $delemietr)) !== false) { |
||
363 | if ($rows[$locationsFieldsMapping['cached_lat']] |
||
364 | && $rows[$locationsFieldsMapping['cached_lng']] |
||
365 | && $rows[$locationsFieldsMapping['address_1']] |
||
366 | && array(null)!==$rows) { |
||
367 | $curSchedule = ""; |
||
368 | $mode = ""; |
||
369 | |||
370 | View Code Duplication | if (isset($rows[$locationsFieldsMapping['schedule_mode']])) { |
|
371 | if ($this->validateScheduleMode($rows[$locationsFieldsMapping['schedule_mode']])) { |
||
372 | $curSchedule = '"mode":"'.$rows[$locationsFieldsMapping['schedule_mode']].'",'; |
||
373 | $mode = $rows[$locationsFieldsMapping['schedule_mode']]; |
||
374 | } else { |
||
375 | array_push($results['fail'], "$iRow --> Wrong schedule mode parameter"); |
||
376 | $curSchedule = ""; |
||
377 | } |
||
378 | } else { |
||
379 | array_push($results['fail'], "$iRow --> The schedule mode parameter is not set"); |
||
380 | $curSchedule = ""; |
||
381 | } |
||
382 | |||
383 | if (isset($rows[$locationsFieldsMapping['schedule_enabled']])) { |
||
384 | if ($this->validateScheduleEnable($rows[$locationsFieldsMapping['schedule_enabled']])) { |
||
385 | $curSchedule .= '"enabled":'.$rows[$locationsFieldsMapping['schedule_enabled']].','; |
||
386 | } else { |
||
387 | array_push($results['fail'], "$iRow --> The schedule enabled parameter is not set "); |
||
388 | $curSchedule = ""; |
||
389 | } |
||
390 | } |
||
391 | |||
392 | if (isset($rows[$locationsFieldsMapping['schedule_every']])) { |
||
393 | if ($this->validateScheduleEvery($rows[$locationsFieldsMapping['schedule_every']])) { |
||
394 | $curSchedule.='"'.$mode.'":{'.'"every":'.$rows[$locationsFieldsMapping['schedule_every']].','; |
||
395 | if ($mode == 'daily') { |
||
396 | $curSchedule = trim($curSchedule,','); |
||
397 | $curSchedule.='}'; |
||
398 | } |
||
399 | } else { |
||
400 | array_push($results['fail'], "$iRow --> The parameter sched_every is not set"); |
||
401 | $curSchedule = ""; |
||
402 | } |
||
403 | } |
||
404 | |||
405 | if ($mode!='daily') { |
||
406 | switch ($mode) { |
||
407 | case 'weekly': |
||
408 | View Code Duplication | if (isset($rows[$locationsFieldsMapping['schedule_weekdays']])) { |
|
409 | if ($this->validateScheduleWeekDays($rows[$locationsFieldsMapping['schedule_weekdays']])) { |
||
410 | $curSchedule .= '"weekdays":['.$rows[$locationsFieldsMapping['schedule_weekdays']].']}'; |
||
411 | } else { |
||
412 | array_push($results['fail'], "$iRow --> Wrong weekdays"); |
||
413 | $curSchedule = ""; |
||
414 | } |
||
415 | } else { |
||
416 | array_push($results['fail'], "$iRow --> The parameters sched_weekdays is not set"); |
||
417 | $curSchedule = ""; |
||
418 | } |
||
419 | break; |
||
420 | case 'monthly': |
||
421 | $monthlyMode = ""; |
||
422 | View Code Duplication | if (isset($rows[$locationsFieldsMapping['monthly_mode']])) { |
|
423 | if ($this->validateScheduleMonthlyMode($rows[$locationsFieldsMapping['monthly_mode']])) { |
||
424 | $monthlyMode = $rows[$locationsFieldsMapping['monthly_mode']]; |
||
425 | $curSchedule .= '"mode": "'.$rows[$locationsFieldsMapping['monthly_mode']].'",'; |
||
426 | } else { |
||
427 | array_push($results['fail'], "$iRow --> Wrong monthly mode"); |
||
428 | $curSchedule = ""; |
||
429 | } |
||
430 | } else { |
||
431 | array_push($results['fail'], "$iRow --> The parameter sched_monthly_mode is not set"); |
||
432 | $curSchedule = ""; |
||
433 | } |
||
434 | |||
435 | if ($monthlyMode != "") { |
||
436 | switch ($monthlyMode) { |
||
437 | case 'dates': |
||
438 | if (isset($rows[$locationsFieldsMapping['monthly_dates']])) { |
||
439 | if ($this->validateScheduleMonthlyDates($rows[$locationsFieldsMapping['monthly_dates']])) { |
||
440 | $curSchedule .= '"dates":['.$rows[$locationsFieldsMapping['monthly_dates']].']}'; |
||
441 | } else { |
||
442 | array_push($results['fail'], "$iRow --> Wrong monthly dates"); |
||
443 | $curSchedule = ""; |
||
444 | } |
||
445 | } |
||
446 | break; |
||
447 | case 'nth': |
||
448 | View Code Duplication | if (isset($rows[$locationsFieldsMapping['monthly_nth_n']])) { |
|
449 | if ($this->validateScheduleNthN($rows[$locationsFieldsMapping['monthly_nth_n']])) { |
||
450 | $curSchedule .= '"nth":{"n":'.$rows[$locationsFieldsMapping['monthly_nth_n']].','; |
||
451 | } else { |
||
452 | array_push($results['fail'], "$iRow --> Wrong parameter sched_nth_n"); |
||
453 | $curSchedule = ""; |
||
454 | } |
||
455 | } else { |
||
456 | array_push($results['fail'], "$iRow --> The parameter sched_nth_n is not set"); |
||
457 | $curSchedule = ""; |
||
458 | } |
||
459 | |||
460 | View Code Duplication | if ($curSchedule != "") { |
|
461 | if (isset($rows[$locationsFieldsMapping['monthly_nth_wwhat']])) { |
||
462 | if ($this->validateScheduleNthWhat($rows[$locationsFieldsMapping['monthly_nth_wwhat']])) { |
||
463 | $curSchedule .= '"what":'.$rows[$locationsFieldsMapping['monthly_nth_wwhat']].'}}'; |
||
464 | } else { |
||
465 | array_push($results['fail'], "$iRow --> Wrong parameter sched_nth_what"); |
||
466 | $curSchedule = ""; |
||
467 | } |
||
468 | } else { |
||
469 | array_push($results['fail'], "$iRow --> The parameter sched_nth_what is not set"); |
||
470 | $curSchedule = ""; |
||
471 | } |
||
472 | } |
||
473 | break; |
||
474 | } |
||
475 | } |
||
476 | break; |
||
477 | default: |
||
478 | $curSchedule = ""; |
||
479 | break; |
||
480 | } |
||
481 | } |
||
482 | |||
483 | if (($mode == 'daily' || $mode == 'weekly' || $mode == 'monthy') && $curSchedule == "") { |
||
484 | $iRow++; |
||
485 | continue; |
||
486 | } |
||
487 | |||
488 | $curSchedule = strtolower($curSchedule); |
||
489 | |||
490 | $curSchedule = '[{'.$curSchedule.'}]'; |
||
491 | |||
492 | $oSchedule = json_decode($curSchedule,TRUE); |
||
493 | |||
494 | $parametersArray = array(); |
||
495 | |||
496 | foreach ($addressBookFields as $addressBookField) { |
||
497 | if (isset($locationsFieldsMapping[$addressBookField])) { |
||
498 | $parametersArray[$addressBookField] = $rows[$locationsFieldsMapping[$addressBookField]]; |
||
499 | } |
||
500 | } |
||
501 | |||
502 | $AdressBookLocationParameters = AddressBookLocation::fromArray($parametersArray); |
||
503 | |||
504 | $abContacts = new AddressBookLocation(); |
||
505 | |||
506 | $abcResults = $abContacts->addAdressBookLocation($AdressBookLocationParameters); //temporarry |
||
507 | |||
508 | array_push($results['success'], "The schedule location with address_id = ".strval($abcResults["address_id"])." added successfuly."); |
||
509 | } |
||
510 | } |
||
511 | |||
512 | return $results; |
||
513 | } |
||
514 | } |
||
515 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.