Conditions | 23 |
Paths | 158 |
Total Lines | 86 |
Code Lines | 58 |
Lines | 0 |
Ratio | 0 % |
Tests | 70 |
CRAP Score | 23 |
Changes | 4 | ||
Bugs | 0 | Features | 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 |
||
218 | 12 | private function ___curlCall($location) |
|
219 | { |
||
220 | 12 | curl_setopt($this->curl, CURLOPT_URL, $location); |
|
221 | |||
222 | 12 | if (!empty($this->_cookies)) { |
|
223 | 1 | $cookies = array(); |
|
224 | 1 | foreach ($this->_cookies as $cookie_name => $cookie_value) { |
|
225 | 1 | $cookies[] = $cookie_name . '=' . $cookie_value[0]; |
|
226 | 1 | } |
|
227 | 1 | curl_setopt($this->curl, CURLOPT_COOKIE, implode('; ', $cookies)); |
|
228 | 1 | } |
|
229 | |||
230 | 12 | $response = curl_exec($this->curl); |
|
231 | 12 | if ($response === false) { |
|
232 | 2 | throw new \SoapFault( |
|
233 | 2 | 'HTTP', |
|
234 | 2 | 'Error Fetching http, ' . curl_error($this->curl) . ' (' . curl_errno($this->curl) . ')' |
|
235 | 2 | ); |
|
236 | } |
||
237 | |||
238 | 10 | $header_size = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE); |
|
239 | 10 | $response_header = substr($response, 0, $header_size); |
|
240 | 10 | $response_body = substr($response, $header_size); |
|
241 | 10 | $http_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
|
242 | |||
243 | 10 | if (isset($this->trace) && $this->trace) { |
|
244 | 3 | $this->__last_request_headers = curl_getinfo($this->curl, CURLINFO_HEADER_OUT); |
|
1 ignored issue
–
show
|
|||
245 | 3 | $this->__last_response_headers = $response_header; |
|
1 ignored issue
–
show
|
|||
246 | 3 | } |
|
247 | |||
248 | 10 | if ($http_code >= 300 && $http_code < 400) { |
|
249 | 4 | $tmp = stristr($response_header, 'Location:'); |
|
250 | 4 | $line_end = strpos($tmp, "\n"); // "\r" will be trimmed |
|
251 | 4 | if ($line_end === false) { |
|
252 | 1 | throw new \SoapFault('HTTP', 'Error Redirecting, No Location'); |
|
253 | } |
||
254 | 3 | $new_location = trim(substr($tmp, 9, $line_end - 9)); |
|
255 | 3 | $url = parse_url($new_location); |
|
256 | 3 | if ($url === false || |
|
257 | 3 | empty($url['scheme']) || |
|
258 | 2 | preg_match('/^https?$/i', $url['scheme']) !== 1 |
|
259 | 3 | ) { |
|
260 | 1 | throw new \SoapFault('HTTP', 'Error Redirecting, Invalid Location'); |
|
261 | } |
||
262 | 2 | if (++$this->redirect_count > $this->redirect_max) { |
|
263 | 1 | throw new \SoapFault('HTTP', 'Redirection limit reached, aborting'); |
|
264 | } |
||
265 | 2 | return $this->___curlCall($new_location); |
|
266 | } |
||
267 | |||
268 | 7 | if ($http_code >= 400) { |
|
269 | 3 | $is_error = false; |
|
270 | 3 | $response_length = strlen($response_body); |
|
271 | 3 | if ($response_length === 0) { |
|
272 | 1 | $is_error = true; |
|
273 | 3 | } elseif ($response_length > 0) { |
|
274 | 2 | $is_xml = false; |
|
275 | 2 | $content_type = curl_getinfo($this->curl, CURLINFO_CONTENT_TYPE); |
|
276 | 2 | if ($content_type !== null) { |
|
277 | 2 | $separator_position = strpos($content_type, ';'); |
|
278 | 2 | if ($separator_position !== false) { |
|
279 | 1 | $content_type = substr($content_type, 0, $separator_position); |
|
280 | 1 | } |
|
281 | 2 | if ($content_type === 'text/xml' || $content_type === 'application/soap+xml') { |
|
282 | 1 | $is_xml = true; |
|
283 | 1 | } |
|
284 | 2 | } |
|
285 | 2 | if (!$is_xml) { |
|
286 | 1 | $str = ltrim($response_body); |
|
287 | 1 | if (strncmp($str, '<?xml', 5)) { |
|
288 | 1 | $is_error = true; |
|
289 | 1 | } |
|
290 | 1 | } |
|
291 | 2 | } |
|
292 | |||
293 | 3 | if ($is_error) { |
|
294 | 2 | $string_http_code = (string)$http_code; |
|
295 | 2 | $code_position = strpos($response_header, $string_http_code); |
|
296 | 2 | $tmp = substr($response_header, $code_position + strlen($string_http_code)); |
|
297 | 2 | $http_message = trim(strstr($tmp, "\n", true)); |
|
298 | 2 | throw new \SoapFault('HTTP', $http_message); |
|
299 | } |
||
300 | 1 | } |
|
301 | |||
302 | 5 | return $response_body; |
|
303 | } |
||
304 | } |
||
305 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: