Skip to content

Commit

Permalink
ci(phpstan): check @throws
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Oct 16, 2023
1 parent 69655c9 commit 87deca9
Show file tree
Hide file tree
Showing 22 changed files with 213 additions and 24 deletions.
10 changes: 10 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@ parameters:
- %currentWorkingDirectory%/src
- %currentWorkingDirectory%/tests

exceptions:
check:
missingCheckedExceptionInThrows: true
tooWideThrowType: true

ignoreErrors:
# There's no other way to test-pass without assertions while counting it towards coverage https://github.com/sebastianbergmann/phpunit/issues/3016
- '~Call to static method PHPUnit\\Framework\\Assert::assertTrue\(\) with true will always evaluate to true~'

# Adds unnecessary maintanence overhead. We rather rely on PHPStan telling us the method returns unhandled FALSE
- "~Class DateTime(Immutable)? is unsafe to use. Its methods can return FALSE instead of throwing an exception. Please add 'use Safe\\\\DateTime(Immutable)?;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library~"

# No need to have @throws in methods that are never called
## PHPUnit
- message: "~Method SimPod\\\\ClickHouseClient\\\\Tests\\\\.+?Test(CaseBase)?::(test.+?|provider.+?|setUp(BeforeClass)?|tearDown|setupClickHouseClient|tearDownDataBase)\\(\\) throws checked exception .+? but it's missing from the PHPDoc @throws tag~"
path: tests

includes:
- phpstan-baseline.neon
41 changes: 40 additions & 1 deletion src/Client/ClickHouseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,35 @@

namespace SimPod\ClickHouseClient\Client;

use InvalidArgumentException;
use Psr\Http\Client\ClientExceptionInterface;
use Safe\Exceptions\PcreException;
use SimPod\ClickHouseClient\Exception\CannotInsert;
use SimPod\ClickHouseClient\Exception\ServerError;
use SimPod\ClickHouseClient\Exception\UnsupportedValue;
use SimPod\ClickHouseClient\Format\Format;
use SimPod\ClickHouseClient\Output\Output;

