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.
Test Failed
Push — master ( 8e4cc8...6e1047 )
by Oleg
33s
created

Route4Me::fileUploadRequest()   B

Complexity

Conditions 8
Paths 34

Size

Total Lines 59
Code Lines 37

Duplication

Lines 7
Ratio 11.86 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 37
c 1
b 0
f 0
nc 34
nop 1
dl 7
loc 59
rs 7.132

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace Route4Me;
4
5
use Route4Me\Exception\ApiError;
6
7
class Route4Me
8
{
9
    static public $apiKey;
10
    static public $baseUrl = 'https://route4me.com';
11
12
    public static function setApiKey($apiKey)
13
    {
14
        self::$apiKey = $apiKey;
15
    }
16
17
    public static function getApiKey()
18
    {
19
        return self::$apiKey;
20
    }
21
22
    public static function setBaseUrl($baseUrl)
23
    {
24
        self::$baseUrl = $baseUrl;
25
    }
26
27
    public static function getBaseUrl()
28
    {
29
        return self::$baseUrl;
30
    }
31
	
32
	public static function fileUploadRequest($options) {
33
		$query = isset($options['query']) ?
34
            array_filter($options['query']) : array();
35
36
		if (sizeof($query)==0) return null;
37
38
		$body = isset($options['body']) ?
39
            array_filter($options['body']) : null;
40
			
41
		$fname = isset($body['strFilename']) ? $body['strFilename'] : '';
42
		if ($fname=='') return null;
43
44
		$rpath = realpath($fname);
0 ignored issues
show
Unused Code introduced by
$rpath 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...
45
		
46
		$fp=fopen(realpath($fname),"r");
47
		
48
		$url = self::$baseUrl.$options['url'] . '?' . http_build_query(array_merge(
49
            array( 'api_key' => self::getApiKey()), $query)
50
        );
51
		
52
		//self::simplePrint($body);die("");
0 ignored issues
show
Unused Code Comprehensibility introduced by
92% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
53
		//echo "url=".$url."<br>";die("");
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
54
		
55
		$ch = curl_init($url);
56
		
57
		$curlOpts = array(
0 ignored issues
show
Unused Code introduced by
$curlOpts 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...
58
            CURLOPT_RETURNTRANSFER => true,
59
            CURLOPT_TIMEOUT        => 60,
60
            CURLOPT_FOLLOWLOCATION => true,
61
            CURLOPT_SSL_VERIFYHOST => FALSE,
62
            CURLOPT_SSL_VERIFYPEER => FALSE
63
        );
64
		
65
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
66
		
67
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
68
			"Content-Type: multipart/form-data",
69
			'Content-Disposition: form-data; name="strFilename"',
70
			'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'
71
		));
72
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
73
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); 
74
		
75
		$result = curl_exec($ch);
76
		fclose($fp);
77
		//var_dump($result); die('');
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
78
		$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
79
        curl_close($ch);
80
		//echo "code = $code <br>";
81
		$json = json_decode($result, true);
82
		//var_dump($json); die("");
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83 View Code Duplication
        if (200 == $code) {
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...
84
            return $json;
85
        } elseif (isset($json['errors'])) {
86
            throw new ApiError(implode(', ', $json['errors']));
87
        } else {
88
            throw new ApiError('Something wrong');
89
        }
90
	}
91
92
    public static function makeRequst($options) {
93
        $method = isset($options['method']) ? $options['method'] : 'GET';
94
        $query = isset($options['query']) ?
95
            array_filter($options['query']) : array();
96
		//echo "query=".$query['member_id'];die("");
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
97
        $body = isset($options['body']) ?
98
            array_filter($options['body']) : null;
99
		$file = isset($options['FILE']) ? $options['FILE'] : null;
100
        $headers = array(
101
            "User-Agent: Route4Me php-sdk"
102
        );
103
        
104
        if (isset($options['HTTPHEADER'])) {
105
            $headers[]=$options['HTTPHEADER'];
106
        }
107
108
		if (isset($options['HTTPHEADERS'])) {
109
		    foreach ($options['HTTPHEADERS'] As $header) $headers[]=$header;
110
        }
111
        //self::simplePrint($headers); die("");
0 ignored issues
show
Unused Code Comprehensibility introduced by
85% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
112
        $ch = curl_init();
113
        $url = $options['url'] . '?' . http_build_query(array_merge(
114
            $query, array( 'api_key' => self::getApiKey())
115
        ));
116
		
117
		//$jfile=json_encode($body); echo $jfile; die("");
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
118
119
        //self::simplePrint($headers); die("");
0 ignored issues
show
Unused Code Comprehensibility introduced by
85% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
120
		$baseUrl=self::getBaseUrl();
121
		
122
		if (strpos($url,'move_route_destination')>0) $baseUrl='https://www.route4me.com';
123
        $curlOpts = arraY(
124
            CURLOPT_URL            => $baseUrl. $url,
125
            CURLOPT_RETURNTRANSFER => true,
126
            CURLOPT_TIMEOUT        => 60,
127
            CURLOPT_FOLLOWLOCATION => true,
128
            CURLOPT_SSL_VERIFYHOST => FALSE,
129
            CURLOPT_SSL_VERIFYPEER => FALSE,
130
            CURLOPT_HTTPHEADER     => $headers
131
        );
132
		
133
		//echo "url=".$baseUrl.$url."<br>";die("");
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
134
        curl_setopt_array($ch, $curlOpts);
135
		
136
		if ($file !=null) {
137
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
138
			$fp=fopen($file,'r');
139
			curl_setopt($ch, CURLOPT_INFILE , $fp);
140
			curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
141
		}
142
		
143 View Code Duplication
        switch($method) {
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...
144
        case 'DELETE':
145
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
146
147
			if (isset($body)) {
148
				curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); 
149
			}
150
            break;
151
		case 'DELETEARRAY':
152
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
153
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query));
154
            break;
