Skip to content

Commit

Permalink
fix: type of return
Browse files Browse the repository at this point in the history
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 <vitor@php.rio>
  • Loading branch information
vitormattos committed May 8, 2024
1 parent c255f42 commit da48cfd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand Down
11 changes: 6 additions & 5 deletions src/Psr7/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -155,4 +156,4 @@ protected function normalize($header): string
{
return str_replace(" ", "-", ucwords(str_replace("-", " ", strtolower($header))));
}
}
}

0 comments on commit da48cfd

Please sign in to comment.