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