interface ClickHouseClient
{
/** @param array<string, float|int|string> $settings */
/**
* @param array<string, float|int|string> $settings
*
* @throws InvalidArgumentException
* @throws ClientExceptionInterface
* @throws PcreException
* @throws ServerError
*/
public function executeQuery(string $query, array $settings = []): void;

/**
* @param array<string, mixed> $params
* @param array<string, float|int|string> $settings
*
* @throws ClientExceptionInterface
* @throws InvalidArgumentException
* @throws PcreException
* @throws ServerError
*/
public function executeQueryWithParams(string $query, array $params, array $settings = []): void;

Expand All @@ -24,6 +42,11 @@ public function executeQueryWithParams(string $query, array $params, array $sett
*
* @return O
*
* @throws ClientExceptionInterface
* @throws InvalidArgumentException
* @throws PcreException
* @throws ServerError
*
* @template O of Output
*/
public function select(string $query, Format $outputFormat, array $settings = []): Output;
Expand All @@ -35,6 +58,12 @@ public function select(string $query, Format $outputFormat, array $settings = []
*
* @return O
*
* @throws ClientExceptionInterface
* @throws InvalidArgumentException
* @throws PcreException
* @throws ServerError
* @throws UnsupportedValue
*
* @template O of Output
*/
public function selectWithParams(string $query, array $params, Format $outputFormat, array $settings = []): Output;
Expand All @@ -43,13 +72,23 @@ public function selectWithParams(string $query, array $params, Format $outputFor
* @param array<array<mixed>> $values
* @param array<string>|null $columns
* @param array<string, float|int|string> $settings
*
* @throws InvalidArgumentException
* @throws CannotInsert
* @throws ClientExceptionInterface
* @throws PcreException
* @throws ServerError
*/
public function insert(string $table, array $values, array|null $columns = null, array $settings = []): void;

/**
* @param array<string, float|int|string> $settings
* @param Format<O> $inputFormat
*
* @throws InvalidArgumentException
* @throws ClientExceptionInterface
* @throws ServerError
*
* @template O of Output
*/
public function insertWithFormat(string $table, Format $inputFormat, string $data, array $settings = []): void;
Expand Down
2 changes: 2 additions & 0 deletions src/Client/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SimPod\ClickHouseClient\Client\Http;

use InvalidArgumentException;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
Expand All @@ -25,6 +26,7 @@ public function __construct(
) {
}

/** @throws InvalidArgumentException */
public function prepareRequest(RequestOptions $requestOptions): RequestInterface
{
$query = http_build_query(
Expand Down
40 changes: 29 additions & 11 deletions src/Client/PsrClickHouseAsyncClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace SimPod\ClickHouseClient\Client;

use DateTimeZone;
use Exception;
use GuzzleHttp\Promise\Create;
use GuzzleHttp\Promise\PromiseInterface;
use Http\Client\HttpAsyncClient;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use SimPod\ClickHouseClient\Client\Http\RequestFactory;
use SimPod\ClickHouseClient\Client\Http\RequestOptions;
Expand All @@ -31,6 +33,11 @@ public function __construct(
$this->sqlFactory = new SqlFactory(new ValueFormatter($clickHouseTimeZone));
}

/**
* {@inheritDoc}
*
* @throws Exception
*/
public function select(string $sql, Format $outputFormat, array $settings = []): PromiseInterface
{
$formatClause = $outputFormat::toSql();
Expand All @@ -45,6 +52,11 @@ public function select(string $sql, Format $outputFormat, array $settings = []):
);
}

/**
* {@inheritDoc}
*
* @throws Exception
*/
public function selectWithParams(
string $sql,
array $params,
Expand All @@ -61,6 +73,9 @@ public function selectWithParams(
/**
* @param array<string, float|int|string> $settings
* @param (callable(ResponseInterface):mixed)|null $processResponse
*
* @throws InvalidArgumentException
* @throws Exception
*/
private function executeRequest(
string $sql,
Expand All @@ -75,18 +90,21 @@ private function executeRequest(
),
);

return Create::promiseFor($this->asyncClient->sendAsyncRequest($request))->then(
static function (ResponseInterface $response) use ($processResponse) {
if ($response->getStatusCode() !== 200) {
throw ServerError::fromResponse($response);
}
return Create::promiseFor(
$this->asyncClient->sendAsyncRequest($request),
)
->then(
static function (ResponseInterface $response) use ($processResponse) {
if ($response->getStatusCode() !== 200) {
throw ServerError::fromResponse($response);
}

if ($processResponse === null) {
return $response;
}
if ($processResponse === null) {
return $response;
}

return $processResponse($response);
},
);
return $processResponse($response);
},
);
}
}
10 changes: 9 additions & 1 deletion src/Client/PsrClickHouseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace SimPod\ClickHouseClient\Client;

use DateTimeZone;
use InvalidArgumentException;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\ResponseInterface;
use SimPod\ClickHouseClient\Client\Http\RequestFactory;
Expand Down Expand Up @@ -128,7 +130,13 @@ public function insertWithFormat(string $table, Format $inputFormat, string $dat
);
}

/** @param array<string, float|int|string> $settings */
/**
* @param array<string, float|int|string> $settings
*
* @throws ServerError
* @throws ClientExceptionInterface
* @throws InvalidArgumentException
*/
private function executeRequest(string $sql, array $settings = []): ResponseInterface
{
$request = $this->requestFactory->prepareRequest(
Expand Down
2 changes: 2 additions & 0 deletions src/Format/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SimPod\ClickHouseClient\Format;

use Safe\Exceptions\JsonException;
use SimPod\ClickHouseClient\Output\Output;

/**
Expand All @@ -12,6 +13,7 @@
*/
final class Json implements Format
{
/** @throws JsonException */
public static function output(string $contents): Output
{
return new \SimPod\ClickHouseClient\Output\Json($contents);
Expand Down
2 changes: 2 additions & 0 deletions src/Format/JsonCompact.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SimPod\ClickHouseClient\Format;

use Safe\Exceptions\JsonException;
use SimPod\ClickHouseClient\Output\Output;

/**
Expand All @@ -12,6 +13,7 @@
*/
final class JsonCompact implements Format
{
/** @throws JsonException */
public static function output(string $contents): Output
{
return new \SimPod\ClickHouseClient\Output\JsonCompact($contents);
Expand Down
2 changes: 2 additions & 0 deletions src/Format/JsonEachRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SimPod\ClickHouseClient\Format;

use Safe\Exceptions\JsonException;
use SimPod\ClickHouseClient\Output\Output;

/**
Expand All @@ -12,6 +13,7 @@
*/
final class JsonEachRow implements Format
{
/** @throws JsonException */
public static function output(string $contents): Output
{
return new \SimPod\ClickHouseClient\Output\JsonEachRow($contents);
Expand Down
3 changes: 3 additions & 0 deletions src/Output/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace SimPod\ClickHouseClient\Output;

use Safe\Exceptions\JsonException;

use function Safe\json_decode;

/**
Expand All @@ -26,6 +28,7 @@ final class Json implements Output
/** @var array{elapsed: float, rows_read: int, bytes_read: int} */
public array $statistics;

/** @throws JsonException */
public function __construct(string $contentsJson)
{
// phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
Expand Down
3 changes: 3 additions & 0 deletions src/Output/JsonCompact.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace SimPod\ClickHouseClient\Output;

use Safe\Exceptions\JsonException;

use function Safe\json_decode;

/**
Expand All @@ -26,6 +28,7 @@ final class JsonCompact implements Output
/** @var array{elapsed: float, rows_read: int, bytes_read: int} */
public array $statistics;

/** @throws JsonException */
public function __construct(string $contentsJson)
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Output/JsonEachRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace SimPod\ClickHouseClient\Output;

use Safe\Exceptions\JsonException;

use function Safe\json_decode;
use function sprintf;
use function str_replace;
Expand All @@ -18,6 +20,7 @@ final class JsonEachRow implements Output
/** @var list<T> */
public array $data;

/** @throws JsonException */
public function __construct(string $contentsJson)
{
/**
Expand Down
10 changes: 10 additions & 0 deletions src/Snippet/CurrentDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@

namespace SimPod\ClickHouseClient\Snippet;

use InvalidArgumentException;
use Psr\Http\Client\ClientExceptionInterface;
use Safe\Exceptions\PcreException;
use SimPod\ClickHouseClient\Client\ClickHouseClient;
use SimPod\ClickHouseClient\Exception\ServerError;
use SimPod\ClickHouseClient\Format\JsonEachRow;

final class CurrentDatabase
{
/**
* @throws ClientExceptionInterface
* @throws InvalidArgumentException
* @throws PcreException
* @throws ServerError
*/
public static function run(ClickHouseClient $clickHouseClient): string
{
/** @var JsonEachRow<array{database: string}> $format */
Expand Down
10 changes: 10 additions & 0 deletions src/Snippet/DatabaseSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@

namespace SimPod\ClickHouseClient\Snippet;

use InvalidArgumentException;
use Psr\Http\Client\ClientExceptionInterface;
use Safe\Exceptions\PcreException;
use SimPod\ClickHouseClient\Client\ClickHouseClient;
use SimPod\ClickHouseClient\Exception\ServerError;
use SimPod\ClickHouseClient\Format\JsonEachRow;
use SimPod\ClickHouseClient\Sql\Expression;

final class DatabaseSize
{
/**
* @throws ClientExceptionInterface
* @throws InvalidArgumentException
* @throws PcreException
* @throws ServerError
*/
public static function run(ClickHouseClient $clickHouseClient, string|null $databaseName = null): int
{
/** @var JsonEachRow<array{size: string|null}> $format */
Expand Down
13 changes: 12 additions & 1 deletion src/Snippet/Parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@

namespace SimPod\ClickHouseClient\Snippet;

use InvalidArgumentException;
use Psr\Http\Client\ClientExceptionInterface;
use Safe\Exceptions\PcreException;
use SimPod\ClickHouseClient\Client\ClickHouseClient;
use SimPod\ClickHouseClient\Exception\ServerError;
use SimPod\ClickHouseClient\Format\JsonEachRow;

use function sprintf;

final class Parts
{
/** @return array<array<string, mixed>> */
/**
* @return array<array<string, mixed>>
*
* @throws ClientExceptionInterface
* @throws InvalidArgumentException
* @throws PcreException
* @throws ServerError
*/
public static function run(ClickHouseClient $clickHouseClient, string $table, bool|null $active = null): array
{
$whereActiveClause = $active === null ? '' : sprintf(' AND active = %d', $active);
Expand Down
Loading

0 comments on commit 87deca9

Please sign in to comment.