Conditions | 10 |
Paths | 65 |
Total Lines | 82 |
Code Lines | 49 |
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 |
||
195 | protected function retrieve($url) |
||
196 | { |
||
197 | $this->debugInit(); |
||
198 | |||
199 | |||
200 | $this->curl = curl_init(); |
||
201 | if (!is_resource($this->curl)) { |
||
202 | throw new ApplicationException('Not a valid resource'); |
||
203 | } |
||
204 | curl_setopt_array( |
||
205 | $this->curl, |
||
206 | [ |
||
207 | CURLOPT_RETURNTRANSFER => true, /* return instead of outputting */ |
||
208 | CURLOPT_URL => $url, |
||
209 | CURLOPT_HEADER => true, /* include the header in the output */ |
||
210 | CURLOPT_FOLLOWLOCATION => true, /* follow redirects */ |
||
211 | CURLOPT_CONNECTTIMEOUT => 60, // The number of seconds to wait while trying to connect. |
||
212 | CURLOPT_TIMEOUT => 60, // The maximum number of seconds to allow cURL functions to execute. |
||
213 | ] |
||
214 | ); |
||
215 | if ($this->skipSslVerification) { |
||
216 | // stop cURL from verifying the peer's certificate |
||
217 | curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false); |
||
218 | // don't check the existence of a common name in the SSL peer certificate |
||
219 | curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0); |
||
220 | } |
||
221 | if (!empty($this->requestHeaders)) { |
||
222 | curl_setopt( |
||
223 | $this->curl, |
||
224 | CURLOPT_HTTPHEADER, |
||
225 | $this->parseRequestHeaders($this->requestHeaders) |
||
226 | ); |
||
227 | } |
||
228 | |||
229 | $this->debugDo(); |
||
230 | |||
231 | switch ($this->method) { |
||
232 | case Http::METHOD_POST: |
||
233 | curl_setopt($this->curl, CURLOPT_POST, true); |
||
234 | if (!empty($this->postData)) { |
||
235 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->postData); |
||
236 | } |
||
237 | break; |
||
238 | case Http::METHOD_HEAD: |
||
239 | curl_setopt($this->curl, CURLOPT_NOBODY, true); |
||
240 | break; |
||
241 | } |
||
242 | |||
243 | $this->response = curl_exec($this->curl); |
||
244 | $this->curlError = curl_error($this->curl); |
||
245 | if (false === $this->response) { |
||
246 | throw new ApplicationException(sprintf("cURL error: %s", $this->curlError)); |
||
247 | } |
||
248 | |||
249 | $this->debugInfo = curl_getinfo($this->curl); |
||
250 | |||
251 | curl_close($this->curl); |
||
252 | |||
253 | $httpCode = $this->getHttpCode(); |
||
254 | |||
255 | if (empty($httpCode)) { |
||
256 | throw new ApplicationException(sprintf("Empty HTTP status code. cURL error: %s", $this->curlError)); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * For redirects, the response will contain evey header/body pair. |
||
261 | * The last header/body will be at the end of the response. |
||
262 | */ |
||
263 | $responseParts = explode("\r\n\r\n", (string) $this->response); |
||
264 | $body = trim((string) array_pop($responseParts)); |
||
265 | |||
266 | $this->responseHeaders = []; |
||
267 | foreach ($responseParts as $item) { |
||
268 | $this->responseHeaders[] = $this->parseResponseHeaders($item); |
||
269 | } |
||
270 | |||
271 | $this->debugFinish(); |
||
272 | |||
273 | return new \WebServCo\Framework\HttpResponse( |
||
274 | $body, |
||
275 | $httpCode, |
||
276 | end($this->responseHeaders) |
||
277 | ); |
||
280 |