| Conditions | 11 |
| Paths | 25 |
| Total Lines | 51 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 27 | public function retrieveResponse( |
||
| 28 | UriInterface $endpoint, |
||
| 29 | $requestBody, |
||
| 30 | array $extraHeaders = array(), |
||
| 31 | $method = 'POST' |
||
| 32 | ) { |
||
| 33 | // Normalize method name |
||
| 34 | $method = strtoupper($method); |
||
| 35 | |||
| 36 | $this->normalizeHeaders($extraHeaders); |
||
| 37 | |||
| 38 | if ($method === 'GET' && !empty($requestBody)) { |
||
| 39 | throw new \InvalidArgumentException('No body expected for "GET" request.'); |
||
| 40 | } |
||
| 41 | |||
| 42 | if (!isset($extraHeaders['Content-Type']) && $method === 'POST' && is_array($requestBody)) { |
||
| 43 | $extraHeaders['Content-Type'] = 'Content-Type: application/x-www-form-urlencoded'; |
||
| 44 | } |
||
| 45 | |||
| 46 | $host = 'Host: '.$endpoint->getHost(); |
||
| 47 | // Append port to Host if it has been specified |
||
| 48 | if ($endpoint->hasExplicitPortSpecified()) { |
||
| 49 | $host .= ':'.$endpoint->getPort(); |
||
| 50 | } |
||
| 51 | |||
| 52 | $extraHeaders['Host'] = $host; |
||
| 53 | $extraHeaders['Connection'] = 'Connection: close'; |
||
| 54 | |||
| 55 | if (is_array($requestBody)) { |
||
| 56 | $requestBody = http_build_query($requestBody, '', '&'); |
||
| 57 | } |
||
| 58 | $extraHeaders['Content-length'] = 'Content-length: '.strlen($requestBody); |
||
| 59 | |||
| 60 | $context = $this->generateStreamContext($requestBody, $extraHeaders, $method); |
||
| 61 | |||
| 62 | $level = error_reporting(0); |
||
| 63 | $response = file_get_contents($endpoint->getAbsoluteUri(), false, $context); |
||
| 64 | error_reporting($level); |
||
| 65 | if (false === $response) { |
||
| 66 | $lastError = error_get_last(); |
||
| 67 | if (is_null($lastError)) { |
||
| 68 | throw new TokenResponseException( |
||
| 69 | 'Failed to request resource. HTTP Code: ' . |
||
| 70 | ((isset($http_response_header[0]))?$http_response_header[0]:'No response') |
||
| 71 | ); |
||
| 72 | } |
||
| 73 | throw new TokenResponseException($lastError['message']); |
||
| 74 | } |
||
| 75 | |||
| 76 | return $response; |
||
| 77 | } |
||
| 78 | |||
| 96 |