Conditions | 67 |
Paths | 4035 |
Total Lines | 168 |
Code Lines | 117 |
Lines | 39 |
Ratio | 23.21 % |
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 |
||
279 | public function addLocationsFromCsvFile($csvFileHandle, $locationsFieldsMapping) |
||
280 | { |
||
281 | $max_line_length = 512; |
||
282 | $delemietr=','; |
||
283 | |||
284 | $results['fail']=array(); |
||
285 | $results['success']=array(); |
||
286 | |||
287 | $columns = fgetcsv($csvFileHandle, $max_line_length, $delemietr); |
||
288 | if (!$columns) { |
||
289 | array_push($results['fail'],'Empty CSV table'); |
||
290 | return ($results); |
||
291 | } |
||
292 | |||
293 | $iRow=1; |
||
294 | |||
295 | while (($rows = fgetcsv($csvFileHandle, $max_line_length, $delemietr)) !== false) { |
||
296 | if ($rows[$locationsFieldsMapping['cached_lat']] && $rows[$locationsFieldsMapping['cached_lng']] && $rows[$locationsFieldsMapping['address_1']] && array(null) !== $rows) { |
||
297 | $schedule=""; |
||
298 | $mode=""; |
||
299 | |||
300 | View Code Duplication | if (isset($rows[$locationsFieldsMapping['schedule_mode']])) { |
|
301 | if ($this->validateScheduleMode($rows[$locationsFieldsMapping['schedule_mode']])) { |
||
302 | $schedule='"mode":"'.$rows[$locationsFieldsMapping['schedule_mode']].'",'; |
||
303 | $mode=$rows[$locationsFieldsMapping['schedule_mode']]; |
||
304 | } |
||
305 | else {array_push($results['fail'],"$iRow --> Wrong schedule mode parameter"); $schedule="";} |
||
306 | } |
||
307 | else {array_push($results['fail'],"$iRow --> The schedule mode parameter is not set"); $schedule="";} |
||
308 | |||
309 | if (isset($rows[$locationsFieldsMapping['schedule_enabled']])) { |
||
310 | if ($this->validateScheduleEnable($rows[$locationsFieldsMapping['schedule_enabled']])) { |
||
311 | $schedule.='"enabled":'.$rows[$locationsFieldsMapping['schedule_enabled']].','; |
||
312 | } |
||
313 | else {array_push($results['fail'],"$iRow --> The schedule enabled parameter is not set "); $schedule="";} |
||
314 | } |
||
315 | |||
316 | if (isset($rows[$locationsFieldsMapping['schedule_every']])) { |
||
317 | if ($this->validateScheduleEvery($rows[$locationsFieldsMapping['schedule_every']])) { |
||
318 | $schedule.='"'.$mode.'":{'.'"every":'.$rows[$locationsFieldsMapping['schedule_every']].','; |
||
319 | if ($mode=='daily') { |
||
320 | $schedule=trim($schedule,','); |
||
321 | $schedule.='}'; |
||
322 | } |
||
323 | } |
||
324 | else {array_push($results['fail'],"$iRow --> The parameter sched_every is not set"); $schedule=""; } |
||
325 | } |
||
326 | |||
327 | if ($mode!='daily') { |
||
328 | switch ($mode) { |
||
329 | case 'weekly': |
||
330 | View Code Duplication | if (isset($rows[$locationsFieldsMapping['schedule_weekdays']])) { |
|
331 | if ($this->validateScheduleWeekDays($rows[$locationsFieldsMapping['schedule_weekdays']])) { |
||
332 | $schedule.='"weekdays":['.$rows[$locationsFieldsMapping['schedule_weekdays']].']}'; |
||
333 | } |
||
334 | else {array_push($results['fail'],"$iRow --> Wrong weekdays"); $schedule="";} |
||
335 | } |
||
336 | else {array_push($results['fail'],"$iRow --> The parameters sched_weekdays is not set"); $schedule="";} |
||
337 | break; |
||
338 | case 'monthly': |
||
339 | $monthlyMode=""; |
||
340 | View Code Duplication | if (isset($rows[$locationsFieldsMapping['monthly_mode']])) { |
|
341 | if ($this->validateScheduleMonthlyMode($rows[$locationsFieldsMapping['monthly_mode']])) { |
||
342 | $monthlyMode=$rows[$locationsFieldsMapping['monthly_mode']]; |
||
343 | $schedule.='"mode": "'.$rows[$locationsFieldsMapping['monthly_mode']].'",'; |
||
344 | } |
||
345 | else {array_push($results['fail'],"$iRow --> Wrong monthly mode"); $schedule="";} |
||
346 | } |
||
347 | else {array_push($results['fail'],"$iRow --> The parameter sched_monthly_mode is not set"); $schedule="";} |
||
348 | |||
349 | if ($monthlyMode!="") { |
||
350 | switch ($monthlyMode) { |
||
351 | case 'dates': |
||
352 | if (isset($rows[$locationsFieldsMapping['monthly_dates']])) { |
||
353 | if ($this->validateScheduleMonthlyDates($rows[$locationsFieldsMapping['monthly_dates']])) { |
||
354 | $schedule.='"dates":['.$rows[$locationsFieldsMapping['monthly_dates']].']}'; |
||
355 | } |
||
356 | else {array_push($results['fail'],"$iRow --> Wrong monthly dates"); $schedule="";} |
||
357 | } |
||
358 | break; |
||
359 | case 'nth': |
||
360 | View Code Duplication | if (isset($rows[$locationsFieldsMapping['monthly_nth_n']])) { |
|
361 | if ($this->validateScheduleNthN($rows[$locationsFieldsMapping['monthly_nth_n']])) { |
||
362 | $schedule.='"nth":{"n":'.$rows[$locationsFieldsMapping['monthly_nth_n']].','; |
||
363 | } |
||
364 | else {array_push($results['fail'],"$iRow --> Wrong parameter sched_nth_n"); $schedule="";} |
||
365 | } |
||
366 | else {array_push($results['fail'],"$iRow --> The parameter sched_nth_n is not set"); $schedule="";} |
||
367 | |||
368 | View Code Duplication | if ($schedule!="") { |
|
369 | if (isset($rows[$locationsFieldsMapping['monthly_nth_wwhat']])) { |
||
370 | if ($this->validateScheduleNthWhat($rows[$locationsFieldsMapping['monthly_nth_wwhat']])) { |
||
371 | $schedule.='"what":'.$rows[$locationsFieldsMapping['monthly_nth_wwhat']].'}}'; |
||
372 | } |
||
373 | else {array_push($results['fail'],"$iRow --> Wrong parameter sched_nth_what"); $schedule="";} |
||
374 | } |
||
375 | else {array_push($results['fail'],"$iRow --> The parameter sched_nth_what is not set"); $schedule="";} |
||
376 | } |
||
377 | |||
378 | break; |
||
379 | } |
||
380 | } |
||
381 | break; |
||
382 | default: |
||
383 | $schedule==""; |
||
384 | break; |
||
385 | } |
||
386 | |||
387 | } |
||
388 | |||
389 | if (($mode=='daily' || $mode=='weekly' || $mode=='monthy') && $schedule=="") {$iRow++; continue;} |
||
390 | |||
391 | $schedule=strtolower($schedule); |
||
392 | |||
393 | $schedule='[{'.$schedule.'}]'; |
||
394 | |||
395 | $oSchedule= json_decode($schedule,TRUE); |
||
396 | |||
397 | $AdressBookLocationParameters=AddressBookLocation::fromArray(array( |
||
398 | "cached_lat" => $rows[$locationsFieldsMapping['cached_lat']], |
||
399 | "cached_lng" => $rows[$locationsFieldsMapping['cached_lng']], |
||
400 | "curbside_lat" => isset($locationsFieldsMapping['curbside_lat']) ? $rows[$locationsFieldsMapping['curbside_lat']] : null, |
||
401 | "curbside_lng" => isset($locationsFieldsMapping['curbside_lng']) ? $rows[$locationsFieldsMapping['curbside_lng']] : null, |
||
402 | "address_alias" => isset($locationsFieldsMapping['address_alias']) ? $rows[$locationsFieldsMapping['address_alias']] : null, |
||
403 | "address_1" => $rows[$locationsFieldsMapping['address_1']], |
||
404 | "address_2" => isset($locationsFieldsMapping['address_2']) ? $rows[$locationsFieldsMapping['address_2']] : null, |
||
405 | "address_city" => isset($locationsFieldsMapping['address_city']) ? $rows[$locationsFieldsMapping['address_city']] : null, |
||
406 | "address_state_id" => isset($locationsFieldsMapping['address_state_id']) ? $rows[$locationsFieldsMapping['address_state_id']] : null, |
||
407 | "address_zip" => isset($locationsFieldsMapping['address_zip']) ? $rows[$locationsFieldsMapping['address_zip']] : null, |
||
408 | "address_phone_number" => isset($locationsFieldsMapping['address_phone_number']) ? $rows[$locationsFieldsMapping['address_phone_number']] : null, |
||
409 | "schedule" => isset($oSchedule) ? $oSchedule : null, |
||
410 | "address_group" => isset($locationsFieldsMapping['address_group']) ? $rows[$locationsFieldsMapping['address_group']] : null, |
||
411 | "first_name" => isset($locationsFieldsMapping['first_name']) ? $rows[$locationsFieldsMapping['first_name']] : null, |
||
412 | "last_name" => isset($locationsFieldsMapping['last_name']) ? $rows[$locationsFieldsMapping['last_name']] : null, |
||
413 | "local_time_window_start" => isset($locationsFieldsMapping['local_time_window_start']) ? $rows[$locationsFieldsMapping['local_time_window_start']] : null, |
||
414 | "local_time_window_end" => isset($locationsFieldsMapping['local_time_window_end']) ? $rows[$locationsFieldsMapping['local_time_window_end']] : null, |
||
415 | "local_time_window_start_2" => isset($locationsFieldsMapping['local_time_window_start_2']) ? $rows[$locationsFieldsMapping['local_time_window_start_2']] : null, |
||
416 | "local_time_window_end_2" => isset($locationsFieldsMapping['local_time_window_end_2']) ? $rows[$locationsFieldsMapping['local_time_window_end_2']] : null, |
||
417 | "address_email" => isset($locationsFieldsMapping['address_email']) ? $rows[$locationsFieldsMapping['address_email']] : null, |
||
418 | "address_country_id" => isset($locationsFieldsMapping['address_country_id']) ? $rows[$locationsFieldsMapping['address_country_id']] : null, |
||
419 | "address_custom_data" => isset($locationsFieldsMapping['address_custom_data']) ? $rows[$locationsFieldsMapping['address_custom_data']] : null, |
||
420 | "schedule_blacklist" => isset($locationsFieldsMapping['schedule_blacklist']) ? $rows[$locationsFieldsMapping['schedule_blacklist']] : null, |
||
421 | "service_time" => isset($locationsFieldsMapping['service_time']) ? $rows[$locationsFieldsMapping['service_time']] : null, |
||
422 | "local_timezone_string" => isset($locationsFieldsMapping['local_timezone_string']) ? $rows[$locationsFieldsMapping['local_timezone_string']] : null, |
||
423 | "color" => isset($locationsFieldsMapping['color']) ? $rows[$locationsFieldsMapping['color']] : null, |
||
424 | "address_icon" => isset($locationsFieldsMapping['address_icon']) ? $rows[$locationsFieldsMapping['address_icon']] : null, |
||
425 | "address_stop_type" => isset($locationsFieldsMapping['address_stop_type']) ? $rows[$locationsFieldsMapping['address_stop_type']] : null, |
||
426 | "address_cube" => isset($locationsFieldsMapping['address_cube']) ? $rows[$locationsFieldsMapping['address_cube']] : null, |
||
427 | "address_pieces" => isset($locationsFieldsMapping['address_pieces']) ? $rows[$locationsFieldsMapping['address_pieces']] : null, |
||
428 | "address_reference_no" => isset($locationsFieldsMapping['address_reference_no']) ? $rows[$locationsFieldsMapping['address_reference_no']] : null, |
||
429 | "address_revenue" => isset($locationsFieldsMapping['address_revenue']) ? $rows[$locationsFieldsMapping['address_revenue']] : null, |
||
430 | "address_weight" => isset($locationsFieldsMapping['address_weight']) ? $rows[$locationsFieldsMapping['address_weight']] : null, |
||
431 | "address_priority" => isset($locationsFieldsMapping['address_priority']) ? $rows[$locationsFieldsMapping['address_priority']] : null, |
||
432 | "address_customer_po" => isset($locationsFieldsMapping['address_customer_po']) ? $rows[$locationsFieldsMapping['address_customer_po']] : null, |
||
433 | )); |
||
434 | |||
435 | $abContacts=new AddressBookLocation(); |
||
436 | |||
437 | $abcResults=$abContacts->addAdressBookLocation($AdressBookLocationParameters); //temporarry |
||
438 | |||
439 | array_push($results['success'],"The schedule location with address_id = ".strval($abcResults["address_id"])." added successfuly."); |
||
440 | |||
441 | } |
||
442 | |||
443 | } |
||
444 | |||
445 | return $results; |
||
446 | } |
||
447 | |||
452 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.