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

Route4Me::simplePrint()   C

Complexity

Conditions 14
Paths 10

Size

Total Lines 50

Duplication

Lines 29
Ratio 58 %

Importance

Changes 0
Metric Value
cc 14
nc 10
nop 2
dl 29
loc 50
rs 6.2666
c 0
b 0
f 0

How to fix   Complexity   

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
namespace Route4Me;
3
4
use Route4Me\Exception\ApiError;
5
use Route4Me\Exception\myErrorHandler;
6
use Route4Me\Enum\Endpoint;
7
8
class Route4Me
9
{
10
    static public $apiKey;
11
    static public $baseUrl = Endpoint::BASE_URL;
12
13
    public static function setApiKey($apiKey)
14
    {
15
        self::$apiKey = $apiKey;
16
    }
17
18
    public static function getApiKey()
19
    {
20
        return self::$apiKey;
21
    }
22
23
    public static function setBaseUrl($baseUrl)
24
    {
25
        self::$baseUrl = $baseUrl;
26
    }
27
28
    public static function getBaseUrl()
29
    {
30
        return self::$baseUrl;
31
    }
32
    
33
    public static function fileUploadRequest($options) {
34
        $query = isset($options['query']) ?
35
            array_filter($options['query']) : array();
36
37
        if (sizeof($query)==0) return null;
38
39
        $body = isset($options['body']) ?
40
            array_filter($options['body']) : null;
41
            
42
        $fname = isset($body['strFilename']) ? $body['strFilename'] : '';
43
        if ($fname=='') return null;
44
45
        $rpath = function_exists('curl_file_create') ? curl_file_create(realpath($fname)) : '@'.realpath($fname);
46
        
47
        $url = self::$baseUrl.$options['url'] . '?' . http_build_query(array_merge(
48
            array( 'api_key' => self::getApiKey()), $query)
49
        );
50
        
51
        $ch = curl_init($url);
52
        
53
        $curlOpts = array(
54
            CURLOPT_POST => true,
55
            CURLOPT_RETURNTRANSFER => false,
56
            CURLOPT_TIMEOUT        => 60,
57
            CURLOPT_FOLLOWLOCATION => true,
58
            CURLOPT_SSL_VERIFYHOST => FALSE,
59
            CURLOPT_SSL_VERIFYPEER => FALSE
60
        );
61
        
62
        curl_setopt_array($ch, $curlOpts);
63
        
64
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
65
        
66
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
67
            "Content-Type: multipart/form-data",
68
            'Content-Disposition: form-data; name="strFilename"'
69
        ));
70
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
71
        curl_setopt($ch, CURLOPT_POSTFIELDS, array('strFilename' => $rpath)); 
72
        
73
        $result = curl_exec($ch);
74
75
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
76
        curl_close($ch);
77
78
        $json = json_decode($result, true);
79
        
80 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...
81
            return $json;
82
        } elseif (isset($json['errors'])) {
83
            throw new ApiError(implode(', ', $json['errors']));
84
        } else {
85
            throw new ApiError('Something wrong');
86
        }
87
    }
88
89
    public static function makeRequst($options) {
90
        $errorHandler = new myErrorHandler();
91
        
92
        $old_error_handler = set_error_handler(array( $errorHandler, "proc_error"));
0 ignored issues
show
Unused Code introduced by
$old_error_handler 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...
93
        $method = isset($options['method']) ? $options['method'] : 'GET';
94
        $query = isset($options['query']) ?
95
            array_filter($options['query'], function($x) { return !is_null($x); } ) : array();
96
97
        $body = isset($options['body']) ? $options['body'] : null;
98
        $file = isset($options['FILE']) ? $options['FILE'] : null;
99
        $headers = array(
100
            "User-Agent: Route4Me php-sdk"
101
        );
102
        
103
        if (isset($options['HTTPHEADER'])) {
104
            $headers[]=$options['HTTPHEADER'];
105
        }
106
         
107
        if (isset($options['HTTPHEADERS'])) {
108
            foreach ($options['HTTPHEADERS'] As $header) $headers[]=$header;
109
        }
110
111
        $ch = curl_init();
112
        $url = $options['url'] . '?' . http_build_query(array_merge(
113
            $query, array( 'api_key' => self::getApiKey())
114
        ));
115
116
        $baseUrl=self::getBaseUrl();
117
118
        $curlOpts = arraY(
119
            CURLOPT_URL            => $baseUrl. $url,
120
            CURLOPT_RETURNTRANSFER => true,
121
            CURLOPT_TIMEOUT        => 80,
122
            CURLOPT_FOLLOWLOCATION => true,
123
            CURLOPT_SSL_VERIFYHOST => FALSE,
124
            CURLOPT_SSL_VERIFYPEER => FALSE,
125
            CURLOPT_HTTPHEADER     => $headers
126
        );
127
        
128
        curl_setopt_array($ch, $curlOpts);
129
        
130
        if ($file !=null) {
131
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
132
            $fp=fopen($file,'r');
133
            curl_setopt($ch, CURLOPT_INFILE , $fp);
134
            curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
135
        }
136
137
        switch($method) {
138 View Code Duplication
        case 'DELETE':
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...
139
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
140
141
            if (isset($body)) {
142
                
143
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); 
144
            }
145
            break;
146 View Code Duplication
        case 'DELETEARRAY':
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...
147
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
148
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query));
149
            break;
