Skip to content

Commit

Permalink
improved type declarations on FastCGI classes
Browse files Browse the repository at this point in the history
  • Loading branch information
deminy committed Feb 16, 2024
1 parent 497bb74 commit 4832d80
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/core/Coroutine/FastCGI/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -91,6 +93,7 @@ public function execute(Request $request, float $timeout = -1): Response
$this->socket->close();
$this->socket = null;
}
/* @var array<Stdout|Stderr|EndRequest> $records */
return match (true) {
$request instanceof HttpRequest => new HttpResponse($records),

Check failure on line 98 in src/core/Coroutine/FastCGI/Client.php

View workflow job for this annotation

GitHub Actions / static-analysis

Parameter #1 $records of class Swoole\FastCGI\HttpResponse constructor expects array<Swoole\FastCGI\Record\EndRequest|Swoole\FastCGI\Record\Stderr|Swoole\FastCGI\Record\Stdout>, array<int, Swoole\FastCGI\Record> given.
default => new Response($records),

Check failure on line 99 in src/core/Coroutine/FastCGI/Client.php

View workflow job for this annotation

GitHub Actions / static-analysis

Parameter #1 $records of class Swoole\FastCGI\Response constructor expects array<Swoole\FastCGI\Record\EndRequest|Swoole\FastCGI\Record\Stderr|Swoole\FastCGI\Record\Stdout>, array<int, Swoole\FastCGI\Record> given.
Expand Down
35 changes: 28 additions & 7 deletions src/core/FastCGI/HttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,15 +24,24 @@ class HttpResponse extends Response
/** @var string */
protected $reasonPhrase;

/** @var array */
protected $headers = [];
/**
* @var array<string, string>
*/
protected array $headers = [];

/** @var array */
protected $headersMap = [];
/**
* @var array<string, string>
*/
protected array $headersMap = [];

/** @var array */
protected $setCookieHeaderLines = [];
/**
* @var array<string>
*/
protected array $setCookieHeaderLines = [];

/**
* @param array<Stdout|Stderr|EndRequest> $records
*/
public function __construct(array $records = [])
{
parent::__construct($records);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -95,6 +107,9 @@ public function getHeader(string $name): ?string
return $name ? $this->headers[$name] : null;
}

/**
* @return array<string, string>
*/
public function getHeaders(): array
{
return $this->headers;
Expand All @@ -107,6 +122,9 @@ public function withHeader(string $name, string $value): self
return $this;
}

/**
* @param array<string, string> $headers
*/
public function withHeaders(array $headers): self
{
foreach ($headers as $name => $value) {
Expand All @@ -115,6 +133,9 @@ public function withHeaders(array $headers): self
return $this;
}

/**
* @return array<string>
*/
public function getSetCookieHeaderLines(): array
{
return $this->setCookieHeaderLines;
Expand Down
16 changes: 11 additions & 5 deletions src/core/FastCGI/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@

class Response extends Message
{
public function __construct(array $records = [])
/**
* @param array<Stdout|Stderr|EndRequest> $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) {
Expand All @@ -38,8 +41,11 @@ public function __construct(array $records = [])
$this->withBody($body)->withError($error);
}

public static function verify(array $records): bool
/**
* @param array<Stdout|Stderr|EndRequest> $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;
}
}

0 comments on commit 4832d80

Please sign in to comment.