| Conditions | 7 |
| Paths | 24 |
| Total Lines | 64 |
| Code Lines | 38 |
| 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 |
||
| 147 | protected function retrieve($url) |
||
| 148 | { |
||
| 149 | $this->debugInit(); |
||
| 150 | |||
| 151 | $this->curl = curl_init(); |
||
| 152 | curl_setopt_array( |
||
| 153 | $this->curl, |
||
| 154 | [ |
||
| 155 | CURLOPT_RETURNTRANSFER => true, /* return instead of outputting */ |
||
| 156 | CURLOPT_URL => $url, |
||
| 157 | CURLOPT_HEADER => true, /* include the header in the output */ |
||
| 158 | CURLOPT_FOLLOWLOCATION => true /* follow redirects */ |
||
| 159 | ] |
||
| 160 | ); |
||
| 161 | if (!empty($this->requestHeaders)) { |
||
| 162 | curl_setopt( |
||
| 163 | $this->curl, |
||
| 164 | CURLOPT_HTTPHEADER, |
||
| 165 | $this->parseRequestHeaders($this->requestHeaders) |
||
| 166 | ); |
||
| 167 | } |
||
| 168 | |||
| 169 | $this->debugDo(); |
||
| 170 | |||
| 171 | switch ($this->method) { |
||
| 172 | case Http::METHOD_POST: |
||
| 173 | curl_setopt($this->curl, CURLOPT_POST, true); |
||
| 174 | if (!empty($this->postData)) { |
||
| 175 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->postData); |
||
| 176 | } |
||
| 177 | break; |
||
| 178 | case Http::METHOD_HEAD: |
||
| 179 | curl_setopt($this->curl, CURLOPT_NOBODY, true); |
||
| 180 | break; |
||
| 181 | } |
||
| 182 | |||
| 183 | $this->response = curl_exec($this->curl); |
||
| 184 | if (false === $this->response) { |
||
| 185 | throw new ApplicationException(curl_error($this->curl)); |
||
| 186 | } |
||
| 187 | |||
| 188 | $this->debugInfo = curl_getinfo($this->curl); |
||
| 189 | |||
| 190 | $httpCode = $this->getHttpCode(); |
||
| 191 | |||
| 192 | curl_close($this->curl); |
||
| 193 | |||
| 194 | /** |
||
| 195 | * For redirects, the response will contain evey header/body pair. |
||
| 196 | * The last header/body will be at the end of the response. |
||
| 197 | */ |
||
| 198 | $responseParts = explode("\r\n\r\n", $this->response); |
||
| 199 | $body = trim(array_pop($responseParts)); |
||
| 200 | |||
| 201 | foreach ($responseParts as $item) { |
||
| 202 | $this->responseHeaders[] = $this->parseResponseHeaders($item); |
||
| 203 | } |
||
| 204 | |||
| 205 | $this->debugFinish(); |
||
| 206 | |||
| 207 | return new \WebServCo\Framework\HttpResponse( |
||
| 208 | $body, |
||
| 209 | $httpCode, |
||
| 210 | end($this->responseHeaders) |
||
| 211 | ); |
||
| 214 |