Conditions | 12 |
Paths | 32 |
Total Lines | 53 |
Code Lines | 43 |
Lines | 20 |
Ratio | 37.74 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
17 | public function request(HTTP\Request $request){ |
||
18 | $http_method = strtoupper($method); |
||
|
|||
19 | $ch = curl_init($url); |
||
20 | $opt = [ |
||
21 | CURLOPT_CUSTOMREQUEST => $http_method, |
||
22 | CURLOPT_SSL_VERIFYHOST => false, |
||
23 | CURLOPT_CONNECTTIMEOUT => 10, |
||
24 | CURLOPT_RETURNTRANSFER => true, |
||
25 | CURLOPT_USERAGENT => static::$UA, |
||
26 | CURLOPT_HEADER => false, |
||
27 | CURLOPT_MAXREDIRS => 10, |
||
28 | CURLOPT_FOLLOWLOCATION => true, |
||
29 | CURLOPT_ENCODING => '', |
||
30 | ]; |
||
31 | |||
32 | if($username && $password) { |
||
33 | $opt[CURLOPT_USERPWD] = "$username:$password"; |
||
34 | } |
||
35 | |||
36 | $headers = array_merge($headers,static::$headers); |
||
37 | |||
38 | View Code Duplication | if($http_method == 'GET'){ |
|
39 | if($data && is_array($data)){ |
||
40 | $tmp = []; |
||
41 | $queried_url = $url; |
||
42 | foreach($data as $key=>$val) $tmp[] = $key.'='.$val; |
||
43 | $queried_url .= (strpos($queried_url,'?') === false) ? '?' : '&'; |
||
44 | $queried_url .= implode('&',$tmp); |
||
45 | $opt[CURLOPT_URL] = $queried_url; |
||
46 | $opt[CURLOPT_HTTPGET] = true; |
||
47 | unset($opt[CURLOPT_CUSTOMREQUEST]); |
||
48 | } |
||
49 | } else { |
||
50 | $opt[CURLOPT_CUSTOMREQUEST] = $http_method; |
||
51 | if($data_as_json or is_object($data)){ |
||
52 | $headers['Content-Type'] = 'application/json'; |
||
53 | $opt[CURLOPT_POSTFIELDS] = json_encode($data); |
||
54 | } else { |
||
55 | $opt[CURLOPT_POSTFIELDS] = http_build_query($data); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | curl_setopt_array($ch,$opt); |
||
60 | $_harr = []; |
||
61 | foreach($headers as $key=>$val) $_harr[] = $key.': '.$val; |
||
62 | curl_setopt($ch, CURLOPT_HTTPHEADER, $_harr); |
||
63 | $result = curl_exec($ch); |
||
64 | $contentType = strtolower(curl_getinfo($ch, CURLINFO_CONTENT_TYPE)); |
||
65 | static::$last_info = curl_getinfo($ch); |
||
66 | if(false !== strpos($contentType,'json')) $result = json_decode($result); |
||
67 | curl_close($ch); |
||
68 | return $result; |
||
69 | } |
||
70 | |||
76 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.