1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-21 09:23:57 +01:00

added PHP 8 typehints

This commit is contained in:
David Grudl 2021-12-12 17:40:23 +01:00
parent 7acef0c34b
commit 08dfc37492
28 changed files with 72 additions and 106 deletions

View File

@ -26,7 +26,7 @@ class Panel implements Tracy\IBarPanel
private array $events = [];
public function __construct($explain = true, ?int $filter = null)
public function __construct(bool $explain = true, ?int $filter = null)
{
$this->filter = $filter ?: Event::QUERY;
$this->explain = $explain;

View File

@ -179,9 +179,8 @@ class Connection implements IConnection
/**
* Returns configuration variable. If no $key is passed, returns the entire array.
* @see self::__construct
* @return mixed
*/
final public function getConfig(?string $key = null, $default = null)
final public function getConfig(?string $key = null, $default = null): mixed
{
return $key === null
? $this->config
@ -204,10 +203,9 @@ class Connection implements IConnection
/**
* Generates (translates) and executes SQL query.
* @param mixed ...$args
* @throws Exception
*/
final public function query(...$args): Result
final public function query(mixed ...$args): Result
{
return $this->nativeQuery($this->translate(...$args));
}
@ -215,10 +213,9 @@ class Connection implements IConnection
/**
* Generates SQL query.
* @param mixed ...$args
* @throws Exception
*/
final public function translate(...$args): string
final public function translate(mixed ...$args): string
{
if (!$this->driver) {
$this->connect();
@ -230,9 +227,8 @@ class Connection implements IConnection
/**
* Generates and prints SQL query.
* @param mixed ...$args
*/
final public function test(...$args): bool
final public function test(mixed ...$args): bool
{
try {
Helpers::dump($this->translate(...$args));
@ -252,10 +248,9 @@ class Connection implements IConnection
/**
* Generates (translates) and returns SQL query as DataSource.
* @param mixed ...$args
* @throws Exception
*/
final public function dataSource(...$args): DataSource
final public function dataSource(mixed ...$args): DataSource
{
return new DataSource($this->translate(...$args), $this);
}
@ -418,10 +413,7 @@ class Connection implements IConnection
}
/**
* @return mixed
*/
public function transaction(callable $callback)
public function transaction(callable $callback): mixed
{
if ($this->transactionDepth === 0) {
$this->begin();
@ -527,10 +519,9 @@ class Connection implements IConnection
/**
* Executes SQL query and fetch result - shortcut for query() & fetch().
* @param mixed ...$args
* @throws Exception
*/
public function fetch(...$args): ?Row
public function fetch(mixed ...$args): ?Row
{
return $this->query($args)->fetch();
}
@ -538,11 +529,10 @@ class Connection implements IConnection
/**
* Executes SQL query and fetch results - shortcut for query() & fetchAll().
* @param mixed ...$args
* @return Row[]|array[]
* @throws Exception
*/
public function fetchAll(...$args): array
public function fetchAll(mixed ...$args): array
{
return $this->query($args)->fetchAll();
}
@ -550,11 +540,9 @@ class Connection implements IConnection
/**
* Executes SQL query and fetch first column - shortcut for query() & fetchSingle().
* @param mixed ...$args
* @return mixed
* @throws Exception
*/
public function fetchSingle(...$args)
public function fetchSingle(mixed ...$args): mixed
{
return $this->query($args)->fetchSingle();
}
@ -562,10 +550,9 @@ class Connection implements IConnection
/**
* Executes SQL query and fetch pairs - shortcut for query() & fetchPairs().
* @param mixed ...$args
* @throws Exception
*/
public function fetchPairs(...$args): array
public function fetchPairs(mixed ...$args): array
{
return $this->query($args)->fetchPairs();
}

View File

@ -44,7 +44,7 @@ class DataSource implements IDataSource
* @param string|array $col column name or array of column names
* @param string $as column alias
*/
public function select($col, ?string $as = null): self
public function select(string|array $col, ?string $as = null): static
{
if (is_array($col)) {
$this->cols = $col;
@ -60,7 +60,7 @@ class DataSource implements IDataSource
/**
* Adds conditions to query.
*/
public function where($cond): self
public function where($cond): static
{
$this->conds[] = is_array($cond)
? $cond // TODO: not consistent with select and orderBy
@ -74,7 +74,7 @@ class DataSource implements IDataSource
* Selects columns to order by.
* @param string|array $row column name or array of column names
*/
public function orderBy($row, string $direction = 'ASC'): self
public function orderBy(string|array $row, string $direction = 'ASC'): static
{
if (is_array($row)) {
$this->sorting = $row;
@ -90,7 +90,7 @@ class DataSource implements IDataSource
/**
* Limits number of rows.
*/
public function applyLimit(int $limit, ?int $offset = null): self
public function applyLimit(int $limit, ?int $offset = null): static
{
$this->limit = $limit;
$this->offset = $offset;
@ -140,7 +140,7 @@ class DataSource implements IDataSource
* Like fetch(), but returns only first field.
* @return mixed value on success, null if no next record
*/
public function fetchSingle()
public function fetchSingle(): mixed
{
return $this->getResult()->fetchSingle();
}

View File

@ -15,10 +15,7 @@ namespace Dibi;
*/
class DateTime extends \DateTimeImmutable
{
/**
* @param string|int $time
*/
public function __construct($time = 'now', ?\DateTimeZone $timezone = null)
public function __construct(string|int $time = 'now', ?\DateTimeZone $timezone = null)
{
$timezone = $timezone ?: new \DateTimeZone(date_default_timezone_get());
if (is_numeric($time)) {

View File

@ -55,7 +55,7 @@ class DummyDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
}
public function getResource()
public function getResource(): mixed
{
return null;
}
@ -169,8 +169,9 @@ class DummyDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
}
public function getResultResource()
public function getResultResource(): mixed
{
return null;
}

View File

@ -187,7 +187,7 @@ class FirebirdDriver implements Dibi\Driver
* Returns the connection resource.
* @return resource|null
*/
public function getResource()
public function getResource(): mixed
{
return is_resource($this->connection) ? $this->connection : null;
}

View File

@ -87,7 +87,7 @@ class FirebirdResult implements Dibi\ResultDriver
* Returns the result set resource.
* @return resource|null
*/
public function getResultResource()
public function getResultResource(): mixed
{
return is_resource($this->resultSet) ? $this->resultSet : null;
}

View File

@ -152,10 +152,7 @@ class MySqliDriver implements Dibi\Driver
}
/**
* @param int|string $code
*/
public static function createException(string $message, $code, string $sql): Dibi\DriverException
public static function createException(string $message, int|string $code, string $sql): Dibi\DriverException
{
if (in_array($code, [1216, 1217, 1451, 1452, 1701], true)) {
return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql);

View File

@ -58,7 +58,7 @@ class NoDataResult implements Dibi\ResultDriver
}
public function getResultResource()
public function getResultResource(): mixed
{
return null;
}

View File

@ -166,7 +166,7 @@ class OdbcDriver implements Dibi\Driver
* Returns the connection resource.
* @return resource|null
*/
public function getResource()
public function getResource(): mixed
{
return is_resource($this->connection) ? $this->connection : null;
}

View File

@ -109,7 +109,7 @@ class OdbcResult implements Dibi\ResultDriver
* Returns the result set resource.
* @return resource|null
*/
public function getResultResource()
public function getResultResource(): mixed
{
return is_resource($this->resultSet) ? $this->resultSet : null;
}

View File

@ -180,7 +180,7 @@ class OracleDriver implements Dibi\Driver
* Returns the connection resource.
* @return resource|null
*/
public function getResource()
public function getResource(): mixed
{
return is_resource($this->connection) ? $this->connection : null;
}

View File

@ -93,7 +93,7 @@ class OracleResult implements Dibi\ResultDriver
* Returns the result set resource.
* @return resource|null
*/
public function getResultResource()
public function getResultResource(): mixed
{
return is_resource($this->resultSet) ? $this->resultSet : null;
}

View File

@ -224,7 +224,7 @@ class PostgreDriver implements Dibi\Driver
* Returns the connection resource.
* @return resource|null
*/
public function getResource()
public function getResource(): mixed
{
return is_resource($this->connection) || $this->connection instanceof PgSql\Connection
? $this->connection

View File

@ -96,7 +96,7 @@ class PostgreResult implements Dibi\ResultDriver
* Returns the result set resource.
* @return resource|PgSql\Result|null
*/
public function getResultResource()
public function getResultResource(): mixed
{
return is_resource($this->resultSet) || $this->resultSet instanceof PgSql\Result
? $this->resultSet

View File

@ -164,7 +164,7 @@ class SqlsrvDriver implements Dibi\Driver
* Returns the connection resource.
* @return resource|null
*/
public function getResource()
public function getResource(): mixed
{
return is_resource($this->connection) ? $this->connection : null;
}

View File

@ -89,7 +89,7 @@ class SqlsrvResult implements Dibi\ResultDriver
* Returns the result set resource.
* @return resource|null
*/
public function getResultResource()
public function getResultResource(): mixed
{
return is_resource($this->resultSet) ? $this->resultSet : null;
}

View File

@ -71,10 +71,7 @@ class Event
}
/**
* @param Result|DriverException|null $result
*/
public function done($result = null): self
public function done(Result|DriverException|null $result = null): static
{
$this->result = $result;
try {

View File

@ -115,7 +115,7 @@ class Fluent implements IDataSource
/**
* Appends new argument to the clause.
*/
public function __call(string $clause, array $args): self
public function __call(string $clause, array $args): static
{
$clause = self::$normalizer->$clause;
@ -202,7 +202,7 @@ class Fluent implements IDataSource
/**
* Switch to a clause.
*/
public function clause(string $clause): self
public function clause(string $clause): static
{
$this->cursor = &$this->clauses[self::$normalizer->$clause];
if ($this->cursor === null) {
@ -216,7 +216,7 @@ class Fluent implements IDataSource
/**
* Removes a clause.
*/
public function removeClause(string $clause): self
public function removeClause(string $clause): static
{
$this->clauses[self::$normalizer->$clause] = null;
return $this;
@ -226,7 +226,7 @@ class Fluent implements IDataSource
/**
* Change a SQL flag.
*/
public function setFlag(string $flag, bool $value = true): self
public function setFlag(string $flag, bool $value = true): static
{
$flag = strtoupper($flag);
if ($value) {
@ -266,7 +266,7 @@ class Fluent implements IDataSource
/**
* Adds Result setup.
*/
public function setupResult(string $method): self
public function setupResult(string $method): static
{
$this->setups[] = func_get_args();
return $this;
@ -278,10 +278,10 @@ class Fluent implements IDataSource
/**
* Generates and executes SQL query.
* @return Result|int|null result set or number of affected rows
* Returns result set or number of affected rows
* @throws Exception
*/
public function execute(?string $return = null)
public function execute(?string $return = null): Result|int|null
{
$res = $this->query($this->_export());
switch ($return) {
@ -297,9 +297,8 @@ class Fluent implements IDataSource
/**
* Generates, executes SQL query and fetches the single row.
* @return Row|array|null
*/
public function fetch()
public function fetch(): Row|array|null
{
return $this->command === 'SELECT' && !$this->clauses['LIMIT']
? $this->query($this->_export(null, ['%lmt', 1]))->fetch()
@ -309,9 +308,9 @@ class Fluent implements IDataSource
/**
* Like fetch(), but returns only first field.
* @return mixed value on success, null if no next record
* Returns value on success, null if no next record
*/
public function fetchSingle()
public function fetchSingle(): mixed
{
return $this->command === 'SELECT' && !$this->clauses['LIMIT']
? $this->query($this->_export(null, ['%lmt', 1]))->fetchSingle()

View File

@ -17,9 +17,8 @@ class Helpers
/**
* Prints out a syntax highlighted version of the SQL command or Result.
* @param string|Result $sql
*/
public static function dump($sql = null, bool $return = false): ?string
public static function dump(string|Result|null $sql = null, bool $return = false): ?string
{
ob_start();
if ($sql instanceof Result && PHP_SAPI === 'cli') {
@ -235,7 +234,7 @@ class Helpers
/**
* Import SQL dump from file.
* @return int count of sql commands
* Returns count of sql commands
*/
public static function loadFromFile(Connection $connection, string $file, ?callable $onProgress = null): int
{
@ -283,14 +282,14 @@ class Helpers
/** @internal */
public static function false2Null($val)
public static function false2Null(mixed $val): mixed
{
return $val === false ? null : $val;
}
/** @internal */
public static function intVal($value): int
public static function intVal(mixed $value): int
{
if (is_int($value)) {
return $value;

View File

@ -107,15 +107,13 @@ class Column
}
/** @return mixed */
public function getDefault()
public function getDefault(): mixed
{
return $this->info['default'] ?? null;
}
/** @return mixed */
public function getVendorInfo(string $key)
public function getVendorInfo(string $key): mixed
{
return $this->info['vendor'][$key] ?? null;
}

View File

@ -126,7 +126,7 @@ class Result implements IDataSource
/**
* Set fetched object class. This class should extend the Row class.
*/
public function setRowClass(?string $class): self
public function setRowClass(?string $class): static
{
$this->rowClass = $class;
return $this;
@ -145,7 +145,7 @@ class Result implements IDataSource
/**
* Set a factory to create fetched object instances. These should extend the Row class.
*/
public function setRowFactory(callable $callback): self
public function setRowFactory(callable $callback): static
{
$this->rowFactory = $callback;
return $this;
@ -155,9 +155,8 @@ class Result implements IDataSource
/**
* Fetches the row at current position, process optional type conversion.
* and moves the internal cursor to the next position
* @return Row|array|null
*/
final public function fetch()
final public function fetch(): mixed
{
$row = $this->getResultDriver()->fetch(true);
if ($row === null) {
@ -178,9 +177,9 @@ class Result implements IDataSource
/**
* Like fetch(), but returns only first field.
* @return mixed value on success, null if no next record
* Returns value on success, null if no next record
*/
final public function fetchSingle()
final public function fetchSingle(): mixed
{
$row = $this->getResultDriver()->fetch(true);
if ($row === null) {
@ -522,7 +521,7 @@ class Result implements IDataSource
* Define column type.
* @param string|null $type use constant Type::*
*/
final public function setType(string $column, ?string $type): self
final public function setType(string $column, ?string $type): static
{
$this->types[$column] = $type;
return $this;
@ -550,7 +549,7 @@ class Result implements IDataSource
/**
* Sets type format.
*/
final public function setFormat(string $type, ?string $format): self
final public function setFormat(string $type, ?string $format): static
{
$this->formats[$type] = $format;
return $this;
@ -560,7 +559,7 @@ class Result implements IDataSource
/**
* Sets type formats.
*/
final public function setFormats(array $formats): self
final public function setFormats(array $formats): static
{
$this->formats = $formats;
return $this;

View File

@ -39,10 +39,9 @@ class ResultIterator implements \Iterator, \Countable
/**
* Returns the key of the current element.
* @return mixed
*/
#[\ReturnTypeWillChange]
public function key()
public function key(): mixed
{
return $this->pointer;
}
@ -50,10 +49,9 @@ class ResultIterator implements \Iterator, \Countable
/**
* Returns the current element.
* @return mixed
*/
#[\ReturnTypeWillChange]
public function current()
public function current(): mixed
{
return $this->row;
}

View File

@ -32,9 +32,8 @@ class Row implements \ArrayAccess, \IteratorAggregate, \Countable
/**
* Converts value to DateTime object.
* @return DateTime|string|null
*/
public function asDateTime(string $key, ?string $format = null)
public function asDateTime(string $key, ?string $format = null): DateTime|string|null
{
$time = $this[$key];
if (!$time instanceof DateTime) {
@ -83,8 +82,7 @@ class Row implements \ArrayAccess, \IteratorAggregate, \Countable
}
#[\ReturnTypeWillChange]
final public function offsetGet($nm)
final public function offsetGet($nm): mixed
{
return $this->$nm;
}

View File

@ -151,9 +151,8 @@ final class Translator
/**
* Apply modifier to single value.
* @param mixed $value
*/
public function formatValue($value, ?string $modifier): string
public function formatValue(mixed $value, ?string $modifier): string
{
if ($this->comment) {
return '...';

View File

@ -85,7 +85,7 @@ class dibi
* @param array $config connection parameters
* @throws Dibi\Exception
*/
public static function connect($config = [], string $name = '0'): Dibi\Connection
public static function connect(array $config = [], string $name = '0'): Dibi\Connection
{
return self::$connection = self::$registry[$name] = new Dibi\Connection($config, $name);
}
@ -148,10 +148,9 @@ class dibi
/**
* Prints out a syntax highlighted version of the SQL command or Result.
* @param string|Dibi\Result $sql
* @param bool $return return output instead of printing it?
*/
public static function dump($sql = null, bool $return = false): ?string
public static function dump(string|Dibi\Result|null $sql = null, bool $return = false): ?string
{
return Dibi\Helpers::dump($sql, $return);
}

View File

@ -18,11 +18,12 @@ class Exception extends \Exception
private ?string $sql;
/**
* @param int|string $code
*/
public function __construct(string $message = '', $code = 0, ?string $sql = null, ?\Throwable $previous = null)
{
public function __construct(
string $message = '',
int|string $code = 0,
?string $sql = null,
?\Throwable $previous = null,
) {
parent::__construct($message, 0, $previous);
$this->code = $code;
$this->sql = $sql;

View File

@ -67,9 +67,8 @@ interface Driver
/**
* Returns the connection resource.
* @return mixed
*/
function getResource();
function getResource(): mixed;
/**
* Returns the connection reflector.
@ -117,7 +116,6 @@ interface ResultDriver
/**
* Moves cursor position without fetching row.
* @return bool true on success, false if unable to seek to specified record
* @throws Exception
*/
function seek(int $row): bool;
@ -142,9 +140,8 @@ interface ResultDriver
/**
* Returns the result set resource.
* @return mixed
*/
function getResultResource();
function getResultResource(): mixed;
/**
* Decodes data from result set.