| Conditions | 12 |
| Paths | 32 |
| Total Lines | 60 |
| Code Lines | 50 |
| 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 | $http_method = strtoupper($method); |
||
| 26 | $ch = curl_init($url); |
||
| 27 | $opt = [ |
||
| 28 | CURLOPT_CUSTOMREQUEST => $http_method, |
||
| 29 | CURLOPT_SSL_VERIFYHOST => false, |
||
| 30 | CURLOPT_CONNECTTIMEOUT => 10, |
||
| 31 | CURLOPT_RETURNTRANSFER => true, |
||
| 32 | CURLOPT_USERAGENT => static::$UA, |
||
| 33 | CURLOPT_HEADER => true, |
||
| 34 | CURLOPT_VERBOSE => true, |
||
| 35 | CURLOPT_MAXREDIRS => 10, |
||
| 36 | CURLOPT_FOLLOWLOCATION => true, |
||
| 37 | CURLOPT_ENCODING => '', |
||
| 38 | CURLOPT_PROXY => static::$proxy, |
||
| 39 | ]; |
||
| 40 | |||
| 41 | if($username && $password) { |
||
| 42 | $opt[CURLOPT_USERPWD] = "$username:$password"; |
||
| 43 | } |
||
| 44 | |||
| 45 | $headers = array_merge($headers,static::$headers); |
||
| 46 | |||
| 47 | if($http_method == 'GET'){ |
||
| 48 | if($data && is_array($data)){ |
||
| 49 | $tmp = []; |
||
| 50 | $queried_url = $url; |
||
| 51 | foreach($data as $key=>$val) $tmp[] = $key.'='.$val; |
||
| 52 | $queried_url .= (strpos($queried_url,'?') === false) ? '?' : '&'; |
||
| 53 | $queried_url .= implode('&',$tmp); |
||
| 54 | $opt[CURLOPT_URL] = $queried_url; |
||
| 55 | $opt[CURLOPT_HTTPGET] = true; |
||
| 56 | unset($opt[CURLOPT_CUSTOMREQUEST]); |
||
| 57 | } |
||
| 58 | } else { |
||
| 59 | $opt[CURLOPT_CUSTOMREQUEST] = $http_method; |
||
| 60 | if($data_as_json or is_object($data)){ |
||
| 61 | $headers['Content-Type'] = 'application/json'; |
||
| 62 | $opt[CURLOPT_POSTFIELDS] = json_encode($data); |
||
| 63 | } else { |
||
| 64 | $opt[CURLOPT_POSTFIELDS] = http_build_query($data); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | curl_setopt_array($ch,$opt); |
||
| 69 | $_harr = []; |
||
| 70 | foreach($headers as $key=>$val) $_harr[] = $key.': '.$val; |
||
| 71 | curl_setopt($ch, CURLOPT_HTTPHEADER, $_harr); |
||
| 72 | $result = curl_exec($ch); |
||
| 73 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
||
| 74 | $contentType = strtolower(curl_getinfo($ch, CURLINFO_CONTENT_TYPE)); |
||
| 75 | static::$last_response_header = substr($result, 0, $header_size); |
||
| 76 | $result = substr($result, $header_size); |
||
| 77 | static::$last_info = curl_getinfo($ch); |
||
| 78 | if(false !== strpos($contentType,'json')) $result = json_decode($result); |
||
| 79 | curl_close($ch); |
||
| 80 | static::trigger("request", $result, static::$last_info); |
||
| 81 | static::$last_response_body = $result; |
||
| 82 | return $result; |
||
| 83 | } |
||
| 84 | |||
| 219 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.