From 1161401ba35254f9b3812ff6006a51b8bd9f54c9 Mon Sep 17 00:00:00 2001 From: Lynh Date: Wed, 3 Jan 2024 00:06:58 +0700 Subject: [PATCH] Support generic --- phpstan.neon.dist | 2 +- src/Contracts/ConnectorInterface.php | 4 ++++ src/Request.php | 3 +++ src/Response.php | 11 ++++++++++- src/Traits/ConnectorTrait.php | 7 +------ tests/Services/JsonPlaceholder/GetUserRequest.php | 3 +++ 6 files changed, 22 insertions(+), 8 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index bb04caa..9ac106a 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -6,7 +6,7 @@ parameters: - src # The level 9 is the highest level - level: 7 + level: 8 tmpDir: build/phpstan diff --git a/src/Contracts/ConnectorInterface.php b/src/Contracts/ConnectorInterface.php index 9844dba..569cd83 100644 --- a/src/Contracts/ConnectorInterface.php +++ b/src/Contracts/ConnectorInterface.php @@ -25,6 +25,10 @@ public function middleware(): Middleware; * Send the given request. * * The request and response should be processed through middleware. + * + * @template T of object + * @param Request $request + * @return Response */ public function send(Request $request): Response; } diff --git a/src/Request.php b/src/Request.php index 6c27844..fabd516 100644 --- a/src/Request.php +++ b/src/Request.php @@ -9,6 +9,9 @@ use Fansipan\Contracts\PayloadInterface; use Fansipan\Decoder\ChainDecoder; +/** + * @template T of object + */ abstract class Request { /** diff --git a/src/Response.php b/src/Response.php index ee598c4..e49728e 100644 --- a/src/Response.php +++ b/src/Response.php @@ -11,6 +11,9 @@ use LogicException; use Psr\Http\Message\ResponseInterface; +/** + * @template T of object + */ final class Response implements \ArrayAccess, \JsonSerializable, \Stringable { use Traits\Macroable; @@ -62,6 +65,8 @@ public function data(): array /** * Get the decoded body of the response as an object. + * + * @return ?T */ public function object(): ?object { @@ -69,7 +74,7 @@ public function object(): ?object return null; } - return $this->decoder->map($this->response); + return $this->decoder->map($this->response); // @phpstan-ignore-line } /** @@ -232,6 +237,10 @@ public function throw(): self if ($this->failed()) { $exception = $this->toException(); + if (! $exception) { + return $this; + } + if ($callback && \is_callable($callback)) { $callback($this, $exception); } diff --git a/src/Traits/ConnectorTrait.php b/src/Traits/ConnectorTrait.php index 6cfd488..a49627f 100644 --- a/src/Traits/ConnectorTrait.php +++ b/src/Traits/ConnectorTrait.php @@ -25,18 +25,13 @@ public static function baseUri(): ?string return null; } - /** - * Send the given request. - * - * The request and response should be processed through middleware. - */ public function send(Request $request): Response { $response = $this->sendRequest( Util::request($request, static::baseUri()) ); - return new Response($response, $request->decoder()); + return new Response($response, $request->decoder()); // @phpstan-ignore-line } public function sendRequest(RequestInterface $request): ResponseInterface diff --git a/tests/Services/JsonPlaceholder/GetUserRequest.php b/tests/Services/JsonPlaceholder/GetUserRequest.php index 025bfa2..3d9f891 100644 --- a/tests/Services/JsonPlaceholder/GetUserRequest.php +++ b/tests/Services/JsonPlaceholder/GetUserRequest.php @@ -9,6 +9,9 @@ use Fansipan\Request; use Fansipan\Util; +/** + * @extends Request + */ final class GetUserRequest extends Request { private $id;