| 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 |
||
| 231 | 12 | private function ___curlCall($location) |
|
| 232 | { |
||
| 233 | 12 | curl_setopt($this->curl, CURLOPT_URL, $location); |
|
| 234 | |||
| 235 | 12 | if (!empty($this->_cookies)) { |
|
| 236 | 1 | $cookies = array(); |
|
| 237 | 1 | foreach ($this->_cookies as $cookie_name => $cookie_value) { |
|
| 238 | 1 | $cookies[] = $cookie_name . '=' . $cookie_value[0]; |
|
| 239 | 1 | } |
|
| 240 | 1 | curl_setopt($this->curl, CURLOPT_COOKIE, implode('; ', $cookies)); |
|
| 241 | 1 | } |
|
| 242 | |||
| 243 | 12 | $response = curl_exec($this->curl); |
|
| 244 | 12 | if ($response === false) { |
|
| 245 | 2 | throw new \SoapFault( |
|
| 246 | 2 | 'HTTP', |
|
| 247 | 2 | 'Error Fetching http, ' . curl_error($this->curl) . ' (' . curl_errno($this->curl) . ')' |
|
| 248 | 2 | ); |
|
| 249 | } |
||
| 250 | |||
| 251 | 10 | $header_size = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE); |
|
| 252 | 10 | $response_header = substr($response, 0, $header_size); |
|
| 253 | 10 | $response_body = substr($response, $header_size); |
|
| 254 | 10 | $http_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
|
| 255 | |||
| 256 | 10 | if (isset($this->trace) && $this->trace) { |
|
| 257 | 3 | $this->__last_request_headers = curl_getinfo($this->curl, CURLINFO_HEADER_OUT); |
|
|
1 ignored issue
–
show
|
|||
| 258 | 3 | $this->__last_response_headers = $response_header; |
|
|
1 ignored issue
–
show
|
|||
| 259 | 3 | } |
|
| 260 | |||
| 261 | 10 | if ($http_code >= 300 && $http_code < 400) { |
|
| 262 | 4 | $tmp = stristr($response_header, 'Location:'); |
|
| 263 | 4 | $line_end = strpos($tmp, "\n"); // "\r" will be trimmed |
|
| 264 | 4 | if ($line_end === false) { |
|
| 265 | 1 | throw new \SoapFault('HTTP', 'Error Redirecting, No Location'); |
|
| 266 | } |
||
| 267 | 3 | $new_location = trim(substr($tmp, 9, $line_end - 9)); |
|
| 268 | 3 | $url = parse_url($new_location); |
|
| 269 | 3 | if ($url === false || |
|
| 270 | 3 | empty($url['scheme']) || |
|
| 271 | 2 | preg_match('/^https?$/i', $url['scheme']) !== 1 |
|
| 272 | 3 | ) { |
|
| 273 | 1 | throw new \SoapFault('HTTP', 'Error Redirecting, Invalid Location'); |
|
| 274 | } |
||
| 275 | 2 | if (++$this->redirect_count > $this->redirect_max) { |
|
| 276 | 1 | throw new \SoapFault('HTTP', 'Redirection limit reached, aborting'); |
|
| 277 | } |
||
| 278 | 2 | return $this->___curlCall($new_location); |
|
| 279 | } |
||
| 280 | |||
| 281 | 7 | if ($http_code >= 400) { |
|
| 282 | 3 | $is_error = false; |
|
| 283 | 3 | $response_length = strlen($response_body); |
|
| 284 | 3 | if ($response_length === 0) { |
|
| 285 | 1 | $is_error = true; |
|
| 286 | 3 | } elseif ($response_length > 0) { |
|
| 287 | 2 | $is_xml = false; |
|
| 288 | 2 | $content_type = curl_getinfo($this->curl, CURLINFO_CONTENT_TYPE); |
|
| 289 | 2 | if ($content_type !== null) { |
|
| 290 | 2 | $separator_position = strpos($content_type, ';'); |
|
| 291 | 2 | if ($separator_position !== false) { |
|
| 292 | 1 | $content_type = substr($content_type, 0, $separator_position); |
|
| 293 | 1 | } |
|
| 294 | 2 | if ($content_type === 'text/xml' || $content_type === 'application/soap+xml') { |
|
| 295 | 1 | $is_xml = true; |
|
| 296 | 1 | } |
|
| 297 | 2 | } |
|
| 298 | 2 | if (!$is_xml) { |
|
| 299 | 1 | $str = ltrim($response_body); |
|
| 300 | 1 | if (strncmp($str, '<?xml', 5)) { |
|
| 301 | 1 | $is_error = true; |
|
| 302 | 1 | } |
|
| 303 | 1 | } |
|
| 304 | 2 | } |
|
| 305 | |||
| 306 | 3 | if ($is_error) { |
|
| 307 | 2 | $string_http_code = (string)$http_code; |
|
| 308 | 2 | $code_position = strpos($response_header, $string_http_code); |
|
| 309 | 2 | $tmp = substr($response_header, $code_position + strlen($string_http_code)); |
|
| 310 | 2 | $http_message = trim(strstr($tmp, "\n", true)); |
|
| 311 | 2 | throw new \SoapFault('HTTP', $http_message); |
|
| 312 | } |
||
| 313 | 1 | } |
|
| 314 | |||
| 315 | 5 | return $response_body; |
|
| 316 | } |
||
| 317 | } |
||
| 318 |
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: