From e07f4a84d026bfd61bf8ad07b89fccd6a80e392a Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Tue, 16 Jan 2024 16:01:45 +0100 Subject: [PATCH] refactor: remove Safe usages (#235) --- src/Client/ClickHouseClient.php | 6 ------ src/Format/Json.php | 2 +- src/Format/JsonCompact.php | 2 +- src/Format/JsonEachRow.php | 2 +- src/Output/Json.php | 8 +++++--- src/Output/JsonCompact.php | 8 +++++--- src/Output/JsonEachRow.php | 12 +++++++++--- src/Snippet/CurrentDatabase.php | 2 -- src/Snippet/DatabaseSize.php | 2 -- src/Snippet/Parts.php | 2 -- src/Snippet/ShowCreateTable.php | 2 -- src/Snippet/ShowDatabases.php | 2 -- src/Snippet/TableSizes.php | 2 -- src/Snippet/Version.php | 2 -- src/Sql/SqlFactory.php | 4 +--- src/Sql/ValueFormatter.php | 8 ++------ tests/Exception/UnsupportedValueTest.php | 8 ++++---- tests/Snippet/ShowCreateTableTest.php | 16 ++++++++++------ tests/Sql/ValueFormatterTest.php | 2 +- tests/WithClient.php | 2 -- 20 files changed, 40 insertions(+), 54 deletions(-) diff --git a/src/Client/ClickHouseClient.php b/src/Client/ClickHouseClient.php index a94f557..6d95c9b 100644 --- a/src/Client/ClickHouseClient.php +++ b/src/Client/ClickHouseClient.php @@ -5,7 +5,6 @@ namespace SimPod\ClickHouseClient\Client; use Psr\Http\Client\ClientExceptionInterface; -use Safe\Exceptions\PcreException; use SimPod\ClickHouseClient\Exception\CannotInsert; use SimPod\ClickHouseClient\Exception\ServerError; use SimPod\ClickHouseClient\Exception\UnsupportedValue; @@ -18,7 +17,6 @@ interface ClickHouseClient * @param array $settings * * @throws ClientExceptionInterface - * @throws PcreException * @throws ServerError */ public function executeQuery(string $query, array $settings = []): void; @@ -28,7 +26,6 @@ public function executeQuery(string $query, array $settings = []): void; * @param array $settings * * @throws ClientExceptionInterface - * @throws PcreException * @throws ServerError * @throws UnsupportedValue */ @@ -41,7 +38,6 @@ public function executeQueryWithParams(string $query, array $params, array $sett * @return O * * @throws ClientExceptionInterface - * @throws PcreException * @throws ServerError * * @template O of Output @@ -56,7 +52,6 @@ public function select(string $query, Format $outputFormat, array $settings = [] * @return O * * @throws ClientExceptionInterface - * @throws PcreException * @throws ServerError * @throws UnsupportedValue * @@ -71,7 +66,6 @@ public function selectWithParams(string $query, array $params, Format $outputFor * * @throws CannotInsert * @throws ClientExceptionInterface - * @throws PcreException * @throws ServerError */ public function insert(string $table, array $values, array|null $columns = null, array $settings = []): void; diff --git a/src/Format/Json.php b/src/Format/Json.php index 9c7342f..9f68bec 100644 --- a/src/Format/Json.php +++ b/src/Format/Json.php @@ -4,7 +4,7 @@ namespace SimPod\ClickHouseClient\Format; -use Safe\Exceptions\JsonException; +use JsonException; use SimPod\ClickHouseClient\Output\Output; /** diff --git a/src/Format/JsonCompact.php b/src/Format/JsonCompact.php index 1e9e1f3..c901cc0 100644 --- a/src/Format/JsonCompact.php +++ b/src/Format/JsonCompact.php @@ -4,7 +4,7 @@ namespace SimPod\ClickHouseClient\Format; -use Safe\Exceptions\JsonException; +use JsonException; use SimPod\ClickHouseClient\Output\Output; /** diff --git a/src/Format/JsonEachRow.php b/src/Format/JsonEachRow.php index 4d669d9..9f2aa8d 100644 --- a/src/Format/JsonEachRow.php +++ b/src/Format/JsonEachRow.php @@ -4,7 +4,7 @@ namespace SimPod\ClickHouseClient\Format; -use Safe\Exceptions\JsonException; +use JsonException; use SimPod\ClickHouseClient\Output\Output; /** diff --git a/src/Output/Json.php b/src/Output/Json.php index 1ac07c2..0bb80fa 100644 --- a/src/Output/Json.php +++ b/src/Output/Json.php @@ -4,9 +4,11 @@ namespace SimPod\ClickHouseClient\Output; -use Safe\Exceptions\JsonException; +use JsonException; -use function Safe\json_decode; +use function json_decode; + +use const JSON_THROW_ON_ERROR; /** * @psalm-immutable @@ -36,7 +38,7 @@ public function __construct(string $contentsJson) * @var array{data: list, meta: array, rows: int, rows_before_limit_at_least?: int, statistics: array{elapsed: float, rows_read: int, bytes_read: int}} $contents * @psalm-suppress ImpureFunctionCall */ - $contents = json_decode($contentsJson, true); + $contents = json_decode($contentsJson, true, flags: JSON_THROW_ON_ERROR); $this->data = $contents['data']; $this->meta = $contents['meta']; $this->rows = $contents['rows']; diff --git a/src/Output/JsonCompact.php b/src/Output/JsonCompact.php index 60793dd..c4d6df2 100644 --- a/src/Output/JsonCompact.php +++ b/src/Output/JsonCompact.php @@ -4,9 +4,11 @@ namespace SimPod\ClickHouseClient\Output; -use Safe\Exceptions\JsonException; +use JsonException; -use function Safe\json_decode; +use function json_decode; + +use const JSON_THROW_ON_ERROR; /** * @psalm-immutable @@ -41,7 +43,7 @@ public function __construct(string $contentsJson) * } $contents * @psalm-suppress ImpureFunctionCall */ - $contents = json_decode($contentsJson, true); + $contents = json_decode($contentsJson, true, flags: JSON_THROW_ON_ERROR); $this->data = $contents['data']; $this->meta = $contents['meta']; $this->rows = $contents['rows']; diff --git a/src/Output/JsonEachRow.php b/src/Output/JsonEachRow.php index f59a14e..c24e8c4 100644 --- a/src/Output/JsonEachRow.php +++ b/src/Output/JsonEachRow.php @@ -4,12 +4,14 @@ namespace SimPod\ClickHouseClient\Output; -use Safe\Exceptions\JsonException; +use JsonException; -use function Safe\json_decode; +use function json_decode; use function sprintf; use function str_replace; +use const JSON_THROW_ON_ERROR; + /** * @psalm-immutable * @template T @@ -27,7 +29,11 @@ public function __construct(string $contentsJson) * @var list $contents * @psalm-suppress ImpureFunctionCall */ - $contents = json_decode(sprintf('[%s]', str_replace("}\n{", '},{', $contentsJson)), true); + $contents = json_decode( + sprintf('[%s]', str_replace("}\n{", '},{', $contentsJson)), + true, + flags: JSON_THROW_ON_ERROR, + ); $this->data = $contents; } } diff --git a/src/Snippet/CurrentDatabase.php b/src/Snippet/CurrentDatabase.php index 60a53d0..477c5a2 100644 --- a/src/Snippet/CurrentDatabase.php +++ b/src/Snippet/CurrentDatabase.php @@ -5,7 +5,6 @@ namespace SimPod\ClickHouseClient\Snippet; use Psr\Http\Client\ClientExceptionInterface; -use Safe\Exceptions\PcreException; use SimPod\ClickHouseClient\Client\ClickHouseClient; use SimPod\ClickHouseClient\Exception\ServerError; use SimPod\ClickHouseClient\Format\JsonEachRow; @@ -14,7 +13,6 @@ final class CurrentDatabase { /** * @throws ClientExceptionInterface - * @throws PcreException * @throws ServerError */ public static function run(ClickHouseClient $clickHouseClient): string diff --git a/src/Snippet/DatabaseSize.php b/src/Snippet/DatabaseSize.php index fa159fc..238f587 100644 --- a/src/Snippet/DatabaseSize.php +++ b/src/Snippet/DatabaseSize.php @@ -5,7 +5,6 @@ namespace SimPod\ClickHouseClient\Snippet; use Psr\Http\Client\ClientExceptionInterface; -use Safe\Exceptions\PcreException; use SimPod\ClickHouseClient\Client\ClickHouseClient; use SimPod\ClickHouseClient\Exception\ServerError; use SimPod\ClickHouseClient\Exception\UnsupportedValue; @@ -16,7 +15,6 @@ final class DatabaseSize { /** * @throws ClientExceptionInterface - * @throws PcreException * @throws ServerError * @throws UnsupportedValue */ diff --git a/src/Snippet/Parts.php b/src/Snippet/Parts.php index dc5633a..74a7715 100644 --- a/src/Snippet/Parts.php +++ b/src/Snippet/Parts.php @@ -5,7 +5,6 @@ namespace SimPod\ClickHouseClient\Snippet; use Psr\Http\Client\ClientExceptionInterface; -use Safe\Exceptions\PcreException; use SimPod\ClickHouseClient\Client\ClickHouseClient; use SimPod\ClickHouseClient\Exception\ServerError; use SimPod\ClickHouseClient\Exception\UnsupportedValue; @@ -19,7 +18,6 @@ final class Parts * @return array> * * @throws ClientExceptionInterface - * @throws PcreException * @throws ServerError * @throws UnsupportedValue */ diff --git a/src/Snippet/ShowCreateTable.php b/src/Snippet/ShowCreateTable.php index 28a3c19..e23bea6 100644 --- a/src/Snippet/ShowCreateTable.php +++ b/src/Snippet/ShowCreateTable.php @@ -5,7 +5,6 @@ namespace SimPod\ClickHouseClient\Snippet; use Psr\Http\Client\ClientExceptionInterface; -use Safe\Exceptions\PcreException; use SimPod\ClickHouseClient\Client\ClickHouseClient; use SimPod\ClickHouseClient\Exception\ServerError; use SimPod\ClickHouseClient\Format\JsonEachRow; @@ -14,7 +13,6 @@ final class ShowCreateTable { /** * @throws ClientExceptionInterface - * @throws PcreException * @throws ServerError */ public static function run(ClickHouseClient $clickHouseClient, string $tableName): string diff --git a/src/Snippet/ShowDatabases.php b/src/Snippet/ShowDatabases.php index c95c9d7..186e1ba 100644 --- a/src/Snippet/ShowDatabases.php +++ b/src/Snippet/ShowDatabases.php @@ -5,7 +5,6 @@ namespace SimPod\ClickHouseClient\Snippet; use Psr\Http\Client\ClientExceptionInterface; -use Safe\Exceptions\PcreException; use SimPod\ClickHouseClient\Client\ClickHouseClient; use SimPod\ClickHouseClient\Exception\ServerError; use SimPod\ClickHouseClient\Format\JsonEachRow; @@ -18,7 +17,6 @@ final class ShowDatabases * @return list * * @throws ClientExceptionInterface - * @throws PcreException * @throws ServerError */ public static function run(ClickHouseClient $clickHouseClient): array diff --git a/src/Snippet/TableSizes.php b/src/Snippet/TableSizes.php index e2fb766..b7377d6 100644 --- a/src/Snippet/TableSizes.php +++ b/src/Snippet/TableSizes.php @@ -5,7 +5,6 @@ namespace SimPod\ClickHouseClient\Snippet; use Psr\Http\Client\ClientExceptionInterface; -use Safe\Exceptions\PcreException; use SimPod\ClickHouseClient\Client\ClickHouseClient; use SimPod\ClickHouseClient\Exception\ServerError; use SimPod\ClickHouseClient\Exception\UnsupportedValue; @@ -19,7 +18,6 @@ final class TableSizes * @return array * * @throws ClientExceptionInterface - * @throws PcreException * @throws ServerError * @throws UnsupportedValue */ diff --git a/src/Snippet/Version.php b/src/Snippet/Version.php index 779b962..a78944f 100644 --- a/src/Snippet/Version.php +++ b/src/Snippet/Version.php @@ -5,7 +5,6 @@ namespace SimPod\ClickHouseClient\Snippet; use Psr\Http\Client\ClientExceptionInterface; -use Safe\Exceptions\PcreException; use SimPod\ClickHouseClient\Client\ClickHouseClient; use SimPod\ClickHouseClient\Exception\ServerError; use SimPod\ClickHouseClient\Format\JsonEachRow; @@ -14,7 +13,6 @@ final class Version { /** * @throws ClientExceptionInterface - * @throws PcreException * @throws ServerError */ public static function run(ClickHouseClient $clickHouseClient): string diff --git a/src/Sql/SqlFactory.php b/src/Sql/SqlFactory.php index 03302a4..cba2085 100644 --- a/src/Sql/SqlFactory.php +++ b/src/Sql/SqlFactory.php @@ -4,12 +4,11 @@ namespace SimPod\ClickHouseClient\Sql; -use Safe\Exceptions\PcreException; use SimPod\ClickHouseClient\Exception\UnsupportedValue; use function assert; use function is_string; -use function Safe\preg_replace; +use function preg_replace; use function sprintf; use function str_replace; @@ -23,7 +22,6 @@ public function __construct(private ValueFormatter $valueFormatter) /** * @param array $parameters * - * @throws PcreException * @throws UnsupportedValue */ public function createWithParameters(string $query, array $parameters): string diff --git a/src/Sql/ValueFormatter.php b/src/Sql/ValueFormatter.php index 7bcdde1..4f3ec6f 100644 --- a/src/Sql/ValueFormatter.php +++ b/src/Sql/ValueFormatter.php @@ -7,7 +7,6 @@ use BackedEnum; use DateTimeImmutable; use DateTimeZone; -use Safe\Exceptions\PcreException; use SimPod\ClickHouseClient\Exception\UnsupportedValue; use function array_key_first; @@ -20,7 +19,7 @@ use function is_object; use function is_string; use function method_exists; -use function Safe\preg_match; +use function preg_match; use function sprintf; /** @internal */ @@ -30,10 +29,7 @@ public function __construct(private DateTimeZone|null $dateTimeZone = null) { } - /** - * @throws PcreException - * @throws UnsupportedValue - */ + /** @throws UnsupportedValue */ public function format(mixed $value, string|null $paramName = null, string|null $sql = null): string { if (is_string($value)) { diff --git a/tests/Exception/UnsupportedValueTest.php b/tests/Exception/UnsupportedValueTest.php index f53f701..1a3d35d 100644 --- a/tests/Exception/UnsupportedValueTest.php +++ b/tests/Exception/UnsupportedValueTest.php @@ -4,14 +4,14 @@ namespace SimPod\ClickHouseClient\Tests\Exception; +use DateTime; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; -use Safe\DateTime; use SimPod\ClickHouseClient\Exception\UnsupportedValue; use SimPod\ClickHouseClient\Tests\TestCaseBase; use stdClass; -use function Safe\opendir; +use function opendir; use function sprintf; use const PHP_VERSION_ID; @@ -41,7 +41,7 @@ public static function providerType(): iterable ]; yield [ - 'Value of type "Safe\DateTime" is not supported as a parameter', + 'Value of type "DateTime" is not supported as a parameter', new DateTime(), ]; } @@ -72,7 +72,7 @@ public static function providerValue(): iterable yield [ sprintf( - "Value \"%sSafe\DateTime::__set_state(array( + "Value \"%sDateTime::__set_state(array( 'date' => '2022-02-02 13:31:37.593289', 'timezone_type' => 3, 'timezone' => 'UTC', diff --git a/tests/Snippet/ShowCreateTableTest.php b/tests/Snippet/ShowCreateTableTest.php index fa1f3ca..f44eb6c 100644 --- a/tests/Snippet/ShowCreateTableTest.php +++ b/tests/Snippet/ShowCreateTableTest.php @@ -9,7 +9,9 @@ use SimPod\ClickHouseClient\Tests\TestCaseBase; use SimPod\ClickHouseClient\Tests\WithClient; -use function Safe\preg_replace; +use function assert; +use function is_string; +use function preg_replace; use function str_replace; #[CoversClass(ShowCreateTable::class)] @@ -29,14 +31,16 @@ public function testRun(): void $createTableSql = ShowCreateTable::run($this->client, 'test'); // BC + $replaced = preg_replace( + '!\s+!', + ' ', + str_replace('\n', ' ', $createTableSql), + ); + assert(is_string($replaced)); $createTableSql = str_replace( ['( ', ' )'], ['(', ')'], - preg_replace( - '!\s+!', - ' ', - str_replace('\n', ' ', $createTableSql), - ), + $replaced, ); self::assertSame($sql, $createTableSql); diff --git a/tests/Sql/ValueFormatterTest.php b/tests/Sql/ValueFormatterTest.php index c5c4f57..4bd70b2 100644 --- a/tests/Sql/ValueFormatterTest.php +++ b/tests/Sql/ValueFormatterTest.php @@ -4,10 +4,10 @@ namespace SimPod\ClickHouseClient\Tests\Sql; +use DateTimeImmutable; use DateTimeZone; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; -use Safe\DateTimeImmutable; use SimPod\ClickHouseClient\Exception\UnsupportedValue; use SimPod\ClickHouseClient\Sql\Expression; use SimPod\ClickHouseClient\Sql\ValueFormatter; diff --git a/tests/WithClient.php b/tests/WithClient.php index 78ad6bd..67d6829 100644 --- a/tests/WithClient.php +++ b/tests/WithClient.php @@ -9,7 +9,6 @@ use PHPUnit\Framework\Attributes\After; use PHPUnit\Framework\Attributes\Before; use Psr\Http\Client\ClientExceptionInterface; -use Safe\Exceptions\PcreException; use SimPod\ClickHouseClient\Client\ClickHouseAsyncClient; use SimPod\ClickHouseClient\Client\ClickHouseClient; use SimPod\ClickHouseClient\Client\Http\RequestFactory; @@ -52,7 +51,6 @@ public function tearDownDataBase(): void /** * @throws ClientExceptionInterface * @throws InvalidArgumentException - * @throws PcreException * @throws ServerError */ private function restartClickHouseClient(): void