150 View Code Duplication
        case 'PUT':
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...
151
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
152
153
            if (isset($body)) {
154
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); 
155
            }
156
            break;
157
        case 'POST':
158
           if (isset($body)) {
159
                $bodyData = json_encode($body);
160
               if (isset($options['HTTPHEADER'])) {
161
                  if (strpos($options['HTTPHEADER'], "multipart/form-data")>0) $bodyData = $body;
162
               }
163
               
164
               curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); 
165
            } 
166
            break;
167
        case 'ADD':
168
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); break;
169
        }
170
171
        $result = curl_exec($ch);
172
        
173
        $isxml=FALSE;
174
        $jxml="";
175 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...
176
        {
177
            $xml = simplexml_load_string($result);
178
            //$jxml = json_encode($xml);
179
            $jxml=self::object2array($xml);
180
            $isxml = TRUE;
181
        }
182
        
183
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
184
        curl_close($ch);
185
        
186
        if (200 == $code) {
187
            if ($isxml) {
188
                $json = $jxml;
189
            } else $json = json_decode($result, true);
190
            
191
            if (isset($json['errors'])) {
192
                throw new ApiError(implode(', ', $json['errors']));
193
            } else {
194
                return $json;
195
            }
196
        }  elseif (409 == $code) {
197
            throw new ApiError('Wrong API key');
198
        } else {
199
            throw new ApiError('Something wrong');
200
        }
201
    }
202
203
    public static function object2array($object)
204
    {
205
        return @json_decode(@json_encode($object),1);
206
    }
207
208
    public static function makeUrlRequst($url, $options) {
209
        $method = isset($options['method']) ? $options['method'] : 'GET';
210
        $query = isset($options['query']) ?
211
            array_filter($options['query'], function($x) { return !is_null($x); } ) : array();
212
        $body = isset($options['body']) ? $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
        switch($method) {
230 View Code Duplication
        case 'DELETE':
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...
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 View Code Duplication
        case 'DELETEARRAY':
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...
238
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
239
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query));
240
            break;
241 View Code Duplication
        case 'PUT':
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...
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 View Code Duplication
        case 'POST':
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...
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;
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 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...
284
            return $json;
285
        } elseif (isset($json['errors'])) {
286
            throw new ApiError(implode(', ', $json['errors']));
287
        } else {
288
            throw new ApiError('Something wrong');
289
        }
290
    }
291
    
292
    /**
293
     * Prints on the screen main keys and values of the array 
294
     *
295
     */
296
    public static function simplePrint($results, $deepPrinting=null)
297
    {
298
        if (isset($results)) {
299
            if (is_array($results)) {
300
                foreach ($results as $key=>$result) {
301
                    if (is_array($result)) {
302
                        foreach ($result as $key1=>$result1) {
303
                            if (is_array($result1)) {
304
                                  if ($deepPrinting) {
305
                                      echo "<br>$key1 ------><br>";
306
                                      Route4Me::simplePrint($result1,true);
307
                                      echo "------<br>";
308
                                  } else {
309
                                      echo $key1." --> "."Array() <br>";
310
                                  } 
311 View Code Duplication
                            } else {
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...
312
                                if (is_object($result1)) {
313
                                    if ($deepPrinting) {
314
                                        echo "<br>$key1 ------><br>";
315
                                        $oarray=(array)$result1;
316
                                        Route4Me::simplePrint($oarray,true);
317
                                        echo "------<br>";
318
                                    } else {
319
                                        echo $key1." --> "."Object <br>";
320
                                    } 
321
                                } else {
322
                                    if (!is_null($result1)) echo $key1." --> ".$result1."<br>";    
323
                                }
324
                            }
325
                        }
326 View Code Duplication
                    } else {
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...
327
                        if (is_object($result)) {
328
                            if ($deepPrinting) {
329
                                echo "<br>$key ------><br>";
330
                                $oarray=(array)$result;
331
                                Route4Me::simplePrint($oarray,true);
332
                                echo "------<br>";
333
                            } else {
334
                                echo $key." --> "."Object <br>";
335
                            } 
336
                        } else {
337
                            if (!is_null($result)) echo $key." --> ".$result."<br>";
338
                        }
339
                        
340
                    }
341
                    //echo "<br>";
342
                }
343
            } 
344
        }
345
    }
346
347
}
348