1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-08 07:06:52 +02:00

added PHP 8 typehints

This commit is contained in:
David Grudl
2021-12-12 17:40:23 +01:00
parent 0575f9ea17
commit 94df6db03d
28 changed files with 72 additions and 108 deletions

View File

@@ -32,7 +32,7 @@ class Panel implements Tracy\IBarPanel
private array $events = []; 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->filter = $filter ?: Event::QUERY;
$this->explain = $explain; $this->explain = $explain;

View File

@@ -187,9 +187,8 @@ class Connection implements IConnection
/** /**
* Returns configuration variable. If no $key is passed, returns the entire array. * Returns configuration variable. If no $key is passed, returns the entire array.
* @see self::__construct * @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 return $key === null
? $this->config ? $this->config
@@ -212,10 +211,9 @@ class Connection implements IConnection
/** /**
* Generates (translates) and executes SQL query. * Generates (translates) and executes SQL query.
* @param mixed ...$args
* @throws Exception * @throws Exception
*/ */
final public function query(...$args): Result final public function query(mixed ...$args): Result
{ {
return $this->nativeQuery($this->translate(...$args)); return $this->nativeQuery($this->translate(...$args));
} }
@@ -223,10 +221,9 @@ class Connection implements IConnection
/** /**
* Generates SQL query. * Generates SQL query.
* @param mixed ...$args
* @throws Exception * @throws Exception
*/ */
final public function translate(...$args): string final public function translate(mixed ...$args): string
{ {
if (!$this->driver) { if (!$this->driver) {
$this->connect(); $this->connect();
@@ -238,9 +235,8 @@ class Connection implements IConnection
/** /**
* Generates and prints SQL query. * Generates and prints SQL query.
* @param mixed ...$args
*/ */
final public function test(...$args): bool final public function test(mixed ...$args): bool
{ {
try { try {
Helpers::dump($this->translate(...$args)); Helpers::dump($this->translate(...$args));
@@ -260,10 +256,9 @@ class Connection implements IConnection
/** /**
* Generates (translates) and returns SQL query as DataSource. * Generates (translates) and returns SQL query as DataSource.
* @param mixed ...$args
* @throws Exception * @throws Exception
*/ */
final public function dataSource(...$args): DataSource final public function dataSource(mixed ...$args): DataSource
{ {
return new DataSource($this->translate(...$args), $this); return new DataSource($this->translate(...$args), $this);
} }
@@ -426,10 +421,7 @@ class Connection implements IConnection
} }
/** public function transaction(callable $callback): mixed
* @return mixed
*/
public function transaction(callable $callback)
{ {
if ($this->transactionDepth === 0) { if ($this->transactionDepth === 0) {
$this->begin(); $this->begin();
@@ -535,10 +527,9 @@ class Connection implements IConnection
/** /**
* Executes SQL query and fetch result - shortcut for query() & fetch(). * Executes SQL query and fetch result - shortcut for query() & fetch().
* @param mixed ...$args
* @throws Exception * @throws Exception
*/ */
public function fetch(...$args): ?Row public function fetch(mixed ...$args): ?Row
{ {
return $this->query($args)->fetch(); return $this->query($args)->fetch();
} }
@@ -546,11 +537,10 @@ class Connection implements IConnection
/** /**
* Executes SQL query and fetch results - shortcut for query() & fetchAll(). * Executes SQL query and fetch results - shortcut for query() & fetchAll().
* @param mixed ...$args
* @return Row[]|array[] * @return Row[]|array[]
* @throws Exception * @throws Exception
*/ */
public function fetchAll(...$args): array public function fetchAll(mixed ...$args): array
{ {
return $this->query($args)->fetchAll(); return $this->query($args)->fetchAll();
} }
@@ -558,11 +548,9 @@ class Connection implements IConnection
/** /**
* Executes SQL query and fetch first column - shortcut for query() & fetchSingle(). * Executes SQL query and fetch first column - shortcut for query() & fetchSingle().
* @param mixed ...$args
* @return mixed
* @throws Exception * @throws Exception
*/ */
public function fetchSingle(...$args) public function fetchSingle(mixed ...$args): mixed
{ {
return $this->query($args)->fetchSingle(); return $this->query($args)->fetchSingle();
} }
@@ -570,10 +558,9 @@ class Connection implements IConnection
/** /**
* Executes SQL query and fetch pairs - shortcut for query() & fetchPairs(). * Executes SQL query and fetch pairs - shortcut for query() & fetchPairs().
* @param mixed ...$args
* @throws Exception * @throws Exception
*/ */
public function fetchPairs(...$args): array public function fetchPairs(mixed ...$args): array
{ {
return $this->query($args)->fetchPairs(); return $this->query($args)->fetchPairs();
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -120,7 +120,7 @@ class Fluent implements IDataSource
/** /**
* Appends new argument to the clause. * 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; $clause = self::$normalizer->$clause;
@@ -207,7 +207,7 @@ class Fluent implements IDataSource
/** /**
* Switch to a clause. * Switch to a clause.
*/ */
public function clause(string $clause): self public function clause(string $clause): static
{ {
$this->cursor = &$this->clauses[self::$normalizer->$clause]; $this->cursor = &$this->clauses[self::$normalizer->$clause];
if ($this->cursor === null) { if ($this->cursor === null) {
@@ -221,7 +221,7 @@ class Fluent implements IDataSource
/** /**
* Removes a clause. * Removes a clause.
*/ */
public function removeClause(string $clause): self public function removeClause(string $clause): static
{ {
$this->clauses[self::$normalizer->$clause] = null; $this->clauses[self::$normalizer->$clause] = null;
return $this; return $this;
@@ -231,7 +231,7 @@ class Fluent implements IDataSource
/** /**
* Change a SQL flag. * 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); $flag = strtoupper($flag);
if ($value) { if ($value) {
@@ -271,7 +271,7 @@ class Fluent implements IDataSource
/** /**
* Adds Result setup. * Adds Result setup.
*/ */
public function setupResult(string $method): self public function setupResult(string $method): static
{ {
$this->setups[] = func_get_args(); $this->setups[] = func_get_args();
return $this; return $this;
@@ -283,10 +283,10 @@ class Fluent implements IDataSource
/** /**
* Generates and executes SQL query. * 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 * @throws Exception
*/ */
public function execute(?string $return = null) public function execute(?string $return = null): Result|int|null
{ {
$res = $this->query($this->_export()); $res = $this->query($this->_export());
switch ($return) { switch ($return) {
@@ -302,9 +302,8 @@ class Fluent implements IDataSource
/** /**
* Generates, executes SQL query and fetches the single row. * 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'] return $this->command === 'SELECT' && !$this->clauses['LIMIT']
? $this->query($this->_export(null, ['%lmt', 1]))->fetch() ? $this->query($this->_export(null, ['%lmt', 1]))->fetch()
@@ -314,9 +313,9 @@ class Fluent implements IDataSource
/** /**
* Like fetch(), but returns only first field. * 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'] return $this->command === 'SELECT' && !$this->clauses['LIMIT']
? $this->query($this->_export(null, ['%lmt', 1]))->fetchSingle() ? $this->query($this->_export(null, ['%lmt', 1]))->fetchSingle()

View File

@@ -19,9 +19,8 @@ class Helpers
/** /**
* Prints out a syntax highlighted version of the SQL command or Result. * 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(); ob_start();
if ($sql instanceof Result && PHP_SAPI === 'cli') { if ($sql instanceof Result && PHP_SAPI === 'cli') {
@@ -237,7 +236,7 @@ class Helpers
/** /**
* Import SQL dump from file. * 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 public static function loadFromFile(Connection $connection, string $file, ?callable $onProgress = null): int
{ {
@@ -285,14 +284,14 @@ class Helpers
/** @internal */ /** @internal */
public static function false2Null($val) public static function false2Null(mixed $val): mixed
{ {
return $val === false ? null : $val; return $val === false ? null : $val;
} }
/** @internal */ /** @internal */
public static function intVal($value): int public static function intVal(mixed $value): int
{ {
if (is_int($value)) { if (is_int($value)) {
return $value; return $value;

View File

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

View File

@@ -130,7 +130,7 @@ class Result implements IDataSource
/** /**
* Set fetched object class. This class should extend the Row class. * 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; $this->rowClass = $class;
return $this; return $this;
@@ -149,7 +149,7 @@ class Result implements IDataSource
/** /**
* Set a factory to create fetched object instances. These should extend the Row class. * 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; $this->rowFactory = $callback;
return $this; return $this;
@@ -159,9 +159,8 @@ class Result implements IDataSource
/** /**
* Fetches the row at current position, process optional type conversion. * Fetches the row at current position, process optional type conversion.
* and moves the internal cursor to the next position * 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); $row = $this->getResultDriver()->fetch(true);
if ($row === null) { if ($row === null) {
@@ -182,9 +181,9 @@ class Result implements IDataSource
/** /**
* Like fetch(), but returns only first field. * 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); $row = $this->getResultDriver()->fetch(true);
if ($row === null) { if ($row === null) {
@@ -526,7 +525,7 @@ class Result implements IDataSource
* Define column type. * Define column type.
* @param string|null $type use constant 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; $this->types[$column] = $type;
return $this; return $this;
@@ -554,7 +553,7 @@ class Result implements IDataSource
/** /**
* Sets type format. * 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; $this->formats[$type] = $format;
return $this; return $this;
@@ -564,7 +563,7 @@ class Result implements IDataSource
/** /**
* Sets type formats. * Sets type formats.
*/ */
final public function setFormats(array $formats): self final public function setFormats(array $formats): static
{ {
$this->formats = $formats; $this->formats = $formats;
return $this; return $this;

View File

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

View File

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

View File

@@ -162,9 +162,8 @@ final class Translator
/** /**
* Apply modifier to single value. * 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) { if ($this->comment) {
return '...'; return '...';

View File

@@ -87,7 +87,7 @@ class dibi
* @param array $config connection parameters * @param array $config connection parameters
* @throws Dibi\Exception * @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); return self::$connection = self::$registry[$name] = new Dibi\Connection($config, $name);
} }
@@ -150,10 +150,9 @@ class dibi
/** /**
* Prints out a syntax highlighted version of the SQL command or Result. * 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? * @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); return Dibi\Helpers::dump($sql, $return);
} }

View File

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

View File

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