Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update generics #28

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/Body/FormPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@

final class FormPayload extends Map implements PayloadInterface
{
/**
* Get the header content type value.
*/
public function contentType(): ?string
{
return 'application/x-www-form-urlencoded';
}

/**
* Get the string representation of the payload.
*/
public function __toString()
{
return \http_build_query($this->all());
Expand Down
6 changes: 0 additions & 6 deletions src/Body/JsonPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,11 @@ public function __construct(array $parameters = [], int $flags = 0)
$this->flags = $flags;
}

/**
* Get the header content type value.
*/
public function contentType(): ?string
{
return 'application/json';
}

/**
* Get the string representation of the payload.
*/
public function __toString()
{
return \json_encode($this->all(), $this->flags) ?: '';
Expand Down
13 changes: 0 additions & 13 deletions src/Body/MultipartPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,13 @@ final class MultipartPayload extends Map implements PayloadInterface
*/
private $boundary;

/**
* Create new multipart payload instance.
*
* @param array $parameters
* @param null|string $boundary
* @return void
*/
public function __construct(array $parameters = [], ?string $boundary = null)
{
parent::__construct($parameters);

$this->boundary = $boundary ?: \bin2hex(\random_bytes(20));
}

/**
* Get the header content type value.
*/
public function contentType(): ?string
{
return 'multipart/form-data; boundary='.$this->boundary;
Expand Down Expand Up @@ -94,9 +84,6 @@ private function part(string $name, $value): string
return $str;
}

/**
* Get the string representation of the payload.
*/
public function __toString()
{
$str = '';
Expand Down
6 changes: 0 additions & 6 deletions src/Body/RawPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public function __construct(string $payload = '')
$this->payload = $payload;
}

/**
* Get the header content type value.
*/
public function contentType(): ?string
{
return null;
Expand Down Expand Up @@ -114,9 +111,6 @@ public function isEmpty(): bool
return $this->payload === '';
}

/**
* Get the string representation of the payload.
*/
public function __toString()
{
return $this->payload;
Expand Down
3 changes: 1 addition & 2 deletions src/ConnectorConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class ConnectorConfigurator
{
/**
* @var \Closure[]
* @var array<\Closure(T): void>
*/
private $handlers = [];

Expand Down Expand Up @@ -64,7 +64,6 @@ final public function middleware(callable $middleware, string $name = '')
protected function register(\Closure $handler)
{
$clone = clone $this;

$clone->handlers[] = $handler;

return $clone;
Expand Down
4 changes: 4 additions & 0 deletions src/Decoder/ChainDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function __construct(iterable $decoders)
public function decode(ResponseInterface $response): iterable
{
foreach ($this->decoders as $decoder) {
if (! $decoder instanceof DecoderInterface) {
throw new \InvalidArgumentException(sprintf('Decoder must implement %s. %s given', DecoderInterface::class, \get_debug_type($decoder)));
}

try {
yield from $decoder->decode($response);
} catch (NotDecodableException $e) {
Expand Down
3 changes: 3 additions & 0 deletions src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ final class Middleware implements \IteratorAggregate, \Countable
*/
private $middleware = [];

/**
* @param array<array-key, callable(RequestInterface, callable): ResponseInterface> $middleware
*/
public function __construct(array $middleware = [])
{
foreach ($middleware as $name => $value) {
Expand Down
4 changes: 4 additions & 0 deletions src/Middleware/Interceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ final class Interceptor
{
/**
* Add a request interceptor.
*
* @param \Closure(RequestInterface): RequestInterface $callback
*/
public static function request(Closure $callback): Closure
{
Expand All @@ -22,6 +24,8 @@ public static function request(Closure $callback): Closure

/**
* Add a response interceptor.
*
* @param \Closure(ResponseInterface): ResponseInterface $callback
*/
public static function response(Closure $callback): Closure
{
Expand Down
8 changes: 6 additions & 2 deletions src/Retry/RetryCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
final class RetryCallback implements RetryStrategyInterface
{
/**
* @var \Closure
* @var \Closure(RequestInterface, ResponseInterface): bool
*/
private $when;

/**
* @var \Closure
* @var \Closure(RetryContext): int
*/
private $delay;

/**
* @param \Closure(RequestInterface, ResponseInterface): bool $when
* @param \Closure(RetryContext): int $delay
*/
public function __construct(Closure $when, Closure $delay)
{
$this->when = $when;
Expand Down
1 change: 0 additions & 1 deletion src/Traits/HasClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ trait HasClient
public function withClient(ClientInterface $client)
{
$clone = clone $this;

$clone->client = $client;

return $clone;
Expand Down
4 changes: 4 additions & 0 deletions src/Traits/HasMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Fansipan\Traits;

use Fansipan\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

trait HasMiddleware
{
Expand All @@ -29,6 +31,8 @@ public function middleware(): Middleware

/**
* Get default middleware.
*
* @return array<array-key, callable(RequestInterface, callable): ResponseInterface>
*/
protected function defaultMiddleware(): array
{
Expand Down
Loading