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.
Completed
Branch Editing-Fixing (c4d168)
by Igor
03:30
created

Order   F

Complexity

Total Complexity 112

Size/Duplication

Total Lines 379
Duplicated Lines 32.19 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 122
loc 379
rs 2
c 0
b 0
f 0
wmc 112
lcom 1
cbo 2

14 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 0 10 3
D addOrder() 0 43 33
A addOrder2Route() 14 14 3
A addOrder2Optimization() 15 15 4
B getOrder() 0 18 8
A getOrders() 13 13 3
A getRandomOrderId() 15 15 3
A getRandomOrder() 15 15 3
A removeOrder() 12 12 2
A updateOrder() 10 10 1
B searchOrder() 0 17 7
A validateLatitude() 8 8 4
A validateLongitude() 8 8 4
F addOrdersFromCsvFile() 12 111 34

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 Order 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 Order, 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 Order extends Common
8
{
9
    public $address_1;
10
    public $address_2;
11
    public $cached_lat;
12
    public $cached_lng;
13
    public $curbside_lat;
14
    public $curbside_lng;
15
    public $address_alias;
16
    public $address_city;
17
    public $EXT_FIELD_first_name;
18
    public $EXT_FIELD_last_name;
19
    public $EXT_FIELD_email;
20
    public $EXT_FIELD_phone;
21
    public $EXT_FIELD_custom_data;
22
    
23
    public $color;
24
    public $order_icon;
25
    public $local_time_window_start;
26
    public $local_time_window_end;
27
    public $local_time_window_start_2;
28
    public $local_time_window_end_2;
29
    public $service_time;
30
    
31
    public $day_scheduled_for_YYMMDD;
32
    
33
    public $route_id;
34
    public $redirect;
35
    public $optimization_problem_id;
36
    public $order_id;
37
    public $order_ids;
38
    
39
    public $day_added_YYMMDD;
40
    public $scheduled_for_YYMMDD;
41
    public $fields;
42
    public $offset;
43
    public $limit;
44
    public $query;
45
    
46
    public $created_timestamp;
47
    public $order_status_id;
48
    public $member_id;
49
    public $address_state_id;
50
    public $address_country_id;
51
    public $address_zip;
52
    public $in_route_count;
53
    public $last_visited_timestamp;
54
    public $last_routed_timestamp;
55
    public $local_timezone_string;
56
    public $is_validated;
57
    public $is_pending;
58
    public $is_accepted;
59
    public $is_started;
60
    public $is_completed;
61
    public $custom_user_fields;
62
    
63
    public static function fromArray(array $params) {
64
        $order= new Order();
65
        foreach($params as $key => $value) {
66
            if (property_exists($order, $key)) {
67
                $order->{$key} = $value;
68
            }
69
        }
70
        
71
        return $order;
72
    }
73
    
74
    public static function addOrder($params)
75
    {
76
        $response = Route4Me::makeRequst(array(
77
            'url'    => Endpoint::ORDER_V4,
78
            'method' => 'POST',
79
            'body'   => array(
80
                'address_1'                 =>  isset($params->address_1) ? $params->address_1: null,
81
                'address_2'                 =>  isset($params->address_2) ? $params->address_2: null,
82
                'member_id'                 =>  isset($params->member_id) ? $params->member_id: null,
83
                'cached_lat'                =>  isset($params->cached_lat) ? $params->cached_lat : null,
84
                'cached_lng'                =>  isset($params->cached_lng) ? $params->cached_lng : null,
85
                'curbside_lat'              =>  isset($params->curbside_lat) ? $params->curbside_lat : null,
86
                'curbside_lng'              =>  isset($params->curbside_lng) ? $params->curbside_lng : null,
87
                'color'                     =>  isset($params->color) ? $params->color : null,
88
                'order_icon'                =>  isset($params->order_icon) ? $params->order_icon : null,
89
                'day_scheduled_for_YYMMDD'  =>  isset($params->day_scheduled_for_YYMMDD) ? $params->day_scheduled_for_YYMMDD : null,
90
                'address_alias'             =>  isset($params->address_alias) ? $params->address_alias : null,
91
                'address_city'              =>  isset($params->address_city) ? $params->address_city: null,
92
                'address_state_id'          =>  isset($params->address_state_id) ? $params->address_state_id: null,
93
                'address_country_id'        =>  isset($params->address_country_id) ? $params->address_country_id: null,
94
                'address_zip'               =>  isset($params->address_zip) ? $params->address_zip: null,
95
                'local_time_window_start'   =>  isset($params->local_time_window_start) ? $params->local_time_window_start: null,
96
                'local_time_window_end'     =>  isset($params->local_time_window_end) ? $params->local_time_window_end: null,
97
                'local_time_window_start_2' =>  isset($params->local_time_window_start_2) ? $params->local_time_window_start_2: null,
98
                'local_time_window_end_2'   =>  isset($params->local_time_window_end_2) ? $params->local_time_window_end_2: null,
99
                'service_time'              =>  isset($params->service_time) ? $params->service_time: null,
100
                'local_timezone_string'     =>  isset($params->local_timezone_string) ? $params->local_timezone_string: null,
101
                'EXT_FIELD_first_name'      =>  isset($params->EXT_FIELD_first_name) ? $params->EXT_FIELD_first_name: null,
102
                'EXT_FIELD_last_name'       =>  isset($params->EXT_FIELD_last_name) ? $params->EXT_FIELD_last_name: null,
103
                'EXT_FIELD_email'           =>  isset($params->EXT_FIELD_email) ? $params->EXT_FIELD_email: null,
104
                'EXT_FIELD_phone'           =>  isset($params->EXT_FIELD_phone) ? $params->EXT_FIELD_phone: null,
105
                'EXT_FIELD_custom_data'     =>  isset($params->EXT_FIELD_custom_data) ? $params->EXT_FIELD_custom_data: null,
106
                'is_validated'              =>  isset($params->is_validated) ? $params->is_validated: null,
107
                'is_pending'                =>  isset($params->is_pending) ? $params->is_pending: null,
108
                'is_accepted'               =>  isset($params->is_accepted) ? $params->is_accepted: null,
109
                'is_started'                =>  isset($params->is_started) ? $params->is_started: null,
110
                'is_completed'              =>  isset($params->is_completed) ? $params->is_completed: null,
111
                'custom_user_fields'        =>  isset($params->custom_user_fields) ? $params->custom_user_fields: null
112
            )
113
        ));
114
115
        return $response;
116
    }
117
118 View Code Duplication
    public static function addOrder2Route($params,$body)
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...
119
    {
120
        $response = Route4Me::makeRequst(array(
121
            'url'    => Endpoint::ROUTE_V4,
122
            'method' => 'PUT',
123
            'query'  => array(
124
                'route_id' => isset($params->route_id) ? $params->route_id: null,
125
                'redirect' => isset($params->redirect) ? $params->redirect : null
126
            ),
127
            'body' => (array)$body
128
        ));
129
130
        return $response;
131
    }
132
    
133 View Code Duplication
    public static function addOrder2Optimization($params,$body)
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...
134
    {
135
        $response = Route4Me::makeRequst(array(
136
            'url'    => Endpoint::OPTIMIZATION_PROBLEM,
137
            'method' => 'PUT',
138
            'query'  => array(
139
                'optimization_problem_id' =>  isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null,
140
                'redirect'                => isset($params['redirect']) ? $params['redirect'] : null,
141
                'device_type'             => isset($params['device_type']) ? $params['device_type'] : null
142
            ),
143
            'body'  => (array)$body
144
        ));
145
146
        return $response;
147
    }
148
    
149
    public static function getOrder($params)
150
    {
151
        $response = Route4Me::makeRequst(array(
152
            'url'    => Endpoint::ORDER_V4,
153
            'method' => 'GET',
154
            'query'  => array(
155
                'order_id'             => isset($params->order_id) ? $params->order_id: null,
156
                'fields'               => isset($params->fields) ? $params->fields: null,
157
                'day_added_YYMMDD'     => isset($params->day_added_YYMMDD) ? $params->day_added_YYMMDD: null,
158
                'scheduled_for_YYMMDD' => isset($params->scheduled_for_YYMMDD) ? $params->scheduled_for_YYMMDD: null,
159
                'query'                => isset($params->query) ? $params->query: null,
160
                'offset'               => isset($params->offset) ? $params->offset: null,
161
                'limit'                => isset($params->limit) ? $params->limit: null
162
            )
163
        ));
164
165
        return $response;
166
    }
167
    
168 View Code Duplication
    public static function getOrders($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...
169
    {
170
        $response = Route4Me::makeRequst(array(
171
            'url'    => Endpoint::ORDER_V4,
172
            'method' => 'GET',
173
            'query'  => array(
174
                'offset' => isset($params->offset) ? $params->offset: null,
175
                'limit'  => isset($params->limit) ? $params->limit: null
176
            )
177
        ));
178
179
        return $response;
180
    }
181
    
182 View Code Duplication
    public function getRandomOrderId($offset,$limit)
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...
183
    {
184
        $params = array('offset' => $offset, 'limit' => $limit);
185
        
186
        $orders = self::getOrders($params);
187
        
188
        if (is_null($orders)) return null;
189
        if (!isset($orders['results'])) return null;
190
        
191
        $randomIndex = rand(0, sizeof($orders['results'])-1);
192
        
193
        $order = $orders['results'][$randomIndex];
194
        
195
        return $order['order_id'];
196
    }
197
    
198 View Code Duplication
    public function getRandomOrder($offset,$limit)
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...
199
    {
200
        $params = array('offset' => $offset, 'limit' => $limit);
201
        
202
        $orders = self::getOrders($params);
203
        
204
        if (is_null($orders)) return null;
205
        if (!isset($orders['results'])) return null;
206
        
207
        $randomIndex = rand(0, sizeof($orders['results'])-1);
208
        
209
        $order = $orders['results'][$randomIndex];
210
        
211
        return $order;
212
    }
213
    
214 View Code Duplication
    public static function removeOrder($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...
215
    {
216
        $response = Route4Me::makeRequst(array(
217
            'url'    => Endpoint::ORDER_V4,
218
            'method' => 'DELETE',
219
            'body'   => array(
220
                'order_ids' =>  isset($params->order_ids) ? $params->order_ids: null
221
            )
222
        ));
223
224
        return $response;
225
    }
226
    
227 View Code Duplication
    public static function updateOrder($body)
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...
228
    {
229
        $response = Route4Me::makeRequst(array(
230
            'url'    => Endpoint::ORDER_V4,
231
            'method' => 'PUT',
232
            'body'   => (array)$body
233
        ));
234
235
        return $response;
236
    }
237
    
238
    public static function searchOrder($params)
239
    {
240
        $response = Route4Me::makeRequst(array(
241
            'url'    => Endpoint::ORDER_V4,
242
            'method' => 'GET',
243
            'query'  => array(
244
                'day_added_YYMMDD'     =>  isset($params->day_added_YYMMDD) ? $params->day_added_YYMMDD: null,
245
                'scheduled_for_YYMMDD' =>  isset($params->scheduled_for_YYMMDD) ? $params->scheduled_for_YYMMDD: null,
246
                'fields'               =>  isset($params->fields) ? $params->fields: null,
247
                'offset'               =>  isset($params->offset) ? $params->offset: null,
248
                'limit'                =>  isset($params->limit) ? $params->limit: null,
249
                'query'                =>  isset($params->query) ? $params->query: null,
250
            )
251
        ));
252
253
        return $response;
254
    }
255
    
256 View Code Duplication
    public static function validateLatitude($lat)
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...
257
    {
258
        if (!is_numeric($lat)) return false;
259
        
260
        if ($lat>90 || $lat<-90) return false;
261
        
262
        return true;
263
    }
264
    
265 View Code Duplication
    public static function validateLongitude($lng)
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...
266
    {
267
        if (!is_numeric($lng)) return false;
268
        
269
        if ($lng>180 || $lng<-180) return false;
270
        
271
        return true;
272
    }
273
    
274
    public function addOrdersFromCsvFile($csvFileHandle, $ordersFieldsMapping)
275
    {
276
        $max_line_length = 512;
277
        $delemietr = ',';
278
        
279
        $results = array();
280
        $results['fail'] = array();
281
        $results['success'] = array();
282
        
283
        $columns = fgetcsv($csvFileHandle, $max_line_length, $delemietr);
284
        
285
        if (!empty($columns)) {
286
             array_push($results['fail'],'Empty CSV table');
287
             return ($results);
288
        }
289
                 
290
        $iRow=1;
291
        
292
        while (($rows = fgetcsv($csvFileHandle, $max_line_length, $delemietr)) !== false) {
293
            if ($rows[$ordersFieldsMapping['cached_lat']] && $rows[$ordersFieldsMapping['cached_lng']] && $rows[$ordersFieldsMapping['address_1']] && array(null) !== $rows) {
294
                
295
                $cached_lat = 0.000;
0 ignored issues
show
Unused Code introduced by
$cached_lat 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...
296
                
297 View Code Duplication
                if (!$this->validateLatitude($rows[$ordersFieldsMapping['cached_lat']])) {
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...
298
                    array_push($results['fail'],"$iRow --> Wrong cached_lat"); 
299
                    $iRow++;
300
                    continue;
301
                }
302
                else $cached_lat = doubleval($rows[$ordersFieldsMapping['cached_lat']]);
303
                
304
                $cached_lng = 0.000;
0 ignored issues
show
Unused Code introduced by
$cached_lng 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...
305
                
306 View Code Duplication
                if (!$this->validateLongitude($rows[$ordersFieldsMapping['cached_lng']])) {
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...
307
                    array_push($results['fail'],"$iRow --> Wrong cached_lng"); 
308
                    $iRow++;
309
                    continue;
310
                }
311
                else $cached_lng = doubleval($rows[$ordersFieldsMapping['cached_lng']]);
312
                
313
                if (isset($ordersFieldsMapping['curbside_lat'])) {
314
                    if (!$this->validateLatitude($rows[$ordersFieldsMapping['curbside_lat']])) {
315
                        array_push($results['fail'],"$iRow --> Wrong curbside_lat"); 
316
                        $iRow++;
317
                        continue;
318
                    }
319
                }
320
                
321
                if (isset($ordersFieldsMapping['curbside_lng'])) {
322
                    if (!$this->validateLongitude($rows[$ordersFieldsMapping['curbside_lng']])) {
323
                        array_push($results['fail'],"$iRow --> Wrong curbside_lng"); 
324
                        $iRow++;
325
                        continue;
326
                    }
327
                }
328
                
329
                $address = $rows[$ordersFieldsMapping['address_1']];
330
                
331
                if (isset($ordersFieldsMapping['order_city'])) $address.=', '.$rows[$ordersFieldsMapping['order_city']];
332
                if (isset($ordersFieldsMapping['order_state_id'])) $address.=', '.$rows[$ordersFieldsMapping['order_state_id']];
333
                if (isset($ordersFieldsMapping['order_zip_code'])) $address.=', '.$rows[$ordersFieldsMapping['order_zip_code']];
334
                if (isset($ordersFieldsMapping['order_country_id'])) $address.=', '.$rows[$ordersFieldsMapping['order_country_id']];
335
                
336
                echo "$iRow --> ".$ordersFieldsMapping['day_scheduled_for_YYMMDD'].", ".$rows[$ordersFieldsMapping['day_scheduled_for_YYMMDD']]."<br>";
337
                
338
                $orderParameters = Order::fromArray(array(
339
                    "cached_lat"                => $cached_lat,
340
                    "cached_lng"                => $cached_lng,
341
                    "curbside_lat"              => isset($ordersFieldsMapping['curbside_lat']) ? $rows[$ordersFieldsMapping['curbside_lat']] : null,
342
                    "curbside_lng"              => isset($ordersFieldsMapping['curbside_lng']) ? $rows[$ordersFieldsMapping['curbside_lng']] : null,
343
                    "color"                     => isset($ordersFieldsMapping['color']) ? $rows[$ordersFieldsMapping['color']] : null,
344
                    "day_scheduled_for_YYMMDD"  => isset($ordersFieldsMapping['day_scheduled_for_YYMMDD'])
345
                                                      ? $rows[$ordersFieldsMapping['day_scheduled_for_YYMMDD']] : null,
346
                    "address_alias"             => isset($ordersFieldsMapping['address_alias']) ? $rows[$ordersFieldsMapping['address_alias']] : null,
347
                    "address_1"                 => $address,
348
                    "address_2"                 => isset($ordersFieldsMapping['address_2']) ? $rows[$ordersFieldsMapping['address_2']] : null,
349
                    "local_time_window_start"   => isset($ordersFieldsMapping['local_time_window_start'])
350
                                                      ? $rows[$ordersFieldsMapping['local_time_window_start']] : null,
351
                    "local_time_window_end"     => isset($ordersFieldsMapping['local_time_window_end'])
352
                                                      ? $rows[$ordersFieldsMapping['local_time_window_end']] : null,
353
                    "local_time_window_start_2" => isset($ordersFieldsMapping['local_time_window_start_2'])
354
                                                      ? $rows[$ordersFieldsMapping['local_time_window_start_2']] : null,
355
                    "local_time_window_end_2"   => isset($ordersFieldsMapping['local_time_window_end_2'])
356
                                                      ? $rows[$ordersFieldsMapping['local_time_window_end_2']] : null,
357
                    "service_time"              => isset($ordersFieldsMapping['service_time'])
358
                                                      ? $rows[$ordersFieldsMapping['service_time']] : null,
359
                    "EXT_FIELD_first_name"      => isset($ordersFieldsMapping['EXT_FIELD_first_name'])
360
                                                      ? $rows[$ordersFieldsMapping['EXT_FIELD_first_name']] : null,
361
                    "EXT_FIELD_last_name"       => isset($ordersFieldsMapping['EXT_FIELD_last_name'])
362
                                                      ? $rows[$ordersFieldsMapping['EXT_FIELD_last_name']] : null,
363
                    "EXT_FIELD_email"           => isset($ordersFieldsMapping['EXT_FIELD_email'])
364
                                                      ? $rows[$ordersFieldsMapping['EXT_FIELD_email']] : null,
365
                    "EXT_FIELD_phone"           => isset($ordersFieldsMapping['EXT_FIELD_phone'])
366
                                                      ? $rows[$ordersFieldsMapping['EXT_FIELD_phone']] : null,
367
                    "EXT_FIELD_custom_data"     => isset($ordersFieldsMapping['EXT_FIELD_custom_data'])
368
                                                      ? $rows[$ordersFieldsMapping['EXT_FIELD_custom_data']] : null,
369
                    "order_icon"                => isset($ordersFieldsMapping['order_icon']) ? $rows[$ordersFieldsMapping['order_icon']] : null,
370
                ));
371
                
372
                $order = new Order();
373
                
374
                $orderResults = $order->addOrder($orderParameters);
375
                
376
                array_push($results['success'],"The order with order_id = ".strval($orderResults["order_id"])." added successfuly.");
377
            }
378
            else {
379
                array_push($results['fail'],"$iRow --> one of the parameters cached_lat, cached_lng, address_1 is not set"); 
380
            }
381
            
382
            $iRow++;
383
        }
384
    }
385
}
386