Skip to content

Commit

Permalink
refactor: remove Safe usages (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod authored Jan 16, 2024
1 parent 00d04f4 commit e07f4a8
Show file tree
Hide file tree
Showing 20 changed files with 40 additions and 54 deletions.
6 changes: 0 additions & 6 deletions src/Client/ClickHouseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,7 +17,6 @@ interface ClickHouseClient
* @param array<string, float|int|string> $settings
*
* @throws ClientExceptionInterface
* @throws PcreException
* @throws ServerError
*/
public function executeQuery(string $query, array $settings = []): void;
Expand All @@ -28,7 +26,6 @@ public function executeQuery(string $query, array $settings = []): void;
* @param array<string, float|int|string> $settings
*
* @throws ClientExceptionInterface
* @throws PcreException
* @throws ServerError
* @throws UnsupportedValue
*/
Expand All @@ -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
Expand All @@ -56,7 +52,6 @@ public function select(string $query, Format $outputFormat, array $settings = []
* @return O
*
* @throws ClientExceptionInterface
* @throws PcreException
* @throws ServerError
* @throws UnsupportedValue
*
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Format/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SimPod\ClickHouseClient\Format;

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

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Format/JsonCompact.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SimPod\ClickHouseClient\Format;

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

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Format/JsonEachRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SimPod\ClickHouseClient\Format;

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

/**
Expand Down
8 changes: 5 additions & 3 deletions src/Output/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -36,7 +38,7 @@ public function __construct(string $contentsJson)
* @var array{data: list<T>, meta: array<mixed>, 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'];
Expand Down
8 changes: 5 additions & 3 deletions src/Output/JsonCompact.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'];
Expand Down
12 changes: 9 additions & 3 deletions src/Output/JsonEachRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,7 +29,11 @@ public function __construct(string $contentsJson)
* @var list<T> $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;
}
}
2 changes: 0 additions & 2 deletions src/Snippet/CurrentDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,7 +13,6 @@ final class CurrentDatabase
{
/**
* @throws ClientExceptionInterface
* @throws PcreException
* @throws ServerError
*/
public static function run(ClickHouseClient $clickHouseClient): string
Expand Down
2 changes: 0 additions & 2 deletions src/Snippet/DatabaseSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,7 +15,6 @@ final class DatabaseSize
{
/**
* @throws ClientExceptionInterface
* @throws PcreException
* @throws ServerError
* @throws UnsupportedValue
*/
Expand Down
2 changes: 0 additions & 2 deletions src/Snippet/Parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +18,6 @@ final class Parts
* @return array<array<string, mixed>>
*
* @throws ClientExceptionInterface
* @throws PcreException
* @throws ServerError
* @throws UnsupportedValue
*/
Expand Down
2 changes: 0 additions & 2 deletions src/Snippet/ShowCreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,7 +13,6 @@ final class ShowCreateTable
{
/**
* @throws ClientExceptionInterface
* @throws PcreException
* @throws ServerError
*/
public static function run(ClickHouseClient $clickHouseClient, string $tableName): string
Expand Down
2 changes: 0 additions & 2 deletions src/Snippet/ShowDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,7 +17,6 @@ final class ShowDatabases
* @return list<string>
*
* @throws ClientExceptionInterface
* @throws PcreException
* @throws ServerError
*/
public static function run(ClickHouseClient $clickHouseClient): array
Expand Down
2 changes: 0 additions & 2 deletions src/Snippet/TableSizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +18,6 @@ final class TableSizes
* @return array<Entry>
*
* @throws ClientExceptionInterface
* @throws PcreException
* @throws ServerError
* @throws UnsupportedValue
*/
Expand Down
2 changes: 0 additions & 2 deletions src/Snippet/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,7 +13,6 @@ final class Version
{
/**
* @throws ClientExceptionInterface
* @throws PcreException
* @throws ServerError
*/
public static function run(ClickHouseClient $clickHouseClient): string
Expand Down
4 changes: 1 addition & 3 deletions src/Sql/SqlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,7 +22,6 @@ public function __construct(private ValueFormatter $valueFormatter)
/**
* @param array<string, mixed> $parameters
*
* @throws PcreException
* @throws UnsupportedValue
*/
public function createWithParameters(string $query, array $parameters): string
Expand Down
8 changes: 2 additions & 6 deletions src/Sql/ValueFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use BackedEnum;
use DateTimeImmutable;
use DateTimeZone;
use Safe\Exceptions\PcreException;
use SimPod\ClickHouseClient\Exception\UnsupportedValue;

use function array_key_first;
Expand All @@ -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 */
Expand All @@ -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)) {
Expand Down
8 changes: 4 additions & 4 deletions tests/Exception/UnsupportedValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
];
}
Expand Down Expand Up @@ -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',
Expand Down
16 changes: 10 additions & 6 deletions tests/Snippet/ShowCreateTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/Sql/ValueFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit e07f4a8

Please sign in to comment.