| Conditions | 3 |
| Paths | 6 |
| Total Lines | 58 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 30 | public function sendRequest(RequestInterface $request):ResponseInterface{ |
||
| 31 | $uri = $request->getUri(); |
||
| 32 | $method = $request->getMethod(); |
||
| 33 | $headers = $this->getRequestHeaders($request); |
||
| 34 | |||
| 35 | $body = in_array($method, ['DELETE', 'PATCH', 'POST', 'PUT'], true) |
||
| 36 | ? $request->getBody()->getContents() |
||
| 37 | : null; |
||
| 38 | |||
| 39 | $context = stream_context_create([ |
||
| 40 | 'http' => [ |
||
| 41 | 'method' => $method, |
||
| 42 | 'header' => $headers, |
||
| 43 | 'content' => $body, |
||
| 44 | 'protocol_version' => '1.1', |
||
| 45 | 'user_agent' => $this->options->user_agent, |
||
| 46 | 'max_redirects' => 0, |
||
| 47 | 'timeout' => 5, |
||
| 48 | ], |
||
| 49 | 'ssl' => [ |
||
| 50 | 'cafile' => $this->options->ca_info, |
||
| 51 | 'verify_peer' => $this->options->ssl_verifypeer, |
||
| 52 | 'verify_depth' => 3, |
||
| 53 | 'peer_name' => $uri->getHost(), |
||
| 54 | 'ciphers' => 'HIGH:!SSLv2:!SSLv3', |
||
| 55 | 'disable_compression' => true, |
||
| 56 | ], |
||
| 57 | ]); |
||
| 58 | |||
| 59 | $requestUri = (string)$uri->withFragment(''); |
||
| 60 | |||
| 61 | /** @phan-suppress-next-line PhanTypeMismatchArgumentInternal */ |
||
| 62 | set_error_handler(function(int $severity, string $msg, string $file, int $line):void{ |
||
| 63 | throw new ErrorException($msg, 0, $severity, $file, $line); |
||
| 64 | }); |
||
| 65 | |||
| 66 | try{ |
||
| 67 | $responseBody = file_get_contents($requestUri, false, $context); |
||
| 68 | /** @phan-suppress-next-line PhanTypeMismatchArgumentInternal https://github.com/phan/phan/issues/3273 */ |
||
| 69 | $responseHeaders = $this->parseResponseHeaders(get_headers($requestUri, 1, $context)); |
||
| 70 | } |
||
| 71 | catch(Exception $e){ |
||
| 72 | $this->logger->error('StreamClient error #'.$e->getCode().': '.$e->getMessage()); |
||
| 73 | |||
| 74 | throw new ClientException($e->getMessage(), $e->getCode()); |
||
| 75 | } |
||
| 76 | |||
| 77 | restore_error_handler(); |
||
| 78 | |||
| 79 | $response = $this->responseFactory |
||
| 80 | ->createResponse($responseHeaders['statuscode'], $responseHeaders['statustext']) |
||
| 81 | ->withProtocolVersion($responseHeaders['httpversion']) |
||
| 82 | ; |
||
| 83 | |||
| 84 | $response->getBody()->write($responseBody); |
||
| 85 | $response->getBody()->rewind(); |
||
| 86 | |||
| 87 | return $response; |
||
| 88 | } |
||
| 136 |