| Conditions | 9 |
| Paths | 48 |
| Total Lines | 56 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 45 | public static function sendRequest($url, $params = [], $method = 'POST', $options = []) |
||
| 46 | { |
||
| 47 | $method = strtoupper($method); |
||
| 48 | $protocol = substr($url, 0, 5); |
||
| 49 | $query_string = is_array($params) ? http_build_query($params) : $params; |
||
| 50 | |||
| 51 | $ch = curl_init(); |
||
| 52 | $defaults = []; |
||
| 53 | if ('GET' == $method) { |
||
| 54 | $geturl = $query_string ? $url . (stripos($url, "?") !== false ? "&" : "?") . $query_string : $url; |
||
| 55 | $defaults[CURLOPT_URL] = $geturl; |
||
| 56 | } else { |
||
| 57 | $defaults[CURLOPT_URL] = $url; |
||
| 58 | if ($method == 'POST') { |
||
| 59 | $defaults[CURLOPT_POST] = 1; |
||
| 60 | } else { |
||
| 61 | $defaults[CURLOPT_CUSTOMREQUEST] = $method; |
||
| 62 | } |
||
| 63 | $defaults[CURLOPT_POSTFIELDS] = $params; |
||
| 64 | } |
||
| 65 | |||
| 66 | $defaults[CURLOPT_HEADER] = false; |
||
| 67 | $defaults[CURLOPT_USERAGENT] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36"; |
||
| 68 | $defaults[CURLOPT_FOLLOWLOCATION] = true; |
||
| 69 | $defaults[CURLOPT_RETURNTRANSFER] = true; |
||
| 70 | $defaults[CURLOPT_CONNECTTIMEOUT] = 3; |
||
| 71 | $defaults[CURLOPT_TIMEOUT] = 3; |
||
| 72 | |||
| 73 | // disable 100-continue |
||
| 74 | curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); |
||
| 75 | |||
| 76 | if ('https' == $protocol) { |
||
| 77 | $defaults[CURLOPT_SSL_VERIFYPEER] = false; |
||
| 78 | $defaults[CURLOPT_SSL_VERIFYHOST] = false; |
||
| 79 | } |
||
| 80 | |||
| 81 | curl_setopt_array($ch, (array)$options + $defaults); |
||
| 82 | $ret = curl_exec($ch); |
||
| 83 | $err = curl_error($ch); |
||
| 84 | if (false === $ret || !empty($err)) { |
||
| 85 | $errno = curl_errno($ch); |
||
| 86 | $info = curl_getinfo($ch); |
||
| 87 | curl_close($ch); |
||
| 88 | return [ |
||
| 89 | 'ret' => false, |
||
| 90 | 'errno' => $errno, |
||
| 91 | 'msg' => $err, |
||
| 92 | 'info' => $info, |
||
| 93 | ]; |
||
| 94 | } |
||
| 95 | |||
| 96 | curl_close($ch); |
||
| 97 | |||
| 98 | return [ |
||
| 99 | 'ret' => true, |
||
| 100 | 'msg' => $ret, |
||
| 101 | ]; |
||
| 186 |