GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 7f01d7...1527b5 )
by Oleg
02:36
created

AddressBookLocation   F

Complexity

Total Complexity 77

Size/Duplication

Total Lines 450
Duplicated Lines 32.44 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 77
lcom 1
cbo 2
dl 146
loc 450
rs 2.24
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 0 12 3
A getAddressBookLocation() 13 13 1
A searchAddressBookLocations() 12 12 1
A getAddressBookLocations() 12 12 1
A getAddressBookLocationsByIDs() 0 12 1
A getRandomAddressBookLocation() 0 15 3
A addAdressBookLocation() 12 12 1
A deleteAdressBookLocation() 12 12 1
A updateAdressBookLocation() 0 12 1
A validateScheduleMode() 0 10 2
A validateScheduleEnable() 0 10 2
A validateScheduleEvery() 0 8 2
A validateScheduleWeekDays() 18 19 6
A validateScheduleMonthlyMode() 0 10 2
A validateScheduleMonthlyDates() 18 19 6
A validateScheduleNthN() 0 12 3
A validateScheduleNthWhat() 0 12 3
F addLocationsFromCsvFile() 49 165 38

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like AddressBookLocation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AddressBookLocation, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Route4Me;
3
4
use Route4Me\Common;
5
use Route4Me\Enum\Endpoint;
6
7
class AddressBookLocation extends Common
8
{
9
    public $address_id;
10
    public $address_group;
11
    public $address_alias;
12
    public $address_1;
13
    public $address_2;
14
    public $first_name;
15
    public $last_name;
16
    public $address_email;
17
    public $address_phone_number;
18
    public $address_city;
19
    public $address_state_id;
20
    public $address_country_id;
21
    public $address_zip;
22
    public $cached_lat;
23
    public $cached_lng;
24
    public $curbside_lat;
25
    public $curbside_lng;
26
    public $color;
27
    public $address_custom_data;
28
    public $schedule;
29
    
30
    public $created_timestamp;
31
    public $member_id;
32
    public $schedule_blacklist;
33
    public $in_route_count;
34
    public $last_visited_timestamp;
35
    public $last_routed_timestamp;
36
    public $local_time_window_start;
37
    public $local_time_window_end;
38
    public $local_time_window_start_2;
39
    public $local_time_window_end_2;
40
    public $service_time;
41
    public $local_timezone_string;
42
    public $address_icon;
43
    public $address_stop_type;
44
    public $address_cube;
45
    public $address_pieces;
46
    public $address_reference_no;
47
    public $address_revenue;
48
    public $address_weight;
49
    public $address_priority;
50
    public $address_customer_po;
51
    
52
    public static function fromArray(array $params)
53
    {
54
        $addressbooklocation = new AddressBookLocation();
55
        
56
        foreach ($params as $key => $value) {
57
            if (property_exists($addressbooklocation, $key)) {
58
                $addressbooklocation->{$key} = $value;
59
            }
60
        }
61
        
62
        return $addressbooklocation;
63
    }
64
    
65
    
66
    
67 View Code Duplication
    public static function getAddressBookLocation($addressId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
68
    {
69
        $ablocations = Route4Me::makeRequst(array(
70
            'url'    => Endpoint::ADDRESS_BOOK_V4,
71
            'method' => 'GET',
72
            'query'  => array(
73
                'query' => $addressId,
74
                'limit' => 30
75
            )
76
        ));
77
78
        return $ablocations;
79
    }
80
    
81 View Code Duplication
    public static function searchAddressBookLocations($params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
82
    {
83
        $allQueryFields = array('display', 'query', 'fields', 'limit', 'offset');
84
        
85
        $result = Route4Me::makeRequst(array(
86
            'url'    => Endpoint::ADDRESS_BOOK_V4,
87
            'method' => 'GET',
88
            'query'  => Route4Me::generateRequestParameters($allQueryFields, $params)
89
        ));
90
91
        return $result;
92
    }
93
    
94 View Code Duplication
    public static function getAddressBookLocations($params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
95
    {
96
        $allQueryFields = array('limit', 'offset');
97
        
98
        $ablocations = Route4Me::makeRequst(array(
99
            'url'    => Endpoint::ADDRESS_BOOK_V4,
100
            'method' => 'GET',
101
            'query'  => Route4Me::generateRequestParameters($allQueryFields, $params)
102
        ));
103
104
        return $ablocations;
105
    }
106
    
107
    public static function getAddressBookLocationsByIDs($ids)
108
    {
109
        $ablocations = Route4Me::makeRequst(array(
110
            'url'    => Endpoint::ADDRESS_BOOK_V4,
111
            'method' => 'GET',
112
            'query'  => array(
113
                'address_id' => $ids
114
            )
115
        ));
116
117
        return $ablocations;
118
    }
119
    
120
    public static function getRandomAddressBookLocation($params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
    {
122
        $ablocations = self::getAddressBookLocations(array('offset' => 0, 'limit' => 20));
123
        
124
        if (isset($ablocations["results"])) {
125
            $locationsSize = sizeof($ablocations["results"]);
126
            
127
            if ($locationsSize>0) {
128
                $randomLocationIndex = rand(0, $locationsSize - 1);
129
                return $ablocations["results"][$randomLocationIndex];
130
            } 
131
        } 
132
133
        return null;
134
    }
135
    
136
    /**
137
     * @param AddressBookLocation $params
138
    */
139 View Code Duplication
    public static function addAdressBookLocation($params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
140
    {
141
        $allBodyFields = Route4Me::getObjectProperties(new AddressBookLocation(), array('address_id', 'in_route_count'));
142
        
143
        $response = Route4Me::makeRequst(array(
144
            'url'    => Endpoint::ADDRESS_BOOK_V4,
145
            'method' => 'POST',
146
            'body'   => Route4Me::generateRequestParameters($allBodyFields, $params)
147
        ));
148
149
        return $response;
150
    }
151
    
152 View Code Duplication
    public function deleteAdressBookLocation($address_ids)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
153
    {
154
        $result = Route4Me::makeRequst(array(
155
            'url'    => Endpoint::ADDRESS_BOOK_V4,
156
            'method' => 'DELETEARRAY',
157
            'query'  => array(
158
                'address_ids' => $address_ids
159
            )
160
        ));
161
162
        return $result;
163
    }
164
    
165
    public function updateAdressBookLocation($params)
166
    {
167
        $allBodyFields = Route4Me::getObjectProperties(new AddressBookLocation(), array('in_route_count'));
168
169
        $response = Route4Me::makeRequst(array(
170
            'url'    => Endpoint::ADDRESS_BOOK_V4,
171
            'method' => 'PUT',
172
            'body'   => Route4Me::generateRequestParameters($allBodyFields, $params)
173
        ));
174
175
        return $response;
176
    }
177
        
178
    public static function validateScheduleMode($scheduleMode)
179
    {
180
        $schedMmodes = array("daily", "weekly", "monthly", "annually");
181
        
182
        if (in_array($scheduleMode, $schedMmodes)) {
183
            return TRUE; 
184
        } else {
185
            return FALSE;
186
        }
187
    }
188
    
189
    public static function validateScheduleEnable($scheduleEnabled)
190
    {
191
        $schedEnables = array(TRUE, FALSE);
192
        
193
        if (in_array($scheduleEnabled, $schedEnables)) {
194
            return TRUE;
195
        } else {
196
            return FALSE;
197
        }
198
    }
199
    
200
    public static function validateScheduleEvery($scheduleEvery)
201
    {
202
        if (is_numeric($scheduleEvery)) {
203
            return TRUE;
204
        } else {
205
            return FALSE;
206
        }
207
    }
208
    
209 View Code Duplication
    public static function validateScheduleWeekDays($scheduleWeekDays)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
210
    {
211
        $weekdays = explode(',', $scheduleWeekDays);
212
        
213
        if (sizeof($weekdays)<1) return FALSE;
214
        
215
        $isValid = TRUE;
216
        
217
        for ($i=0; $i<sizeof($weekdays); $i++) { 
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
218
            if (is_numeric($weekdays[$i])) {
219
                $wday = intval($weekdays[$i]);
220
                if ($wday<1 || $wday>7) $isValid = FALSE;
221
            } else {
222
                $isValid = FALSE;
223
            }
224
        }
225
        
226
        return $isValid;
227
    }
228
    
229
    public static function validateScheduleMonthlyMode($scheduleMonthlyMode)
230
    {
231
        $schedMonthlyMmodes = array("dates", "nth");
232
        
233
        if (in_array($scheduleMonthlyMode, $schedMonthlyMmodes)) {
234
            return TRUE;
235
        } else {
236
            return FALSE;
237
        }
238
    }
239
    
240 View Code Duplication
    public static function validateScheduleMonthlyDates($scheduleMonthlyDates)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
241
    {
242
        $monthlyDates = explode(',', $scheduleMonthlyDates);
243
        
244
        if (sizeof($monthlyDates) <1) return FALSE;
245
        
246
        $isValid = TRUE;
247
        
248
        for ($i=0; $i < sizeof($monthlyDates); $i++) { 
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
249
            if (is_numeric($monthlyDates[$i])) {
250
                $mday = intval($monthlyDates[$i]);
251
                if ($mday <1 || $mday > 31) $isValid = FALSE;
252
            } else {
253
                $isValid = FALSE;
254
            }
255
        }
256
257
        return $isValid;
258
    }
259
    
260
    public static function validateScheduleNthN($scheduleNthN)
261
    {
262
        if (!is_numeric($scheduleNthN)) return FALSE;
263
        
264
        $schedNthNs = array(1, 2, 3, 4, 5, -1);
265
        
266
        if (in_array($scheduleNthN, $schedNthNs)) {
267
            return TRUE;
268
        } else {
269
            return FALSE;
270
        }
271
    }
272
    
273
    public static function validateScheduleNthWhat($scheduleNthWhat)
274
    {
275
        if (!is_numeric($scheduleNthWhat)) return FALSE;
276
        
277
        $schedNthWhats = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
278
        
279
        if (in_array($scheduleNthWhat, $schedNthWhats)) {
280
            return TRUE;
281
        } else {
282
            return FALSE;
283
        }
284
    }
285
    
286
    /** Function adds the locations (with/without schedule) from the CSV file. 
287
     * $csvFileHandle - a file handler.
288
     * Returns array $results which contains two arrays: fail and succes.
289
     */
290
    public function addLocationsFromCsvFile($csvFileHandle, $locationsFieldsMapping)
291
    {
292
        $max_line_length = 512;
293
        $delemietr = ',';
294
        
295
        $results = array();
296
        $results['fail'] = array();
297
        $results['success'] = array();
298
        
299
        $columns = fgetcsv($csvFileHandle, $max_line_length, $delemietr);
300
        
301
        $addressBookFields = Route4Me::getObjectProperties(new AddressBookLocation(), array('address_id', 'in_route_count'));
302
303
        if (empty($columns)) {
304
            array_push($results['fail'], 'Empty CSV table');
305
            return ($results);
306
        }
307
308
        $iRow = 1;
309
        
310
        while (($rows = fgetcsv($csvFileHandle, $max_line_length, $delemietr))!==false) {
311
            if (!isset($rows[$locationsFieldsMapping['cached_lat']]) || !isset($rows[$locationsFieldsMapping['cached_lng']]) 
312
                  || !isset($rows[$locationsFieldsMapping['address_1']]) || array(null)==$rows) {
313
                continue;
314
            }
315
                      
316
            $curSchedule = "";
317
            $mode = "";
318
            
319
            $failCount = sizeof($results['fail']); 
320
            
321 View Code Duplication
            if (isset($rows[$locationsFieldsMapping['schedule_mode']])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
322
                if ($this->validateScheduleMode($rows[$locationsFieldsMapping['schedule_mode']])) {
323
                    $curSchedule = '"mode":"'.$rows[$locationsFieldsMapping['schedule_mode']].'",'; 
324
                    $mode = $rows[$locationsFieldsMapping['schedule_mode']];
325
                } else {
326
                    array_push($results['fail'], "$iRow --> Wrong schedule mode parameter"); 
327
                }
328
            } else {
329
                array_push($results['fail'], "$iRow --> The schedule mode parameter is not set"); 
330
            }
331
            
332
            if (isset($rows[$locationsFieldsMapping['schedule_enabled']])) {
333
                if ($this->validateScheduleEnable($rows[$locationsFieldsMapping['schedule_enabled']])) { 
334
                    $curSchedule .= '"enabled":'.$rows[$locationsFieldsMapping['schedule_enabled']].',';
335
                } else {
336
                    array_push($results['fail'], "$iRow --> The schedule enabled parameter is not set ");  
337
                }
338
            }
339
            
340
            if (isset($rows[$locationsFieldsMapping['schedule_every']])) {
341
                if ($this->validateScheduleEvery($rows[$locationsFieldsMapping['schedule_every']])) {
342
                    $curSchedule.='"'.$mode.'":{'.'"every":'.$rows[$locationsFieldsMapping['schedule_every']].','; 
343
                    if ($mode=='daily') {
344
                        $curSchedule = trim($curSchedule,',');
345
                        $curSchedule.='}';
346
                    }
347
                } else {
348
                    array_push($results['fail'], "$iRow --> The parameter sched_every is not set"); 
349
                }
350
            }
351
            
352
            if ($mode!='daily') {
353
                switch ($mode) {
354
                    case 'weekly':
355 View Code Duplication
                        if (isset($rows[$locationsFieldsMapping['schedule_weekdays']])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
356
                            if ($this->validateScheduleWeekDays($rows[$locationsFieldsMapping['schedule_weekdays']])) {
357
                                 $curSchedule .= '"weekdays":['.$rows[$locationsFieldsMapping['schedule_weekdays']].']}';
358
                            } else {
359
                                array_push($results['fail'], "$iRow --> Wrong weekdays"); 
360
                            }
361
                        } else {
362
                            array_push($results['fail'], "$iRow --> The parameters sched_weekdays is not set"); 
363
                        }
364
                        break;
365
                    case 'monthly':
366
                        $monthlyMode = "";
367 View Code Duplication
                        if (isset($rows[$locationsFieldsMapping['monthly_mode']])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
368
                            if ($this->validateScheduleMonthlyMode($rows[$locationsFieldsMapping['monthly_mode']])) {
369
                                 $monthlyMode = $rows[$locationsFieldsMapping['monthly_mode']];
370
                                 $curSchedule .= '"mode": "'.$rows[$locationsFieldsMapping['monthly_mode']].'",';
371
                            } else {
372
                                array_push($results['fail'], "$iRow --> Wrong monthly mode"); 
373
                            }
374
                        } else {
375
                            array_push($results['fail'], "$iRow --> The parameter sched_monthly_mode is not set"); 
376
                        }
377
                        
378
                        if ($monthlyMode!="") {
379
                            switch ($monthlyMode) {
380
                                case 'dates':
381
                                    if (isset($rows[$locationsFieldsMapping['monthly_dates']])) {
382
                                        if ($this->validateScheduleMonthlyDates($rows[$locationsFieldsMapping['monthly_dates']])) {
383
                                             $curSchedule .= '"dates":['.$rows[$locationsFieldsMapping['monthly_dates']].']}';
384
                                        } else {
385
                                            array_push($results['fail'], "$iRow --> Wrong monthly dates"); 
386
                                        }
387
                                    }
388
                                    break;
389
                                case 'nth':
390 View Code Duplication
                                    if (isset($rows[$locationsFieldsMapping['monthly_nth_n']])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
391
                                        if ($this->validateScheduleNthN($rows[$locationsFieldsMapping['monthly_nth_n']])) {
392
                                             $curSchedule .= '"nth":{"n":'.$rows[$locationsFieldsMapping['monthly_nth_n']].',';
393
                                        } else {
394
                                            array_push($results['fail'], "$iRow --> Wrong parameter sched_nth_n"); 
395
                                        }
396
                                    } else {
397
                                        array_push($results['fail'], "$iRow --> The parameter sched_nth_n is not set"); 
398
                                    }
399
                                    
400 View Code Duplication
                                    if ($curSchedule!="") {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
401
                                        if (isset($rows[$locationsFieldsMapping['monthly_nth_wwhat']])) {
402
                                            if ($this->validateScheduleNthWhat($rows[$locationsFieldsMapping['monthly_nth_wwhat']])) {
403
                                                 $curSchedule .= '"what":'.$rows[$locationsFieldsMapping['monthly_nth_wwhat']].'}}';
404
                                            } else {
405
                                                array_push($results['fail'], "$iRow --> Wrong parameter sched_nth_what"); 
406
                                            }
407
                                        } else {
408
                                            array_push($results['fail'], "$iRow --> The parameter sched_nth_what is not set"); 
409
                                        }
410
                                    }
411
                                    break;
412
                            }
413
                        }
414
                        break;
415
                    default:
416
                        $curSchedule = "";
417
                        break;
418
                }
419
            }
420
421
            if (sizeof($results['fail'])>$failCount) {
422
                $curSchedule = "";
423
            }
424
425
            if (($mode=='daily' || $mode=='weekly' || $mode=='monthy') && $curSchedule=="") {
426
                $iRow++; 
427
                continue;
428
            }
429
            
430
            $curSchedule = strtolower($curSchedule);
431
            
432
            $curSchedule = '[{'.$curSchedule.'}]';
433
434
            $oSchedule = json_decode($curSchedule,TRUE);
0 ignored issues
show
Unused Code introduced by
$oSchedule is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
435
            
436
            $parametersArray = array();
437
            
438
            foreach ($addressBookFields as $addressBookField) {
439
                if (isset($locationsFieldsMapping[$addressBookField])) {
440
                    $parametersArray[$addressBookField] = $rows[$locationsFieldsMapping[$addressBookField]];
441
                }
442
            }
443
            
444
            $AdressBookLocationParameters = AddressBookLocation::fromArray($parametersArray);
445
            
446
            $abContacts = new AddressBookLocation();
447
448
            $abcResults = $abContacts->addAdressBookLocation($AdressBookLocationParameters); //temporarry
449
            
450
            array_push($results['success'], "The schedule location with address_id = ".strval($abcResults["address_id"])." added successfuly.");
451
        }
452
453
        return $results;
454
    }
455
456
 }
457