155
        case 'PUT':
156
			//$jfile=json_encode($body); echo $jfile; die("");
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
157
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
158
			if (isset($query)) {
159
				curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query));
160
			}
161
162
			if (isset($body)) {
163
				curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); 
164
			}
165
			break;
166
        case 'POST':
167
			if (isset($query)) {
168
            	curl_setopt($ch,  CURLOPT_POSTFIELDS, json_encode($query)); 
169
			}
170
            
171
			//echo json_encode($body); die("");
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
172
			if (isset($body)) {
173
				curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); 
174
			} 
175
			break;
176
		case 'ADD':
177
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); break;
0 ignored issues
show
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
178
        }
179
180
        $result = curl_exec($ch);
181
		//var_dump($result); die("");
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
182
		$isxml=FALSE;
183
		$jxml="";
184 View Code Duplication
		if (strpos($result, '<?xml')>-1)
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...
185
		{
186
			$xml = simplexml_load_string($result);
187
			$jxml = json_encode($xml);
188
			$isxml = TRUE;
189
		}
190
		
191
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
192
        curl_close($ch);
193
		
194
		if ($isxml) {
195
			$json = $jxml;
196
		} else $json = json_decode($result, true);
197
		//var_dump($json); die("");
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
198 View Code Duplication
        if (200 == $code) {
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...
199
            return $json;
200
        } elseif (isset($json['errors'])) {
201
            throw new ApiError(implode(', ', $json['errors']));
202
        } else {
203
            throw new ApiError('Something wrong');
204
        }
205
    }
206
207
	public static function makeUrlRequst($url, $options) {
208
		$method = isset($options['method']) ? $options['method'] : 'GET';
209
        $query = isset($options['query']) ?
210
            array_filter($options['query']) : array();
211
        $body = isset($options['body']) ?
212
            array_filter($options['body']) : null;
213
        $ch = curl_init();
214
		
215
		$curlOpts = arraY(
216
            CURLOPT_URL            => $url,
217
            CURLOPT_RETURNTRANSFER => true,
218
            CURLOPT_TIMEOUT        => 60,
219
            CURLOPT_FOLLOWLOCATION => true,
220
            CURLOPT_SSL_VERIFYHOST => FALSE,
221
            CURLOPT_SSL_VERIFYPEER => FALSE,
222
            CURLOPT_HTTPHEADER     => array(
223
                'User-Agent' => 'Route4Me php-sdk'
224
            )
225
        );
226
		
227
		curl_setopt_array($ch, $curlOpts);
228
		
229 View Code Duplication
        switch($method) {
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...
230
        case 'DELETE':
231
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
232
233
			if (isset($body)) {
234
				curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); 
235
			}
236
            break;
237
		case 'DELETEARRAY':
238
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
239
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query));
240
            break;
241
        case 'PUT':
242
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
243
			if (isset($query)) {
244
				curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query));
245
			}
246
247
			if (isset($body)) {
248
				curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); 
249
			}
250
			break;
251
        case 'POST':
252
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
253
			if (isset($query)) {
254
            	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); 
255
			}
256
257
			if (isset($body)) {
258
				curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); 
259
			} 
260
			break;
261
		case 'ADD':
262
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); break;
0 ignored issues
show
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
263
        }
264
265
		$result = curl_exec($ch);
266
        
267
		$isxml=FALSE;
268
		$jxml="";
269 View Code Duplication
		if (strpos($result, '<?xml')>-1)
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...
270
		{
271
			$xml = simplexml_load_string($result);
272
			$jxml = json_encode($xml);
273
			$isxml = TRUE;
274
		}
275
		
276
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
277
        curl_close($ch);
278
		
279
		if ($isxml) {
280
			$json = $jxml;
281
		} else $json = json_decode($result, true);
282
		
283
		
284 View Code Duplication
        if (200 == $code) {
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...
285
            return $json;
286
        } elseif (isset($json['errors'])) {
287
            throw new ApiError(implode(', ', $json['errors']));
288
        } else {
289
            throw new ApiError('Something wrong');
290
        }
291
	}
292
	
293
	/**
294
	 * Prints on the screen main keys and values of the array 
295
	 *
296
	 */
297
	public static function simplePrint($results)
298
	{
299
		if (isset($results)) {
300
			if (is_array($results)) {
301
				foreach ($results as $key=>$result) {
302
					if (is_array($result)) {
303
						foreach ($result as $key1=>$result1) {
304
							if (is_array($result1)) {
305
								echo $key1." --> "."Array() <br>";
306
								/**
307
								 * for deep printing here should be recursive call:
308
								 * Route4Me::simplePrint($result1); 
309
								 */
310
							} else {
311
								if (is_object($result1)) {
312
									echo $key." --> "."Object <br>";
313
									/**
314
									 * for deep printing here should be recursive call:
315
									 * $oarray=(array)$result1;
316
									 * Route4Me::simplePrint($oarray);
317
									 */
318
								} else {
319
									echo $key1." --> ".$result1."<br>";	
320
								}
321
								
322
							}
323
						}
324
					} else {
325
						if (is_object($result)) {
326
							echo $key." --> "."Object <br>";
327
							/**
328
							 * for deep printing here should be recursive call:
329
							 * $oarray=(array)$result;
330
							 * Route4Me::simplePrint($oarray);
331
							 */
332
						} else {
333
							echo $key." --> ".$result."<br>";
334
						}
335
						
336
					}
337
					echo "<br>";
338
				}
339
			} 
340
		}
341
	}
342
343
}
344