Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
Remove SapiEmitter return
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark authored Feb 1, 2021
1 parent 4f4dc1a commit 4296175
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/SapiEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use function strtolower;

/**
* SapiEmitter sends a response using PHP Server API
* SapiEmitter sends a response using PHP Server API.
*/
final class SapiEmitter
{
Expand All @@ -34,7 +34,7 @@ final class SapiEmitter
public function __construct(int $bufferSize = null)
{
if ($bufferSize !== null && $bufferSize <= 0) {
throw new InvalidArgumentException('Buffer size must be greater than zero');
throw new InvalidArgumentException('Buffer size must be greater than zero.');
}
$this->bufferSize = $bufferSize ?? self::DEFAULT_BUFFER_SIZE;
}
Expand All @@ -46,10 +46,8 @@ public function __construct(int $bufferSize = null)
* @param bool $withoutBody If body should be ignored.
*
* @throws HeadersHaveBeenSentException
*
* @return bool
*/
public function emit(ResponseInterface $response, bool $withoutBody = false): bool
public function emit(ResponseInterface $response, bool $withoutBody = false): void
{
$status = $response->getStatusCode();
$withoutBody = $withoutBody || !$this->shouldOutputBody($response);
Expand All @@ -58,19 +56,21 @@ public function emit(ResponseInterface $response, bool $withoutBody = false): bo
$response = $response->withoutHeader('Content-Length');
}

// we can't send headers if they are already sent
// We can't send headers if they are already sent.
if (headers_sent()) {
throw new HeadersHaveBeenSentException();
}
header_remove();
// send HTTP Status-Line

// Send HTTP Status-Line.
header(sprintf(
'HTTP/%s %d %s',
$response->getProtocolVersion(),
$status,
$response->getReasonPhrase()
), true, $status);
// send headers

// Send headers.
foreach ($response->getHeaders() as $header => $values) {
$replaceFirst = strtolower($header) !== 'set-cookie';
foreach ($values as $value) {
Expand All @@ -89,8 +89,6 @@ public function emit(ResponseInterface $response, bool $withoutBody = false): bo

$this->emitBody($response);
}

return true;
}

private function emitBody(ResponseInterface $response): void
Expand All @@ -110,7 +108,7 @@ private function shouldOutputBody(ResponseInterface $response): bool
if (in_array($response->getStatusCode(), self::NO_BODY_RESPONSE_CODES, true)) {
return false;
}
// check if body is empty
// Check if body is empty.
$body = $response->getBody();
if (!$body->isReadable()) {
return false;
Expand Down

0 comments on commit 4296175

Please sign in to comment.