Skip to content

Commit 38b66ea

Browse files
authored
Merge pull request #193 from GrahamCampbell/type-errors
Consitent implementation of union type checking
2 parents cd17f35 + ec6e78a commit 38b66ea

8 files changed

+37
-9
lines changed

spec/FlexibleHttpClientSpec.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public function it_is_an_async_http_client()
3232
$this->shouldImplement(HttpAsyncClient::class);
3333
}
3434

35-
public function it_throw_exception_if_invalid_client()
35+
public function it_throw_type_error_if_invalid_client()
3636
{
3737
$this->beConstructedWith(null);
3838

39-
$this->shouldThrow(\LogicException::class)->duringInstantiation();
39+
$this->shouldThrow(\TypeError::class)->duringInstantiation();
4040
}
4141

4242
public function it_emulates_an_async_client(

src/FlexibleHttpClient.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ final class FlexibleHttpClient implements HttpClient, HttpAsyncClient
2424
*/
2525
public function __construct($client)
2626
{
27-
if (!($client instanceof ClientInterface) && !($client instanceof HttpAsyncClient)) {
28-
throw new \LogicException(sprintf('Client must be an instance of %s or %s', ClientInterface::class, HttpAsyncClient::class));
27+
if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) {
28+
throw new \TypeError(
29+
sprintf('%s::__construct(): Argument #1 ($client) must be of type %s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, get_debug_type($client))
30+
);
2931
}
3032

3133
$this->httpClient = $client;

src/HttpClientPool/HttpClientPool.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ abstract class HttpClientPool implements HttpClientPoolInterface
2929
*/
3030
public function addHttpClient($client): void
3131
{
32+
if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient && !$client instanceof HttpClientPoolItem) {
33+
throw new \TypeError(
34+
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))
35+
);
36+
}
37+
3238
if (!$client instanceof HttpClientPoolItem) {
3339
$client = new HttpClientPoolItem($client);
3440
}

src/HttpClientPool/HttpClientPoolItem.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ class HttpClientPoolItem implements HttpClient, HttpAsyncClient
5959
*/
6060
public function __construct($client, int $reenableAfter = null)
6161
{
62+
if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) {
63+
throw new \TypeError(
64+
sprintf('%s::__construct(): Argument #1 ($client) must be of type %s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, get_debug_type($client))
65+
);
66+
}
67+
6268
$this->client = new FlexibleHttpClient($client);
6369
$this->reenableAfter = $reenableAfter;
6470
}

src/HttpClientRouter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public function sendAsyncRequest(RequestInterface $request)
4646
*/
4747
public function addClient($client, RequestMatcher $requestMatcher): void
4848
{
49+
if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) {
50+
throw new \TypeError(
51+
sprintf('%s::addClient(): Argument #1 ($client) must be of type %s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, get_debug_type($client))
52+
);
53+
}
54+
4955
$this->clients[] = [
5056
'matcher' => $requestMatcher,
5157
'client' => new FlexibleHttpClient($client),

src/PluginClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ final class PluginClient implements HttpClient, HttpAsyncClient
5050
*
5151
* @var int $max_restarts
5252
* }
53-
*
54-
* @throws \RuntimeException if client is not an instance of HttpClient or HttpAsyncClient
5553
*/
5654
public function __construct($client, array $plugins = [], array $options = [])
5755
{
@@ -60,7 +58,9 @@ public function __construct($client, array $plugins = [], array $options = [])
6058
} elseif ($client instanceof ClientInterface) {
6159
$this->client = new EmulatedHttpAsyncClient($client);
6260
} else {
63-
throw new \LogicException(sprintf('Client must be an instance of %s or %s', ClientInterface::class, HttpAsyncClient::class));
61+
throw new \TypeError(
62+
sprintf('%s::__construct(): Argument #1 ($client) must be of type %s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, get_debug_type($client))
63+
);
6464
}
6565

6666
$this->plugins = $plugins;

src/PluginClientBuilder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ public function removeOption(string $name): self
4545
}
4646

4747
/**
48-
* @param ClientInterface | HttpAsyncClient $client
48+
* @param ClientInterface|HttpAsyncClient $client
4949
*/
5050
public function createClient($client): PluginClient
5151
{
5252
if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) {
53-
throw new \RuntimeException('You must provide a valid http client');
53+
throw new \TypeError(
54+
sprintf('%s::createClient(): Argument #1 ($client) must be of type %s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, get_debug_type($client))
55+
);
5456
}
5557

5658
$plugins = $this->plugins;

src/PluginClientFactory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public static function setFactory(callable $factory)
4747
*/
4848
public function createClient($client, array $plugins = [], array $options = []): PluginClient
4949
{
50+
if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient) {
51+
throw new \TypeError(
52+
sprintf('%s::createClient(): Argument #1 ($client) must be of type %s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, get_debug_type($client))
53+
);
54+
}
55+
5056
if (static::$factory) {
5157
$factory = static::$factory;
5258

0 commit comments

Comments
 (0)