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