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 ( 39430a...e623a9 )
by Oleg
02:28
created

Route   F

Complexity

Total Complexity 83

Size/Duplication

Total Lines 453
Duplicated Lines 35.54 %

Coupling/Cohesion

Components 7
Dependencies 4

Importance

Changes 0
Metric Value
dl 161
loc 453
rs 2
c 0
b 0
f 0
wmc 83
lcom 7
cbo 4

22 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 8 34 4
D getRoutes() 0 47 11
A getRoutePoints() 15 15 3
A duplicateRoute() 0 14 1
A resequenceRoute() 17 17 4
A resequenceAllAddresses() 16 16 4
A mergeRoutes() 0 16 3
A shareRoute() 17 17 3
B getRandomRouteId() 0 29 6
A update() 17 17 4
A updateAddress() 15 15 4
A addAddresses() 0 14 3
A insertAddressOptimalPosition() 17 17 4
B addNoteFile() 0 31 11
A delete() 21 21 1
A GetAddressesFromRoute() 0 7 2
A GetRandomAddressFromRoute() 0 13 2
A getRouteId() 0 4 1
A getOptimizationId() 0 4 1
A GetLastLocation() 0 15 3
A GetTrackingHistoryFromTimeRange() 18 18 6
A GetAssetTracking() 0 14 2

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 Route 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 Route, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Route4Me;
4
5
use Route4Me\Common;
6
use Route4Me\Address;
7
use Route4Me\Exception\BadParam;
8
use Route4Me\RouteParameters;
9
use Route4Me\Route4Me;
10
use GuzzleHttp\Client;
11
12
class Route extends Common
13
{
14
    static public $apiUrl = '/api.v4/route.php';
15
	static public $apiUrlAddress = '/api.v4/address.php';
16
	static public $apiUrlDuplicate='/actions/duplicate_route.php';
17
	static public $apiUrlDelete='/actions/delete_routes.php';
18
	static public $apiUrlReseq='/api.v3/route/reoptimize_2.php';
19
	static public $apiUrlMerge='/actions/merge_routes.php';
20
	static public $apiUrlShare='/actions/route/share_route.php';
21
	static public $apiUrlNoteFile='/actions/addRouteNotes.php';
22
	static public $apiUrlAsset='/api.v4/status.php';
23
	static public $apiUrlDeviceLocation='/api/track/get_device_location.php';
24
	//static public $apiUrlMove='/actions/route/move_route_destination.php';
25
    public $route_id;
26
	public $route_destination_id;
27
    public $optimization_problem_id;
28
    public $vehicle_alias;
29
    public $driver_alias;
30
    public $trip_distance;
31
    public $mpg;
32
    public $gas_price;
33
    public $route_duration_sec;
34
    public $parameters;
35
    public $addresses = array();
36
    public $links = array();
37
    public $directions = array();
38
    public $path = array();
39
    public $tracking_history = array();
40
	public $recipient_email;
41
	public $httpheaders;
42
	public $is_unrouted;
43
	
44
	public $dev_lat;
45
	public $dev_lng;
46
47
    public static function fromArray(array $params) 
48
    {
49
        $route = new Route();
50
        $route->route_id = Common::getValue($params, 'route_id');
51
        $route->optimization_problem_id = Common::getValue($params, 'optimization_problem_id');
52
        $route->vehicle_alias = Common::getValue($params, 'vehicle_alias');
53
        $route->driver_alias = Common::getValue($params, 'driver_alias');
54
        $route->trip_distance = Common::getValue($params, 'trip_distance');
55
        $route->mpg = Common::getValue($params, 'mpg');
56
        $route->gas_price = Common::getValue($params, 'gas_price');
57
        $route->route_duration_sec = Common::getvalue($params, 'route_duration_sec');
58
        $route->is_unrouted = Common::getvalue($params, 'is_unrouted');
59
60
        // Make RouteParameters
61
        if (isset($params['parameters'])) {
62
            $route->parameters = RouteParameters::fromArray($params['parameters']);
63
        }
64
65 View Code Duplication
        if (isset($params['addresses'])) {
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...
66
            $addresses = array();
67
            foreach ($params['addresses'] as $address) {
68
                $addresses[] = Address::fromArray($address);
69
            }
70
71
            $route->addresses = $addresses;
72
        }
73
74
        $route->links = Common::getValue($params, 'links', array());
75
        $route->directions = Common::getValue($params, 'directions', array());
76
        $route->path = Common::getValue($params, 'path', array());
77
        $route->tracking_history = Common::getValue($params, 'tracking_history', array());
78
79
        return $route;
80
    }
81
82
    public static function getRoutes($routeId=null, $params=null)
83
    {
84
        $query = array(
85
            'api_key' => Route4Me::getApiKey()
86
        );
87
88
        if ($routeId) {
89
            $query['route_id'] = implode(',', (array) $routeId);
90
        }
91
92
        if ($params) {
93
            if (isset($params['directions'])) {
94
                $query['directions'] = $params['directions'];
95
            }
96
97
            if (isset($params['route_path_output'])) {
98
                $query['route_path_output'] = $params['route_path_output'];
99
            }
100
101
            if (isset($params['device_tracking_history'])) {
102
                $query['device_tracking_history'] = $params['device_tracking_history'];
103
            }
104
			
105
			if (isset($params['query'])) {
106
                $query['query'] = $params['query'];
107
            }
108
109
            $query['limit'] = isset($params['limit']) ? $params['limit'] : 30;
110
            $query['offset'] = isset($params['offset']) ? $params['offset'] : 0;
111
        }
112
113
        $json = Route4Me::makeRequst(array(
114
            'url'    => self::$apiUrl,
115
            'method' => 'GET',
116
            'query'  => $query
117
        ));
118
119
        if ($routeId) {
120
            return Route::fromArray($json); die("");
0 ignored issues
show
Unused Code introduced by
die(''); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
121
        } else {
122
            $routes = array();
123
            foreach($json as $route) {
124
                $routes[] = Route::fromArray($route);
125
            }
126
            return $routes;
127
        }
128
    }
129
130 View Code Duplication
	public function getRoutePoints($routeId, $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...
131
	{
132
		$result = Route4Me::makeRequst(array(
133
            'url'    => self::$apiUrl,
134
            'method' => 'GET',
135
            'query'  => array(
136
            	'api_key' => Route4Me::getApiKey(),
137
                'route_id' => $routeId,
138
                'route_path_output' => isset($params['route_path_output']) ? $params['route_path_output'] : null,
139
                'directions' => isset($params['directions']) ? $params['directions'] : null,
140
            )
141
        ));
142
143
		return $result;
144
	}
145
146
	public function duplicateRoute($route_id)
147
	{
148
		$result = Route4Me::makeRequst(array(
149
            'url'    => self::$apiUrlDuplicate,
150
            'method' => 'GET',
151
            'query'  => array(
152
            	'api_key' => Route4Me::getApiKey(),
153
                'route_id' => $route_id,
154
                'to' => 'none',
155
            )
156
        ));
157
		
158
		return $result;
159
	}
160
	
161 View Code Duplication
	public function resequenceRoute($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...
162
	{
163
		$result = Route4Me::makeRequst(array(
164
            'url'    => self::$apiUrl,
165
            'method' => 'PUT',
166
            'query'  => array(
167
            	'api_key' => Route4Me::getApiKey(),
168
                'route_id' => isset($params['route_id']) ? $params['route_id'] : null,
169
                'route_destination_id' => isset($params['route_destination_id']) ? $params['route_destination_id'] : null,
170
            ),
171
            'body'  => array(
172
				'addresses' => isset($params['addresses']) ? $params['addresses'] : null,
173
			)
174
        ));
175
		
176
		return $result;
177
	}
178
	
179 View Code Duplication
	public function resequenceAllAddresses($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...
180
	{
181
		$result = Route4Me::makeRequst(array(
182
            'url'    => self::$apiUrlReseq,
183
            'method' => 'GET',
184
            'query'  => array(
185
            	'api_key' => Route4Me::getApiKey(),
186
                'route_id' => isset($params['route_id']) ? $params['route_id'] : null,
187
                'disable_optimization' => isset($params['disable_optimization']) ? $params['disable_optimization'] : null,
188
                'optimize' => isset($params['optimize']) ? $params['optimize'] : null,
189
            )
190
        ));
191
		
192
		return $result;
193
		
194
	}
195
196
	public function mergeRoutes($params)
197
	{
198
		$result = Route4Me::makeRequst(array(
199
            'url'    => self::$apiUrlMerge,
200
            'method' => 'POST',
201
            'query'  => array(
202
            	'api_key' => Route4Me::getApiKey(),
203
            ),
204
            'body'  => array(
205
				'route_ids' => isset($params['route_ids']) ? $params['route_ids'] : null,
206
			),
207
			'HTTPHEADER'  => isset($this->httpheaders) ? $this->httpheaders : null,
208
        ));
209
		
210
		return $result;
211
	}
212
	
213 View Code Duplication
	public function shareRoute($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...
214
	{
215
		$result = Route4Me::makeRequst(array(
216
            'url'    => self::$apiUrlShare,
217
            'method' => 'POST',
218
            'query'  => array(
219
            	'api_key' => Route4Me::getApiKey(),
220
            	'route_id' => isset($params['route_id']) ? $params['route_id'] : null,
221
            ),
222
            'body'  => array(
223
				'recipient_email' => isset($params['recipient_email']) ? $params['recipient_email'] : null,
224
			),
225
			'Content-Type' => 'multipart/form-data'
226
        ));
227
		
228
		return $result;
229
	}
230
	
231
	// Getting random route_id from existing routes between $offset and $offset+$limit
232
	public function getRandomRouteId($offset,$limit)
0 ignored issues
show
Unused Code introduced by
The parameter $offset 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...
Unused Code introduced by
The parameter $limit 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...
233
	{
234
		$query['limit'] = isset($params['limit']) ? $params['limit'] : 30;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$query was never initialized. Although not strictly required by PHP, it is generally a good practice to add $query = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
Bug introduced by
The variable $params seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
235
        $query['offset'] = isset($params['offset']) ? $params['offset'] : 0;
236
			
237
		$json = Route4Me::makeRequst(array(
238
            'url'    => self::$apiUrl,
239
            'method' => 'GET',
240
            'query'  => $query
241
        ));
242
        
243
        if (sizeof($json)>0) {
244
            $routes = array();
245
            foreach($json as $route) {
246
                $routes[] = Route::fromArray($route);
247
            }
248
            
249
            $num=rand(0,sizeof($routes)-1);
250
            $rRoute=(array)$routes[$num];
251
            
252
            if (is_array($rRoute)) 
253
            {
254
                return $rRoute["route_id"];
255
            }
256
            else return null;
257
        } else return null;
258
		
259
		
260
	}
261
262 View Code Duplication
    public function update()
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...
263
    {
264
        $route = Route4Me::makeRequst(array(
265
            'url'    => self::$apiUrl,
266
            'method' => 'PUT',
267
            'query'  => array(
268
                'route_id' => isset($this->route_id) ? $this->route_id : null,
269
                'route_destination_id' => isset($this->route_destination_id) ? $this->route_destination_id : null,
270
            ),
271
            'body' => array (
272
            	'parameters' => $this->parameters,
273
            	),
274
            'HTTPHEADER'  => isset($this->httpheaders) ? $this->httpheaders : null,
275
        ));
276
277
        return Route::fromArray($route);
278
    }
279
	
280 View Code Duplication
	public function updateAddress()
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...
281
    {
282
        $result = Route4Me::makeRequst(array(
283
            'url'    => self::$apiUrlAddress,
284
            'method' => 'PUT',
285
            'query'  => array(
286
                'route_id' => isset($this->route_id) ? $this->route_id : null,
287
                'route_destination_id' => isset($this->route_destination_id) ? $this->route_destination_id : null,
288
            ),
289
            'body' => get_object_vars($this->parameters),
290
            'HTTPHEADER'  => isset($this->httpheaders) ? $this->httpheaders : null,
291
        ));
292
293
        return $result;
294
    }
295
296
    public function addAddresses(array $params)
297
    {
298
        $route = Route4Me::makeRequst(array(
299
            'url'    => self::$apiUrl,
300
            'method' => 'PUT',
301
            'query'  => array(
302
            	'api_key' => Route4Me::getApiKey(),
303
                'route_id' => isset($params['route_id']) ? $params['route_id'] : null,
304
                'addresses' => isset($params['addresses']) ? $params['addresses'] : null
305
            )
306
        ));
307
308
        return Route::fromArray($route);
309
    }
310
	
311 View Code Duplication
	public function insertAddressOptimalPosition(array $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...
312
	{
313
		$route = Route4Me::makeRequst(array(
314
            'url'    => self::$apiUrl,
315
            'method' => 'PUT',
316
            'query'  => array(
317
            	'api_key' => Route4Me::getApiKey(),
318
                'route_id' => isset($params['route_id']) ? $params['route_id'] : null,
319
            ),
320
            'body'  => array(
321
				'addresses' => isset($params['addresses']) ? $params['addresses'] : null,
322
				'optimal_position' => isset($params['optimal_position']) ? $params['optimal_position'] : null,
323
			)
324
        ));
325
326
        return Route::fromArray($route);
327
	}
328
	
329
	public function addNoteFile($params)
330
	{
331
		$fname = isset($params['strFilename']) ? $params['strFilename'] : null;
332
		$rpath = realpath($fname);
333
		//echo $rpath;die("");
334
		$result= Route4Me::makeRequst(array(
335
            'url'    => self::$apiUrlNoteFile,
336
            'method' => 'POST',
337
            'query'  => array(
338
            	'api_key' => Route4Me::getApiKey(),
339
                'route_id' => isset($params['route_id']) ? $params['route_id'] : null,
340
                'address_id' => isset($params['address_id']) ? $params['address_id'] : null,
341
                'dev_lat' => isset($params['dev_lat']) ? $params['dev_lat'] : null,
342
                'dev_lng' => isset($params['dev_lng']) ? $params['dev_lng'] : null,
343
                'device_type' => isset($params['device_type']) ? $params['device_type'] : null,
344
                'dev_lng' => isset($params['dev_lng']) ? $params['dev_lng'] : null,
345
            ),
346
            'body'  => array(
347
				'strUpdateType' => isset($params['strUpdateType']) ? $params['strUpdateType'] : null,
348
				'strFilename' => isset($params['strFilename']) ? $params['strFilename'] : null,
349
				'strNoteContents' => isset($params['strNoteContents']) ? $params['strNoteContents'] : null,
350
			),
351
			'FILE' => $rpath,
352
353
			'HTTPHEADER' => array(
354
				'Content-Type: application/x-www-form-urlencoded'
355
			)
356
        ));
357
358
        return $result;
359
	}
360
361 View Code Duplication
    public function delete($route_id)
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...
362
    {
363
        $result = Route4Me::makeRequst(array(
364
            'url'    => self::$apiUrl,
365
            'method' => 'DELETE',
366
            'query'  => array( 'route_id' => $route_id )
367
        ));
368
		
369
		// The code below doesn't work, altough this method is described as workable in REST API 
370
		/*
371
		$result = Route4Me::makeRequst(array(
372
            'url'    => self::$apiUrlDelete,
373
            'method' => 'GET',
374
            'query'  => array(
375
            	'api_key' => Route4Me::getApiKey(),
376
                'route_id' => $route_id,
377
            )
378
        ));
379
		*/
380
        return $result;
381
    }
382
	
383
	public function GetAddressesFromRoute($route_id)
384
	{
385
		$route1=Route::getRoutes($route_id,null);
386
		if (isset($route1)) {
387
			return $route1->addresses();
0 ignored issues
show
Bug introduced by
The method addresses() does not exist on Route4Me\Route. Did you maybe mean resequenceAllAddresses()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
388
		} else { return null;}
389
	}
390
	
391
	public function GetRandomAddressFromRoute($route_id)
392
	{
393
		$route1=Route::getRoutes($route_id,null);
394
		
395
		if (isset($route1)) {
396
			$addresses=$route1->addresses;
397
			
398
			$rnd=rand(0,sizeof($addresses)-1);
399
			
400
			return $addresses[$rnd];
401
			
402
		} else { return null;}
403
	}
404
405
    public function getRouteId()
406
    {
407
        return $this->route_id;
408
    }
409
410
    public function getOptimizationId()
411
    {
412
        return $this->optimization_problem_id;
413
    }
414
	
415
	public function GetLastLocation(array $params)
416
	{
417
		$route = Route4Me::makeRequst(array(
418
            'url'    => self::$apiUrl,
419
            'method' => 'GET',
420
            'query'  => array(
421
            	'api_key' => Route4Me::getApiKey(),
422
                'route_id' => isset($params['route_id']) ? $params['route_id'] : null,
423
                'device_tracking_history' => isset($params['device_tracking_history']) ? $params['device_tracking_history'] : null
424
            )
425
        ));
426
427
        return Route::fromArray($route);
428
		
429
	}
430
	
431 View Code Duplication
	public function GetTrackingHistoryFromTimeRange(array $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...
432
	{
433
		$route = Route4Me::makeRequst(array(
434
            'url'    => self::$apiUrlDeviceLocation,
435
            'method' => 'GET',
436
            'query'  => array(
437
            	'api_key' => Route4Me::getApiKey(),
438
                'route_id' => isset($params['route_id']) ? $params['route_id'] : null,
439
                'format' => isset($params['format']) ? $params['format'] : null,
440
                'time_period' => isset($params['time_period']) ? $params['time_period'] : null,
441
                'start_date' => isset($params['start_date']) ? $params['start_date'] : null,
442
                'end_date' => isset($params['end_date']) ? $params['end_date'] : null
443
                )
444
        ));
445
446
        return $route;
447
		
448
	}
449
	
450
	public function GetAssetTracking(array $params)
451
	{
452
		$route = Route4Me::makeRequst(array(
453
            'url'    => self::$apiUrlAsset,
454
            'method' => 'GET',
455
            'query'  => array(
456
            	'api_key' => Route4Me::getApiKey(),
457
                'tracking' => isset($params['tracking']) ? $params['tracking'] : null
458
                )
459
        ));
460
461
        return $route;
462
		
463
	}
464
}
465