| Conditions | 5 |
| Paths | 12 |
| Total Lines | 59 |
| Code Lines | 34 |
| 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 |
||
| 130 | protected function retrieve($url) |
||
| 131 | { |
||
| 132 | $this->debugInit(); |
||
| 133 | |||
| 134 | $this->curl = curl_init(); |
||
| 135 | curl_setopt_array( |
||
| 136 | $this->curl, |
||
| 137 | [ |
||
| 138 | CURLOPT_RETURNTRANSFER => true, /* return instead of outputting */ |
||
| 139 | CURLOPT_URL => $url, |
||
| 140 | CURLOPT_HEADER => true, /* include the header in the output */ |
||
| 141 | CURLOPT_FOLLOWLOCATION => true /* follow redirects */ |
||
| 142 | ] |
||
| 143 | ); |
||
| 144 | if (!empty($this->requestHeaders)) { |
||
| 145 | curl_setopt( |
||
| 146 | $this->curl, |
||
| 147 | CURLOPT_HTTPHEADER, |
||
| 148 | $this->parseRequestHeaders($this->requestHeaders) |
||
| 149 | ); |
||
| 150 | } |
||
| 151 | |||
| 152 | $this->debugDo(); |
||
| 153 | |||
| 154 | if ($this->method == Http::METHOD_POST) { |
||
| 155 | curl_setopt($this->curl, CURLOPT_POST, true); |
||
| 156 | if (!empty($this->postData)) { |
||
| 157 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->postData); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | $this->response = curl_exec($this->curl); |
||
| 162 | if (false === $this->response) { |
||
| 163 | throw new ApplicationException(curl_error($this->curl)); |
||
| 164 | } |
||
| 165 | |||
| 166 | $this->debugInfo = curl_getinfo($this->curl); |
||
| 167 | |||
| 168 | $httpCode = $this->getHttpCode(); |
||
| 169 | |||
| 170 | curl_close($this->curl); |
||
| 171 | |||
| 172 | /** |
||
| 173 | * For redirects, the response will contain evey header/body pair. |
||
| 174 | * The last header/body will be at the end of the response. |
||
| 175 | */ |
||
| 176 | $responseParts = explode("\r\n\r\n", $this->response); |
||
| 177 | $body = array_pop($responseParts); |
||
| 178 | $headerString = array_pop($responseParts); |
||
| 179 | |||
| 180 | $body = trim($body); |
||
| 181 | $headers = $this->parseResponseHeaders($headerString); |
||
| 182 | |||
| 183 | $this->debugFinish(); |
||
| 184 | |||
| 185 | return new \WebServCo\Framework\HttpResponse( |
||
| 186 | $body, |
||
| 187 | $httpCode, |
||
| 188 | $headers |
||
| 189 | ); |
||
| 192 |