From da48cfd86ad4b70dfe5b42317399f59e704678a2 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Wed, 8 May 2024 12:27:49 -0300 Subject: [PATCH] fix: type of return The method getBody can't return null. The return of this method need to be a instance of StreamInterface. To fix this I create a instance of a class that extends StreamInterface. Reference: https://github.com/php-fig/http-message/blob/1.0/src/MessageInterface.php#L169-L171 Signed-off-by: Vitor Mattos --- src/HttpClient.php | 3 ++- src/Psr7/Message.php | 11 ++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/HttpClient.php b/src/HttpClient.php index d485d28..ed87192 100644 --- a/src/HttpClient.php +++ b/src/HttpClient.php @@ -5,6 +5,7 @@ use ByJG\Util\Exception\CurlException; use ByJG\Util\Exception\NetworkException; use ByJG\Util\Exception\RequestException; +use ByJG\Util\Psr7\NullStream; use InvalidArgumentException; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestInterface; @@ -180,7 +181,7 @@ protected function setMethod(): void protected function setBody(): void { $stream = $this->request->getBody(); - if (!is_null($stream)) { + if (!$stream instanceof NullStream) { if (!$this->getCurl(CURLOPT_POST) && !$this->getCurl(CURLOPT_CUSTOMREQUEST)) { throw new RequestException($this->request,"Cannot set body with method GET"); } diff --git a/src/Psr7/Message.php b/src/Psr7/Message.php index f65d364..1f4769e 100644 --- a/src/Psr7/Message.php +++ b/src/Psr7/Message.php @@ -133,11 +133,12 @@ public function withoutHeader($name): MessageInterface /** * @inheritDoc */ - public function getBody(): ?StreamInterface + public function getBody(): StreamInterface { - if (!is_null($this->body)) { - $this->body->rewind(); - } + if (is_null($this->body)) { + $this->body = new NullStream(); + } + $this->body->rewind(); return $this->body; } @@ -155,4 +156,4 @@ protected function normalize($header): string { return str_replace(" ", "-", ucwords(str_replace("-", " ", strtolower($header)))); } -} \ No newline at end of file +}