| Conditions | 6 |
| Paths | 210 |
| Total Lines | 74 |
| Code Lines | 55 |
| Lines | 5 |
| Ratio | 6.76 % |
| 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 |
||
| 41 | public static function post($url, $postData, $postType = 'form-urlencoded', $headers = [], $method = 'post') |
||
| 42 | { |
||
| 43 | if ($postType == 'form-urlencoded') { |
||
| 44 | $encodedVariables = array_map(['Ajde_Http_Curl', 'rawURLEncodeCallback'], $postData, array_keys($postData)); |
||
| 45 | |||
| 46 | $postContent = implode('&', $encodedVariables); |
||
| 47 | $postContentLen = strlen($postContent); |
||
| 48 | |||
| 49 | $headers = array_merge([ |
||
| 50 | 'Content-Type' => 'application/x-www-form-urlencoded', |
||
| 51 | 'Content-Length' => $postContentLen, |
||
| 52 | ], $headers); |
||
| 53 | } else { |
||
| 54 | if ($postType == 'json') { |
||
| 55 | $postContent = json_encode($postData); |
||
| 56 | $postContentLen = strlen($postContent); |
||
| 57 | |||
| 58 | $headers = array_merge([ |
||
| 59 | 'Content-Type' => 'application/json', |
||
| 60 | 'Content-Length' => $postContentLen, |
||
| 61 | ], $headers); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | $sendHeaders = []; |
||
| 66 | foreach ($headers as $k => $v) { |
||
| 67 | $sendHeaders[] = $k.': '.$v; |
||
| 68 | } |
||
| 69 | |||
| 70 | $output = false; |
||
| 71 | |||
| 72 | try { |
||
| 73 | $ch = curl_init(); |
||
| 74 | |||
| 75 | View Code Duplication | if ($method == 'post') { |
|
| 76 | curl_setopt($ch, CURLOPT_POST, 1); |
||
| 77 | } else { |
||
| 78 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method)); |
||
| 79 | } |
||
| 80 | curl_setopt($ch, CURLOPT_POSTFIELDS, $postContent); |
||
| 81 | curl_setopt($ch, CURLOPT_URL, |
||
| 82 | $url); // The URL to fetch. This can also be set when initializing a session with curl_init(). |
||
| 83 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, |
||
| 84 | true); // TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. |
||
| 85 | curl_setopt($ch, CURLOPT_HEADER, false); // TRUE to include the header in the output. |
||
| 86 | |||
| 87 | // Not possible in SAFE_MODE |
||
| 88 | //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set). |
||
| 89 | |||
| 90 | curl_setopt($ch, CURLOPT_MAXREDIRS, |
||
| 91 | 10); // The maximum amount of HTTP redirections to follow. Use this option alongside CURLOPT_FOLLOWLOCATION. |
||
| 92 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, |
||
| 93 | 5); // The number of seconds to wait while trying to connect. Use 0 to wait indefinitely. |
||
| 94 | curl_setopt($ch, CURLOPT_TIMEOUT, |
||
| 95 | 5); // The maximum number of seconds to allow cURL functions to execute. |
||
| 96 | curl_setopt($ch, CURLOPT_USERAGENT, |
||
| 97 | "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36')"); // The contents of the "User-Agent: " header to be used in a HTTP request. |
||
| 98 | curl_setopt($ch, CURLOPT_ENCODING, |
||
| 99 | ''); // The contents of the "Accept-Encoding: " header. This enables decoding of the response. Supported encodings are "identity", "deflate", and "gzip". If an empty string, "", is set, a header containing all supported encoding types is sent. |
||
| 100 | curl_setopt($ch, CURLOPT_AUTOREFERER, |
||
| 101 | true); // TRUE to automatically set the Referer: field in requests where it follows a Location: redirect. |
||
| 102 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, |
||
| 103 | false); // FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option. CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE if CURLOPT_SSL_VERIFYPEER is disabled (it defaults to 2). |
||
| 104 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, |
||
| 105 | false); // 1 to check the existence of a common name in the SSL peer certificate. 2 to check the existence of a common name and also verify that it matches the hostname provided. In production environments the value of this option should be kept at 2 (default value). |
||
| 106 | curl_setopt($ch, CURLOPT_HTTPHEADER, $sendHeaders); |
||
| 107 | $output = curl_exec($ch); |
||
| 108 | curl_close($ch); |
||
| 109 | } catch (Exception $e) { |
||
| 110 | throw $e; |
||
| 111 | } |
||
| 112 | |||
| 113 | return $output; |
||
| 114 | } |
||
| 115 | |||
| 280 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.