| Conditions | 11 |
| Paths | 32 |
| Total Lines | 46 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 76 | protected function communicate($method, $url, $params = [], $headers = [], $userpwd = null) |
||
| 77 | { |
||
| 78 | $headers = $this->buildHeader($headers); |
||
| 79 | |||
| 80 | $curl = curl_init(); |
||
| 81 | |||
| 82 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); |
||
| 83 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
| 84 | curl_setopt($curl, CURLOPT_HEADER, true); |
||
| 85 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
||
| 86 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); |
||
| 87 | if ($method === 'GET') { |
||
| 88 | curl_setopt($curl, CURLOPT_URL, $url . (strpos($url, '?') === false ? '?' : '&') . http_build_query($params)); |
||
| 89 | } elseif ($method === 'POST' || $method === 'PUT' || $method === 'PATCH' || $method === 'DELETE') { |
||
| 90 | curl_setopt($curl, CURLOPT_URL, $url); |
||
| 91 | if (count(preg_grep("/^Content-Type: application\/json/i", $headers)) > 0) { |
||
| 92 | $data = json_encode($params); |
||
| 93 | } else { |
||
| 94 | $data = http_build_query($params); |
||
| 95 | } |
||
| 96 | curl_setopt($curl, CURLOPT_POSTFIELDS, $data); |
||
| 97 | } |
||
| 98 | if (!empty($headers)) { |
||
| 99 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
||
| 100 | } |
||
| 101 | if ($userpwd !== null) { |
||
| 102 | curl_setopt($curl, CURLOPT_USERPWD, $userpwd); |
||
| 103 | } |
||
| 104 | |||
| 105 | $response = curl_exec($curl); |
||
| 106 | $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
| 107 | $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
||
| 108 | $total_time = curl_getinfo($curl, CURLINFO_TOTAL_TIME); |
||
| 109 | |||
| 110 | curl_close($curl); |
||
| 111 | |||
| 112 | Log::debug(sprintf('[%s][%s][%ssec]', $url, $http_code, $total_time)); |
||
| 113 | if (!$response) { |
||
| 114 | Log::info('Acquisition failed'); |
||
| 115 | return false; |
||
| 116 | } |
||
| 117 | /** @phpstan-ignore-next-line */ |
||
| 118 | $header = substr($response, 0, $header_size); |
||
| 119 | /** @phpstan-ignore-next-line */ |
||
| 120 | $body = substr($response, $header_size); |
||
| 121 | return new HttpResponse($http_code, $header, $body); |
||
| 122 | } |
||
| 215 |