| Conditions | 8 |
| Paths | 48 |
| Total Lines | 72 |
| Code Lines | 42 |
| 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 |
||
| 179 | protected function retrieve($url) |
||
| 180 | { |
||
| 181 | $this->debugInit(); |
||
| 182 | |||
| 183 | |||
| 184 | $this->curl = curl_init(); |
||
| 185 | curl_setopt_array( |
||
| 186 | $this->curl, |
||
| 187 | [ |
||
| 188 | CURLOPT_RETURNTRANSFER => true, /* return instead of outputting */ |
||
| 189 | CURLOPT_URL => $url, |
||
| 190 | CURLOPT_HEADER => true, /* include the header in the output */ |
||
| 191 | CURLOPT_FOLLOWLOCATION => true, /* follow redirects */ |
||
| 192 | ] |
||
| 193 | ); |
||
| 194 | if ($this->skipSslVerification) { |
||
| 195 | // stop cURL from verifying the peer's certificate |
||
| 196 | curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false); |
||
| 197 | // don't check the existence of a common name in the SSL peer certificate |
||
| 198 | curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0); |
||
| 199 | } |
||
| 200 | if (!empty($this->requestHeaders)) { |
||
| 201 | curl_setopt( |
||
| 202 | $this->curl, |
||
| 203 | CURLOPT_HTTPHEADER, |
||
| 204 | $this->parseRequestHeaders($this->requestHeaders) |
||
| 205 | ); |
||
| 206 | } |
||
| 207 | |||
| 208 | $this->debugDo(); |
||
| 209 | |||
| 210 | switch ($this->method) { |
||
| 211 | case Http::METHOD_POST: |
||
| 212 | curl_setopt($this->curl, CURLOPT_POST, true); |
||
| 213 | if (!empty($this->postData)) { |
||
| 214 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->postData); |
||
| 215 | } |
||
| 216 | break; |
||
| 217 | case Http::METHOD_HEAD: |
||
| 218 | curl_setopt($this->curl, CURLOPT_NOBODY, true); |
||
| 219 | break; |
||
| 220 | } |
||
| 221 | |||
| 222 | $this->response = curl_exec($this->curl); |
||
| 223 | if (false === $this->response) { |
||
| 224 | throw new ApplicationException(curl_error($this->curl)); |
||
| 225 | } |
||
| 226 | |||
| 227 | $this->debugInfo = curl_getinfo($this->curl); |
||
| 228 | |||
| 229 | $httpCode = $this->getHttpCode(); |
||
| 230 | |||
| 231 | curl_close($this->curl); |
||
| 232 | |||
| 233 | /** |
||
| 234 | * For redirects, the response will contain evey header/body pair. |
||
| 235 | * The last header/body will be at the end of the response. |
||
| 236 | */ |
||
| 237 | $responseParts = explode("\r\n\r\n", $this->response); |
||
| 238 | $body = trim(array_pop($responseParts)); |
||
| 239 | |||
| 240 | $this->responseHeaders = []; |
||
| 241 | foreach ($responseParts as $item) { |
||
| 242 | $this->responseHeaders[] = $this->parseResponseHeaders($item); |
||
| 243 | } |
||
| 244 | |||
| 245 | $this->debugFinish(); |
||
| 246 | |||
| 247 | return new \WebServCo\Framework\HttpResponse( |
||
| 248 | $body, |
||
| 249 | $httpCode, |
||
| 250 | end($this->responseHeaders) |
||
| 251 | ); |
||
| 254 |