| Conditions | 8 |
| Paths | 48 |
| Total Lines | 71 |
| 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 |
||
| 153 | protected function retrieve($url) |
||
| 154 | { |
||
| 155 | $this->debugInit(); |
||
| 156 | $this->responseHeaders = []; |
||
| 157 | |||
| 158 | $this->curl = curl_init(); |
||
| 159 | curl_setopt_array( |
||
| 160 | $this->curl, |
||
| 161 | [ |
||
| 162 | CURLOPT_RETURNTRANSFER => true, /* return instead of outputting */ |
||
| 163 | CURLOPT_URL => $url, |
||
| 164 | CURLOPT_HEADER => true, /* include the header in the output */ |
||
| 165 | CURLOPT_FOLLOWLOCATION => true, /* follow redirects */ |
||
| 166 | ] |
||
| 167 | ); |
||
| 168 | if ($this->skipSslVerification) { |
||
| 169 | // stop cURL from verifying the peer's certificate |
||
| 170 | curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false); |
||
| 171 | // don't check the existence of a common name in the SSL peer certificate |
||
| 172 | curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0); |
||
| 173 | } |
||
| 174 | if (!empty($this->requestHeaders)) { |
||
| 175 | curl_setopt( |
||
| 176 | $this->curl, |
||
| 177 | CURLOPT_HTTPHEADER, |
||
| 178 | $this->parseRequestHeaders($this->requestHeaders) |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | |||
| 182 | $this->debugDo(); |
||
| 183 | |||
| 184 | switch ($this->method) { |
||
| 185 | case Http::METHOD_POST: |
||
| 186 | curl_setopt($this->curl, CURLOPT_POST, true); |
||
| 187 | if (!empty($this->postData)) { |
||
| 188 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->postData); |
||
| 189 | } |
||
| 190 | break; |
||
| 191 | case Http::METHOD_HEAD: |
||
| 192 | curl_setopt($this->curl, CURLOPT_NOBODY, true); |
||
| 193 | break; |
||
| 194 | } |
||
| 195 | |||
| 196 | $this->response = curl_exec($this->curl); |
||
| 197 | if (false === $this->response) { |
||
| 198 | throw new ApplicationException(curl_error($this->curl)); |
||
| 199 | } |
||
| 200 | |||
| 201 | $this->debugInfo = curl_getinfo($this->curl); |
||
| 202 | |||
| 203 | $httpCode = $this->getHttpCode(); |
||
| 204 | |||
| 205 | curl_close($this->curl); |
||
| 206 | |||
| 207 | /** |
||
| 208 | * For redirects, the response will contain evey header/body pair. |
||
| 209 | * The last header/body will be at the end of the response. |
||
| 210 | */ |
||
| 211 | $responseParts = explode("\r\n\r\n", $this->response); |
||
| 212 | $body = trim(array_pop($responseParts)); |
||
| 213 | |||
| 214 | foreach ($responseParts as $item) { |
||
| 215 | $this->responseHeaders[] = $this->parseResponseHeaders($item); |
||
| 216 | } |
||
| 217 | |||
| 218 | $this->debugFinish(); |
||
| 219 | |||
| 220 | return new \WebServCo\Framework\HttpResponse( |
||
| 221 | $body, |
||
| 222 | $httpCode, |
||
| 223 | end($this->responseHeaders) |
||
| 224 | ); |
||
| 227 |