Conditions | 13 |
Paths | 64 |
Total Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
24 | protected static function request($method, $url, $data=[], array $headers=[], $data_as_json=false, $username=null, $password = null){ |
||
25 | preg_match('/.+(?::([0-9]+))\/?/i', $url, $match); |
||
26 | $port = count($match) > 1 ? $match[1] : 80; |
||
27 | $http_method = strtoupper($method); |
||
28 | $ch = curl_init($url); |
||
29 | $opt = [ |
||
30 | CURLOPT_CUSTOMREQUEST => $http_method, |
||
31 | CURLOPT_SSL_VERIFYHOST => false, |
||
32 | CURLOPT_CONNECTTIMEOUT => 10, |
||
33 | CURLOPT_RETURNTRANSFER => true, |
||
34 | CURLOPT_USERAGENT => static::$UA, |
||
35 | CURLOPT_HEADER => true, |
||
36 | CURLOPT_VERBOSE => true, |
||
37 | CURLOPT_MAXREDIRS => 10, |
||
38 | CURLOPT_FOLLOWLOCATION => true, |
||
39 | CURLOPT_ENCODING => '', |
||
40 | CURLOPT_PROXY => static::$proxy, |
||
41 | CURLOPT_PORT => $port |
||
42 | ]; |
||
43 | |||
44 | if($username && $password) { |
||
45 | $opt[CURLOPT_USERPWD] = "$username:$password"; |
||
46 | } |
||
47 | |||
48 | $headers = array_merge($headers,static::$headers); |
||
49 | |||
50 | if($http_method == 'GET'){ |
||
51 | if($data && is_array($data)){ |
||
52 | $tmp = []; |
||
53 | $queried_url = $url; |
||
54 | foreach($data as $key=>$val) $tmp[] = $key.'='.$val; |
||
55 | $queried_url .= (strpos($queried_url,'?') === false) ? '?' : '&'; |
||
56 | $queried_url .= implode('&',$tmp); |
||
57 | $opt[CURLOPT_URL] = $queried_url; |
||
58 | $opt[CURLOPT_HTTPGET] = true; |
||
59 | unset($opt[CURLOPT_CUSTOMREQUEST]); |
||
60 | } |
||
61 | } else { |
||
62 | $opt[CURLOPT_CUSTOMREQUEST] = $http_method; |
||
63 | if($data_as_json or is_object($data)){ |
||
64 | $headers['Content-Type'] = 'application/json'; |
||
65 | $opt[CURLOPT_POSTFIELDS] = json_encode($data); |
||
66 | } else { |
||
67 | $opt[CURLOPT_POSTFIELDS] = http_build_query($data); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | curl_setopt_array($ch,$opt); |
||
72 | $_harr = []; |
||
73 | foreach($headers as $key=>$val) $_harr[] = $key.': '.$val; |
||
74 | curl_setopt($ch, CURLOPT_HTTPHEADER, $_harr); |
||
75 | $result = curl_exec($ch); |
||
76 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
||
77 | $contentType = strtolower(curl_getinfo($ch, CURLINFO_CONTENT_TYPE)); |
||
78 | static::$last_response_header = substr($result, 0, $header_size); |
||
79 | $result = substr($result, $header_size); |
||
80 | static::$last_info = curl_getinfo($ch); |
||
81 | if(false !== strpos($contentType,'json')) $result = json_decode($result); |
||
82 | curl_close($ch); |
||
83 | static::trigger("request", $result, static::$last_info); |
||
84 | static::$last_response_body = $result; |
||
85 | return $result; |
||
86 | } |
||
87 | |||
218 |
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:
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 thebar
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.