From 4832d802b2723f01bd3a9e9f2c0cd49dc2121d9b Mon Sep 17 00:00:00 2001 From: Demin Yin Date: Fri, 16 Feb 2024 12:59:55 -0800 Subject: [PATCH] improved type declarations on FastCGI classes --- src/core/Coroutine/FastCGI/Client.php | 3 +++ src/core/FastCGI/HttpResponse.php | 35 +++++++++++++++++++++------ src/core/FastCGI/Response.php | 16 ++++++++---- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/core/Coroutine/FastCGI/Client.php b/src/core/Coroutine/FastCGI/Client.php index 330af1e..df9f693 100644 --- a/src/core/Coroutine/FastCGI/Client.php +++ b/src/core/Coroutine/FastCGI/Client.php @@ -17,6 +17,8 @@ use Swoole\FastCGI\HttpRequest; use Swoole\FastCGI\HttpResponse; use Swoole\FastCGI\Record\EndRequest; +use Swoole\FastCGI\Record\Stderr; +use Swoole\FastCGI\Record\Stdout; use Swoole\FastCGI\Request; use Swoole\FastCGI\Response; @@ -91,6 +93,7 @@ public function execute(Request $request, float $timeout = -1): Response $this->socket->close(); $this->socket = null; } + /* @var array $records */ return match (true) { $request instanceof HttpRequest => new HttpResponse($records), default => new Response($records), diff --git a/src/core/FastCGI/HttpResponse.php b/src/core/FastCGI/HttpResponse.php index 8765c3b..089946f 100644 --- a/src/core/FastCGI/HttpResponse.php +++ b/src/core/FastCGI/HttpResponse.php @@ -11,6 +11,9 @@ namespace Swoole\FastCGI; +use Swoole\FastCGI\Record\EndRequest; +use Swoole\FastCGI\Record\Stderr; +use Swoole\FastCGI\Record\Stdout; use Swoole\Http\Status; class HttpResponse extends Response @@ -21,15 +24,24 @@ class HttpResponse extends Response /** @var string */ protected $reasonPhrase; - /** @var array */ - protected $headers = []; + /** + * @var array + */ + protected array $headers = []; - /** @var array */ - protected $headersMap = []; + /** + * @var array + */ + protected array $headersMap = []; - /** @var array */ - protected $setCookieHeaderLines = []; + /** + * @var array + */ + protected array $setCookieHeaderLines = []; + /** + * @param array $records + */ public function __construct(array $records = []) { parent::__construct($records); @@ -62,7 +74,7 @@ public function __construct(array $records = []) } } $statusCode = (int) ($statusCode ?? Status::OK); - $reasonPhrase = (string) ($reasonPhrase ?? Status::getReasonPhrase($statusCode)); + $reasonPhrase = $reasonPhrase ?? Status::getReasonPhrase($statusCode); $this->withStatusCode($statusCode)->withReasonPhrase($reasonPhrase); $this->withBody($body); } @@ -95,6 +107,9 @@ public function getHeader(string $name): ?string return $name ? $this->headers[$name] : null; } + /** + * @return array + */ public function getHeaders(): array { return $this->headers; @@ -107,6 +122,9 @@ public function withHeader(string $name, string $value): self return $this; } + /** + * @param array $headers + */ public function withHeaders(array $headers): self { foreach ($headers as $name => $value) { @@ -115,6 +133,9 @@ public function withHeaders(array $headers): self return $this; } + /** + * @return array + */ public function getSetCookieHeaderLines(): array { return $this->setCookieHeaderLines; diff --git a/src/core/FastCGI/Response.php b/src/core/FastCGI/Response.php index b4a24c3..7c041e7 100644 --- a/src/core/FastCGI/Response.php +++ b/src/core/FastCGI/Response.php @@ -17,13 +17,16 @@ class Response extends Message { - public function __construct(array $records = []) + /** + * @param array $records + */ + public function __construct(array $records) { if (!static::verify($records)) { throw new \InvalidArgumentException('Bad records'); } - $body = ''; - $error = ''; + + $body = $error = ''; foreach ($records as $record) { if ($record instanceof Stdout) { if ($record->getContentLength() > 0) { @@ -38,8 +41,11 @@ public function __construct(array $records = []) $this->withBody($body)->withError($error); } - public static function verify(array $records): bool + /** + * @param array $records + */ + protected static function verify(array $records): bool { - return !empty($records) && $records[count($records) - 1] instanceof EndRequest; + return !empty($records) && $records[array_key_last($records)] instanceof EndRequest; } }