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