| Conditions | 11 |
| Paths | 100 |
| Total Lines | 44 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 37 | public function emit(ResponseInterface $response, bool $withoutBody = false): bool |
||
| 38 | { |
||
| 39 | $status = $response->getStatusCode(); |
||
| 40 | $withoutBody = $withoutBody || !$this->shouldOutputBody($response); |
||
| 41 | $withoutContentLength = $withoutBody || $response->hasHeader('Transfer-Encoding'); |
||
| 42 | |||
| 43 | // we can't send headers if they are already sent |
||
| 44 | if (headers_sent()) { |
||
| 45 | throw new HeadersHaveBeenSentException(); |
||
| 46 | } |
||
| 47 | header_remove(); |
||
| 48 | // send HTTP Status-Line |
||
| 49 | header(sprintf( |
||
| 50 | 'HTTP/%s %d %s', |
||
| 51 | $response->getProtocolVersion(), |
||
| 52 | $status, |
||
| 53 | $response->getReasonPhrase() |
||
| 54 | ), true, $status); |
||
| 55 | // filter headers |
||
| 56 | $headers = $withoutContentLength |
||
| 57 | ? $response->withoutHeader('Content-Length') |
||
| 58 | ->getHeaders() |
||
| 59 | : $response->getHeaders(); |
||
| 60 | // send headers |
||
| 61 | foreach ($headers as $header => $values) { |
||
| 62 | $replaceFirst = strtolower($header) !== 'set-cookie'; |
||
| 63 | foreach ($values as $value) { |
||
| 64 | header(sprintf('%s: %s', $header, $value), $replaceFirst); |
||
| 65 | $replaceFirst = false; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | if (!$withoutBody) { |
||
| 70 | if (!$withoutContentLength && !$response->hasHeader('Content-Length')) { |
||
| 71 | $contentLength = $response->getBody()->getSize(); |
||
| 72 | if ($contentLength !== null) { |
||
| 73 | header('Content-Length: ' . $contentLength, true); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | $this->emitBody($response); |
||
| 78 | } |
||
| 79 | |||
| 80 | return true; |
||
| 81 | } |
||
| 116 |