-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Psalm's "other issues found" down by 40 or so.
- Loading branch information
Showing
58 changed files
with
1,099 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Database\Exceptions; | ||
|
||
use Exception; | ||
|
||
abstract class TypedDatabaseException extends Exception | ||
{ | ||
} |
16 changes: 16 additions & 0 deletions
16
site/app/Database/Exceptions/TypedDatabaseTypeException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Database\Exceptions; | ||
|
||
use Throwable; | ||
|
||
class TypedDatabaseTypeException extends TypedDatabaseException | ||
{ | ||
|
||
public function __construct(string $expectedType, mixed $value, ?Throwable $previous = null) | ||
{ | ||
parent::__construct(sprintf('%s expected, %s given', $expectedType, get_debug_type($value)), previous: $previous); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Database; | ||
|
||
use JetBrains\PhpStorm\Language; | ||
use MichalSpacekCz\Database\Exceptions\TypedDatabaseTypeException; | ||
use Nette\Database\Explorer; | ||
use Nette\Utils\DateTime; | ||
|
||
readonly class TypedDatabase | ||
{ | ||
|
||
public function __construct( | ||
private Explorer $database, | ||
) { | ||
} | ||
|
||
|
||
/** | ||
* @param literal-string $sql | ||
* @param array<array-key, mixed> $params | ||
* @return array<string, string> | ||
*/ | ||
public function fetchPairsStringString(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): array | ||
{ | ||
$values = []; | ||
/** | ||
* @var mixed $value | ||
*/ | ||
foreach ($this->database->fetchPairs($sql, ...$params) as $key => $value) { | ||
if (!is_string($key)) { | ||
throw new TypedDatabaseTypeException('string', $key); | ||
} elseif (!is_string($value)) { | ||
throw new TypedDatabaseTypeException('string', $value); | ||
} | ||
$values[$key] = $value; | ||
} | ||
return $values; | ||
} | ||
|
||
|
||
/** | ||
* @param literal-string $sql | ||
* @param array<array-key, mixed> $params | ||
* @return array<int, string> | ||
*/ | ||
public function fetchPairsIntString(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): array | ||
{ | ||
$values = []; | ||
/** | ||
* @var mixed $value | ||
*/ | ||
foreach ($this->database->fetchPairs($sql, ...$params) as $key => $value) { | ||
if (!is_int($key)) { | ||
throw new TypedDatabaseTypeException('int', $key); | ||
} elseif (!is_string($value)) { | ||
throw new TypedDatabaseTypeException('string', $value); | ||
} | ||
$values[$key] = $value; | ||
} | ||
return $values; | ||
} | ||
|
||
|
||
/** | ||
* @param literal-string $sql | ||
* @param array<array-key, mixed> $params | ||
* @return list<DateTime> | ||
*/ | ||
public function fetchPairsListDateTime(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): array | ||
{ | ||
$values = []; | ||
foreach ($this->database->fetchPairs($sql, ...$params) as $value) { | ||
if (!$value instanceof DateTime) { | ||
throw new TypedDatabaseTypeException(DateTime::class, $value); | ||
} | ||
$values[] = $value; | ||
} | ||
return $values; | ||
} | ||
|
||
|
||
/** | ||
* @param literal-string $sql | ||
* @param array<array-key, mixed> $params | ||
*/ | ||
public function fetchFieldString(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): string | ||
{ | ||
$field = $this->database->fetchField($sql, ...$params); | ||
if (!is_string($field)) { | ||
throw new TypedDatabaseTypeException('string', $field); | ||
} | ||
return $field; | ||
} | ||
|
||
|
||
/** | ||
* @param literal-string $sql | ||
* @param array<array-key, mixed> $params | ||
*/ | ||
public function fetchFieldStringNullable(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): ?string | ||
{ | ||
$field = $this->database->fetchField($sql, ...$params); | ||
if (!is_string($field) && !is_null($field)) { | ||
throw new TypedDatabaseTypeException('string|null', $field); | ||
} | ||
return $field; | ||
} | ||
|
||
|
||
/** | ||
* @param literal-string $sql | ||
* @param array<array-key, mixed> $params | ||
*/ | ||
public function fetchFieldInt(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): int | ||
{ | ||
$field = $this->database->fetchField($sql, ...$params); | ||
if (!is_int($field)) { | ||
throw new TypedDatabaseTypeException('int', $field); | ||
} | ||
return $field; | ||
} | ||
|
||
|
||
/** | ||
* @param literal-string $sql | ||
* @param array<array-key, mixed> $params | ||
*/ | ||
public function fetchFieldIntNullable(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): ?int | ||
{ | ||
$field = $this->database->fetchField($sql, ...$params); | ||
if (!is_int($field) && !is_null($field)) { | ||
throw new TypedDatabaseTypeException('int|null', $field); | ||
} | ||
return $field; | ||
} | ||
|
||
|
||
/** | ||
* @param literal-string $sql | ||
* @param array<array-key, mixed> $params | ||
*/ | ||
public function fetchFieldDateTime(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): DateTime | ||
{ | ||
$field = $this->database->fetchField($sql, ...$params); | ||
if (!$field instanceof DateTime) { | ||
throw new TypedDatabaseTypeException(DateTime::class, $field); | ||
} | ||
return $field; | ||
} | ||
|
||
|
||
/** | ||
* @param literal-string $sql | ||
* @param array<array-key, mixed> $params | ||
*/ | ||
public function fetchFieldDateTimeNullable(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): ?DateTime | ||
{ | ||
$field = $this->database->fetchField($sql, ...$params); | ||
if (!$field instanceof DateTime && !is_null($field)) { | ||
throw new TypedDatabaseTypeException(DateTime::class . '|null', $field); | ||
} | ||
return $field; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.