diff --git a/spec/FlexibleHttpClientSpec.php b/spec/FlexibleHttpClientSpec.php index 85de240..ec88da7 100644 --- a/spec/FlexibleHttpClientSpec.php +++ b/spec/FlexibleHttpClientSpec.php @@ -32,11 +32,11 @@ public function it_is_an_async_http_client() $this->shouldImplement(HttpAsyncClient::class); } - public function it_throw_exception_if_invalid_client() + public function it_throw_type_error_if_invalid_client() { $this->beConstructedWith(null); - $this->shouldThrow(\LogicException::class)->duringInstantiation(); + $this->shouldThrow(\TypeError::class)->duringInstantiation(); } public function it_emulates_an_async_client( diff --git a/src/FlexibleHttpClient.php b/src/FlexibleHttpClient.php index d6f1862..1a602c4 100644 --- a/src/FlexibleHttpClient.php +++ b/src/FlexibleHttpClient.php @@ -24,8 +24,10 @@ final class FlexibleHttpClient implements HttpClient, HttpAsyncClient */ public function __construct($client) { - if (!($client instanceof ClientInterface) && !($client instanceof HttpAsyncClient)) { - throw new \LogicException(sprintf('Client must be an instance of %s or %s', ClientInterface::class, HttpAsyncClient::class)); + if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) { + throw new \TypeError( + sprintf('%s::__construct(): Argument #1 ($client) must be of type %s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, get_debug_type($client)) + ); } $this->httpClient = $client; diff --git a/src/HttpClientPool/HttpClientPool.php b/src/HttpClientPool/HttpClientPool.php index ffa21d2..0665aa7 100644 --- a/src/HttpClientPool/HttpClientPool.php +++ b/src/HttpClientPool/HttpClientPool.php @@ -29,6 +29,12 @@ abstract class HttpClientPool implements HttpClientPoolInterface */ public function addHttpClient($client): void { + if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient && !$client instanceof HttpClientPoolItem) { + throw new \TypeError( + sprintf('%s::addHttpClient(): Argument #1 ($client) must be of type %s|%s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, HttpClientPoolItem::class, get_debug_type($client)) + ); + } + if (!$client instanceof HttpClientPoolItem) { $client = new HttpClientPoolItem($client); } diff --git a/src/HttpClientPool/HttpClientPoolItem.php b/src/HttpClientPool/HttpClientPoolItem.php index c22c080..f29d065 100644 --- a/src/HttpClientPool/HttpClientPoolItem.php +++ b/src/HttpClientPool/HttpClientPoolItem.php @@ -59,6 +59,12 @@ class HttpClientPoolItem implements HttpClient, HttpAsyncClient */ public function __construct($client, int $reenableAfter = null) { + if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) { + throw new \TypeError( + sprintf('%s::__construct(): Argument #1 ($client) must be of type %s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, get_debug_type($client)) + ); + } + $this->client = new FlexibleHttpClient($client); $this->reenableAfter = $reenableAfter; } diff --git a/src/HttpClientRouter.php b/src/HttpClientRouter.php index 84c7e04..78a4e69 100644 --- a/src/HttpClientRouter.php +++ b/src/HttpClientRouter.php @@ -46,6 +46,12 @@ public function sendAsyncRequest(RequestInterface $request) */ public function addClient($client, RequestMatcher $requestMatcher): void { + if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) { + throw new \TypeError( + sprintf('%s::addClient(): Argument #1 ($client) must be of type %s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, get_debug_type($client)) + ); + } + $this->clients[] = [ 'matcher' => $requestMatcher, 'client' => new FlexibleHttpClient($client), diff --git a/src/PluginClient.php b/src/PluginClient.php index 7a44aab..f935b0d 100644 --- a/src/PluginClient.php +++ b/src/PluginClient.php @@ -50,8 +50,6 @@ final class PluginClient implements HttpClient, HttpAsyncClient * * @var int $max_restarts * } - * - * @throws \RuntimeException if client is not an instance of HttpClient or HttpAsyncClient */ public function __construct($client, array $plugins = [], array $options = []) { @@ -60,7 +58,9 @@ public function __construct($client, array $plugins = [], array $options = []) } elseif ($client instanceof ClientInterface) { $this->client = new EmulatedHttpAsyncClient($client); } else { - throw new \LogicException(sprintf('Client must be an instance of %s or %s', ClientInterface::class, HttpAsyncClient::class)); + throw new \TypeError( + sprintf('%s::__construct(): Argument #1 ($client) must be of type %s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, get_debug_type($client)) + ); } $this->plugins = $plugins; diff --git a/src/PluginClientBuilder.php b/src/PluginClientBuilder.php index c1012df..87ee280 100644 --- a/src/PluginClientBuilder.php +++ b/src/PluginClientBuilder.php @@ -45,12 +45,14 @@ public function removeOption(string $name): self } /** - * @param ClientInterface | HttpAsyncClient $client + * @param ClientInterface|HttpAsyncClient $client */ public function createClient($client): PluginClient { if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) { - throw new \RuntimeException('You must provide a valid http client'); + throw new \TypeError( + sprintf('%s::createClient(): Argument #1 ($client) must be of type %s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, get_debug_type($client)) + ); } $plugins = $this->plugins; diff --git a/src/PluginClientFactory.php b/src/PluginClientFactory.php index 3806e09..bbdc5c3 100644 --- a/src/PluginClientFactory.php +++ b/src/PluginClientFactory.php @@ -47,6 +47,12 @@ public static function setFactory(callable $factory) */ public function createClient($client, array $plugins = [], array $options = []): PluginClient { + if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) { + throw new \TypeError( + sprintf('%s::createClient(): Argument #1 ($client) must be of type %s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, get_debug_type($client)) + ); + } + if (static::$factory) { $factory = static::$factory;