| Conditions | 10 |
| Paths | 36 |
| Total Lines | 51 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 31 |
| CRAP Score | 10.003 |
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 |
||
| 34 | 59 | protected function readResponse(RequestInterface $request, $socket) |
|
| 35 | { |
||
| 36 | 59 | $headers = []; |
|
| 37 | 59 | $reason = null; |
|
| 38 | |||
| 39 | 59 | while (($line = fgets($socket)) !== false) { |
|
| 40 | 58 | if (rtrim($line) === '') { |
|
| 41 | 58 | break; |
|
| 42 | } |
||
| 43 | 58 | $headers[] = trim($line); |
|
| 44 | 58 | } |
|
| 45 | |||
| 46 | 59 | $metadatas = stream_get_meta_data($socket); |
|
| 47 | |||
| 48 | 59 | if (array_key_exists('timed_out', $metadatas) && true === $metadatas['timed_out']) { |
|
| 49 | throw new NetworkException("Error while reading response, stream timed out", $request); |
||
| 50 | } |
||
| 51 | |||
| 52 | 59 | $parts = explode(' ', array_shift($headers), 3); |
|
| 53 | |||
| 54 | 59 | if (count($parts) <= 1) { |
|
| 55 | 1 | throw new NetworkException('Cannot read the response', $request); |
|
| 56 | } |
||
| 57 | |||
| 58 | 58 | $protocol = substr($parts[0], -3); |
|
| 59 | 58 | $status = $parts[1]; |
|
| 60 | |||
| 61 | 58 | if (isset($parts[2])) { |
|
| 62 | 58 | $reason = $parts[2]; |
|
| 63 | 58 | } |
|
| 64 | |||
| 65 | // Set the size on the stream if it was returned in the response |
||
| 66 | 58 | $responseHeaders = []; |
|
| 67 | |||
| 68 | 58 | foreach ($headers as $header) { |
|
| 69 | 58 | $headerParts = explode(':', $header, 2); |
|
| 70 | |||
| 71 | 58 | if (!array_key_exists(trim($headerParts[0]), $responseHeaders)) { |
|
| 72 | 58 | $responseHeaders[trim($headerParts[0])] = []; |
|
| 73 | 58 | } |
|
| 74 | |||
| 75 | 58 | $responseHeaders[trim($headerParts[0])][] = isset($headerParts[1]) |
|
| 76 | 58 | ? trim($headerParts[1]) |
|
| 77 | 58 | : ''; |
|
| 78 | 58 | } |
|
| 79 | |||
| 80 | 58 | $response = $this->messageFactory->createResponse($status, $reason, $responseHeaders, null, $protocol); |
|
| 81 | 58 | $stream = $this->createStream($socket, $response); |
|
| 82 | |||
| 83 | 58 | return $response->withBody($stream); |
|
| 84 | } |
||
| 85 | |||
| 105 |