Skip to content

Consitent implementation of union type checking #193

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

Merged
merged 2 commits into from
Jul 1, 2020
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
4 changes: 2 additions & 2 deletions spec/FlexibleHttpClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 4 additions & 2 deletions src/FlexibleHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/HttpClientPool/HttpClientPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions src/HttpClientPool/HttpClientPoolItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions src/HttpClientRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions src/PluginClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doc was incorrect anyway, so I think my change is fine, and can be regarded as a bug fix.

NB In general, I don't think we ever need throws docs for "Errors", only "Exceptions", so this is why I've not added any. One can think of Errors in PHP just like how Java's type system treats RuntimeExceptions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree 👍

*/
public function __construct($client, array $plugins = [], array $options = [])
{
Expand All @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/PluginClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/PluginClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down