Skip to content

Commit

Permalink
added PHP 8 typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 22, 2022
1 parent bb6ab16 commit 361b0fe
Show file tree
Hide file tree
Showing 28 changed files with 72 additions and 108 deletions.
2 changes: 1 addition & 1 deletion src/Dibi/Bridges/Tracy/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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;
Expand Down
33 changes: 10 additions & 23 deletions src/Dibi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@ final public function isConnected(): bool
/**
* 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
Expand All @@ -212,21 +211,19 @@ final public function getDriver(): Driver

/**
* 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));
}


/**
* 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();
Expand All @@ -238,9 +235,8 @@ final public function translate(...$args): string

/**
* 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));
Expand All @@ -260,10 +256,9 @@ final public function test(...$args): bool

/**
* 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);
}
Expand Down Expand Up @@ -426,10 +421,7 @@ public function rollback(?string $savepoint = null): void
}


/**
* @return mixed
*/
public function transaction(callable $callback)
public function transaction(callable $callback): mixed
{
if ($this->transactionDepth === 0) {
$this->begin();
Expand Down Expand Up @@ -535,45 +527,40 @@ public function substitute(string $value): string

/**
* 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();
}


/**
* 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();
}


/**
* 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();
}


/**
* 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();
}
Expand Down
10 changes: 5 additions & 5 deletions src/Dibi/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __construct(string $sql, Connection $connection)
* @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;
Expand All @@ -71,7 +71,7 @@ public function select($col, ?string $as = null): self
/**
* 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
Expand All @@ -85,7 +85,7 @@ public function where($cond): self
* 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;
Expand All @@ -101,7 +101,7 @@ public function orderBy($row, string $direction = 'ASC'): self
/**
* 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;
Expand Down Expand Up @@ -151,7 +151,7 @@ public function fetch(): ?Row
* 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();
}
Expand Down
5 changes: 1 addition & 4 deletions src/Dibi/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ class DateTime extends \DateTimeImmutable
{
use Strict;

/**
* @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)) {
Expand Down
5 changes: 3 additions & 2 deletions src/Dibi/Drivers/DummyDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function rollback(?string $savepoint = null): void
}


public function getResource()
public function getResource(): mixed
{
return null;
}
Expand Down Expand Up @@ -171,8 +171,9 @@ public function free(): void
}


public function getResultResource()
public function getResultResource(): mixed
{
return null;
}


Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/FirebirdDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function inTransaction(): bool
* Returns the connection resource.
* @return resource|null
*/
public function getResource()
public function getResource(): mixed
{
return is_resource($this->connection) ? $this->connection : null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/FirebirdResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function free(): void
* Returns the result set resource.
* @return resource|null
*/
public function getResultResource()
public function getResultResource(): mixed
{
$this->autoFree = false;
return is_resource($this->resultSet) ? $this->resultSet : null;
Expand Down
5 changes: 1 addition & 4 deletions src/Dibi/Drivers/MySqliDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,7 @@ public function query(string $sql): ?Dibi\ResultDriver
}


/**
* @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);
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/NoDataResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function getResultColumns(): array
}


public function getResultResource()
public function getResultResource(): mixed
{
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/OdbcDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function inTransaction(): bool
* Returns the connection resource.
* @return resource|null
*/
public function getResource()
public function getResource(): mixed
{
return is_resource($this->connection) ? $this->connection : null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/OdbcResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function getResultColumns(): array
* Returns the result set resource.
* @return resource|null
*/
public function getResultResource()
public function getResultResource(): mixed
{
$this->autoFree = false;
return is_resource($this->resultSet) ? $this->resultSet : null;
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/OracleDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function rollback(?string $savepoint = null): void
* Returns the connection resource.
* @return resource|null
*/
public function getResource()
public function getResource(): mixed
{
return is_resource($this->connection) ? $this->connection : null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/OracleResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function getResultColumns(): array
* Returns the result set resource.
* @return resource|null
*/
public function getResultResource()
public function getResultResource(): mixed
{
$this->autoFree = false;
return is_resource($this->resultSet) ? $this->resultSet : null;
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/PostgreDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public function inTransaction(): bool
* 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
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/PostgreResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function getResultColumns(): array
* Returns the result set resource.
* @return resource|PgSql\Result|null
*/
public function getResultResource()
public function getResultResource(): mixed
{
$this->autoFree = false;
return is_resource($this->resultSet) || $this->resultSet instanceof PgSql\Result
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/SqlsrvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function rollback(?string $savepoint = null): void
* Returns the connection resource.
* @return resource|null
*/
public function getResource()
public function getResource(): mixed
{
return is_resource($this->connection) ? $this->connection : null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/SqlsrvResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function getResultColumns(): array
* Returns the result set resource.
* @return resource|null
*/
public function getResultResource()
public function getResultResource(): mixed
{
$this->autoFree = false;
return is_resource($this->resultSet) ? $this->resultSet : null;
Expand Down
5 changes: 1 addition & 4 deletions src/Dibi/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ public function __construct(Connection $connection, int $type, ?string $sql = nu
}


/**
* @param Result|DriverException|null $result
*/
public function done($result = null): self
public function done(Result|DriverException|null $result = null): static
{
$this->result = $result;
try {
Expand Down
Loading

0 comments on commit 361b0fe

Please sign in to comment.