1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-29 08:49:50 +02:00

type fixes

This commit is contained in:
David Grudl
2018-04-17 10:38:07 +02:00
parent ab7683a3d2
commit 5a2332899b
19 changed files with 46 additions and 54 deletions

View File

@@ -23,7 +23,7 @@ class DibiExtension22 extends Nette\DI\CompilerExtension
private $debugMode; private $debugMode;
public function __construct(bool $debugMode = null) public function __construct(bool $debugMode = false)
{ {
$this->debugMode = $debugMode; $this->debugMode = $debugMode;
} }

View File

@@ -25,7 +25,7 @@ class Panel implements Tracy\IBarPanel
/** @var int maximum SQL length */ /** @var int maximum SQL length */
public static $maxLength = 1000; public static $maxLength = 1000;
/** @var bool explain queries? */ /** @var bool|string explain queries? */
public $explain; public $explain;
/** @var int */ /** @var int */
@@ -35,7 +35,7 @@ class Panel implements Tracy\IBarPanel
private $events = []; private $events = [];
public function __construct(bool $explain = true, int $filter = null) public function __construct($explain = true, int $filter = null)
{ {
$this->filter = $filter ?: Event::QUERY; $this->filter = $filter ?: Event::QUERY;
$this->explain = $explain; $this->explain = $explain;

View File

@@ -31,7 +31,7 @@ class Connection implements IConnection
/** @var Driver */ /** @var Driver */
private $driver; private $driver;
/** @var Translator */ /** @var Translator|null */
private $translator; private $translator;
/** @var bool Is connected? */ /** @var bool Is connected? */
@@ -50,7 +50,7 @@ class Connection implements IConnection
* - run (bool) => enable profiler? * - run (bool) => enable profiler?
* - file => file to log * - file => file to log
* - substitutes (array) => map of driver specific substitutes (under development) * - substitutes (array) => map of driver specific substitutes (under development)
* @param mixed $config connection parameters * @param array $config connection parameters
* @throws Exception * @throws Exception
*/ */
public function __construct($config, string $name = null) public function __construct($config, string $name = null)
@@ -198,7 +198,7 @@ class Connection implements IConnection
/** /**
* Generates (translates) and executes SQL query. * Generates (translates) and executes SQL query.
* @param mixed $args * @param mixed ...$args
* @return Result|int result set or number of affected rows * @return Result|int result set or number of affected rows
* @throws Exception * @throws Exception
*/ */
@@ -210,7 +210,7 @@ class Connection implements IConnection
/** /**
* Generates SQL query. * Generates SQL query.
* @param mixed $args * @param mixed ...$args
* @throws Exception * @throws Exception
*/ */
final public function translate(...$args): string final public function translate(...$args): string
@@ -221,7 +221,7 @@ class Connection implements IConnection
/** /**
* Generates and prints SQL query. * Generates and prints SQL query.
* @param mixed $args * @param mixed ...$args
*/ */
final public function test(...$args): bool final public function test(...$args): bool
{ {
@@ -242,7 +242,7 @@ class Connection implements IConnection
/** /**
* Generates (translates) and returns SQL query as DataSource. * Generates (translates) and returns SQL query as DataSource.
* @param mixed $args * @param mixed ...$args
* @throws Exception * @throws Exception
*/ */
final public function dataSource(...$args): DataSource final public function dataSource(...$args): DataSource
@@ -267,7 +267,7 @@ class Connection implements IConnection
/** /**
* Executes the SQL query. * Executes the SQL query.
* @return Result|int result set or number of affected rows * @return Result|int|null result set or number of affected rows
* @throws Exception * @throws Exception
*/ */
final public function nativeQuery(string $sql) final public function nativeQuery(string $sql)
@@ -425,21 +425,16 @@ class Connection implements IConnection
} }
public function update(string $table, array $args): Fluent public function update(string $table, iterable $args): Fluent
{ {
if (!(is_array($args) || $args instanceof Traversable)) {
throw new \InvalidArgumentException('Arguments must be array or Traversable.');
}
return $this->command()->update('%n', $table)->set($args); return $this->command()->update('%n', $table)->set($args);
} }
public function insert(string $table, array $args): Fluent public function insert(string $table, iterable $args): Fluent
{ {
if ($args instanceof Traversable) { if ($args instanceof Traversable) {
$args = iterator_to_array($args); $args = iterator_to_array($args);
} elseif (!is_array($args)) {
throw new \InvalidArgumentException('Arguments must be array or Traversable.');
} }
return $this->command()->insert() return $this->command()->insert()
->into('%n', $table, '(%n)', array_keys($args))->values('%l', $args); ->into('%n', $table, '(%n)', array_keys($args))->values('%l', $args);
@@ -480,7 +475,7 @@ 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 * @param mixed ...$args
* @throws Exception * @throws Exception
*/ */
public function fetch(...$args): ?Row public function fetch(...$args): ?Row
@@ -491,8 +486,8 @@ 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 * @param mixed ...$args
* @return Row[] * @return Row[]|array[]
* @throws Exception * @throws Exception
*/ */
public function fetchAll(...$args): array public function fetchAll(...$args): array
@@ -503,7 +498,7 @@ 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 * @param mixed ...$args
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
@@ -515,7 +510,7 @@ 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 * @param mixed ...$args
* @throws Exception * @throws Exception
*/ */
public function fetchPairs(...$args): array public function fetchPairs(...$args): array

View File

@@ -23,13 +23,13 @@ class DataSource implements IDataSource
/** @var string */ /** @var string */
private $sql; private $sql;
/** @var Result */ /** @var Result|null */
private $result; private $result;
/** @var int */ /** @var int|null */
private $count; private $count;
/** @var int */ /** @var int|null */
private $totalCount; private $totalCount;
/** @var array */ /** @var array */

View File

@@ -53,7 +53,7 @@ class MsSqlReflector implements Dibi\Reflector
/** /**
* Returns count of rows in a table * Returns count of rows in a table
*/ */
public function getTableCount(string $table, bool $fallback = true): int public function getTableCount(string $table, bool $fallback = true): ?int
{ {
if (empty($table)) { if (empty($table)) {
return null; return null;

View File

@@ -243,7 +243,7 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the connection resource. * Returns the connection resource.
*/ */
public function getResource(): \mysqli public function getResource(): ?\mysqli
{ {
return @$this->connection->thread_id ? $this->connection : null; return @$this->connection->thread_id ? $this->connection : null;
} }

View File

@@ -30,7 +30,7 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
{ {
use Dibi\Strict; use Dibi\Strict;
/** @var PDO Connection resource */ /** @var PDO|null Connection resource */
private $connection; private $connection;
/** @var \PDOStatement|null Resultset resource */ /** @var \PDOStatement|null Resultset resource */
@@ -190,7 +190,7 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the connection resource. * Returns the connection resource.
*/ */
public function getResource(): PDO public function getResource(): ?PDO
{ {
return $this->connection; return $this->connection;
} }

View File

@@ -202,7 +202,7 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the connection resource. * Returns the connection resource.
*/ */
public function getResource(): SQLite3 public function getResource(): ?SQLite3
{ {
return $this->connection; return $this->connection;
} }

View File

@@ -46,7 +46,7 @@ class Event
/** @var float */ /** @var float */
public $time; public $time;
/** @var int */ /** @var int|null */
public $count; public $count;
/** @var array */ /** @var array */
@@ -77,7 +77,7 @@ class Event
} }
} }
\dibi::$elapsedTime = false; \dibi::$elapsedTime = null;
\dibi::$numOfQueries++; \dibi::$numOfQueries++;
\dibi::$sql = $sql; \dibi::$sql = $sql;
} }

View File

@@ -15,7 +15,7 @@ namespace Dibi;
* *
* @method Fluent select(...$field) * @method Fluent select(...$field)
* @method Fluent distinct() * @method Fluent distinct()
* @method Fluent from($table) * @method Fluent from($table, ...$args)
* @method Fluent where(...$cond) * @method Fluent where(...$cond)
* @method Fluent groupBy(...$field) * @method Fluent groupBy(...$field)
* @method Fluent having(...$cond) * @method Fluent having(...$cond)
@@ -97,7 +97,7 @@ class Fluent implements IDataSource
/** @var array */ /** @var array */
private $flags = []; private $flags = [];
/** @var array */ /** @var array|null */
private $cursor; private $cursor;
/** @var HashMap normalized clauses */ /** @var HashMap normalized clauses */
@@ -268,7 +268,6 @@ class Fluent implements IDataSource
/** /**
* Adds Result setup. * Adds Result setup.
* @param mixed $method
*/ */
public function setupResult(string $method): self public function setupResult(string $method): self
{ {
@@ -282,7 +281,6 @@ class Fluent implements IDataSource
/** /**
* Generates and executes SQL query. * Generates and executes SQL query.
* @param mixed $return what to return?
* @return Result|int result set or number of affected rows * @return Result|int result set or number of affected rows
* @throws Exception * @throws Exception
*/ */

View File

@@ -14,7 +14,7 @@ class Helpers
{ {
use Strict; use Strict;
/** @var array */ /** @var HashMap */
private static $types; private static $types;

View File

@@ -31,7 +31,7 @@ class FirePhpLogger
/** @var int */ /** @var int */
public $filter; public $filter;
/** @var int Elapsed time for all queries */ /** @var float Elapsed time for all queries */
public $totalTime = 0; public $totalTime = 0;
/** @var int Number of all queries */ /** @var int Number of all queries */

View File

@@ -26,7 +26,7 @@ class Database
/** @var Dibi\Reflector */ /** @var Dibi\Reflector */
private $reflector; private $reflector;
/** @var string */ /** @var string|null */
private $name; private $name;
/** @var array */ /** @var array */
@@ -40,7 +40,7 @@ class Database
} }
public function getName(): string public function getName(): ?string
{ {
return $this->name; return $this->name;
} }

View File

@@ -44,10 +44,10 @@ class Result implements IDataSource
/** @var bool Already fetched? Used for allowance for first seek(0) */ /** @var bool Already fetched? Used for allowance for first seek(0) */
private $fetched = false; private $fetched = false;
/** @var ?string returned object class */ /** @var string|null returned object class */
private $rowClass = Row::class; private $rowClass = Row::class;
/** @var callable returned object factory*/ /** @var callable|null returned object factory */
private $rowFactory; private $rowFactory;
/** @var array format */ /** @var array format */
@@ -96,7 +96,7 @@ class Result implements IDataSource
*/ */
final public function seek(int $row): bool final public function seek(int $row): bool
{ {
return ($row !== 0 || $this->fetched) ? (bool) $this->getResultDriver()->seek($row) : true; return ($row !== 0 || $this->fetched) ? $this->getResultDriver()->seek($row) : true;
} }
@@ -204,7 +204,7 @@ class Result implements IDataSource
/** /**
* Fetches all records from table. * Fetches all records from table.
* @return Row[] * @return Row[]|array[]
*/ */
final public function fetchAll(int $offset = null, int $limit = null): array final public function fetchAll(int $offset = null, int $limit = null): array
{ {

View File

@@ -31,7 +31,7 @@ class Row implements \ArrayAccess, \IteratorAggregate, \Countable
/** /**
* Converts value to DateTime object. * Converts value to DateTime object.
* @return \DateTime|string|null * @return DateTime|string|null
*/ */
public function asDateTime(string $key, string $format = null) public function asDateTime(string $key, string $format = null)
{ {

View File

@@ -41,10 +41,10 @@ final class Translator
/** @var int */ /** @var int */
private $ifLevelStart = 0; private $ifLevelStart = 0;
/** @var int */ /** @var int|null */
private $limit; private $limit;
/** @var int */ /** @var int|null */
private $offset; private $offset;
/** @var HashMap */ /** @var HashMap */
@@ -337,7 +337,7 @@ final class Translator
return $value === null ? 'NULL' : $this->driver->escapeBinary($value); return $value === null ? 'NULL' : $this->driver->escapeBinary($value);
case 'b': // boolean case 'b': // boolean
return $value === null ? 'NULL' : $this->driver->escapeBool($value); return $value === null ? 'NULL' : $this->driver->escapeBool((bool) $value);
case 'sN': // string or null case 'sN': // string or null
case 'sn': case 'sn':

View File

@@ -52,13 +52,13 @@ class dibi
ASC = 'ASC', ASC = 'ASC',
DESC = 'DESC'; DESC = 'DESC';
/** @var string Last SQL command @see dibi::query() */ /** @var string|null Last SQL command @see dibi::query() */
public static $sql; public static $sql;
/** @var int Elapsed time for last query */ /** @var float|null Elapsed time for last query */
public static $elapsedTime; public static $elapsedTime;
/** @var int Elapsed time for all queries */ /** @var float Elapsed time for all queries */
public static $totalTime; public static $totalTime;
/** @var int Number or queries */ /** @var int Number or queries */
@@ -170,7 +170,7 @@ 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|Result $sql * @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($sql = null, bool $return = false): ?string

View File

@@ -93,7 +93,7 @@ class ProcedureException extends Exception
/** /**
* Construct the exception. * Construct the exception.
*/ */
public function __construct(string $message = null, int $code = 0, string $severity = null, string $sql = null) public function __construct(string $message = '', int $code = 0, string $severity = '', string $sql = null)
{ {
parent::__construct($message, $code, $sql); parent::__construct($message, $code, $sql);
$this->severity = $severity; $this->severity = $severity;

View File

@@ -223,7 +223,6 @@ interface IConnection
/** /**
* Generates (translates) and executes SQL query. * Generates (translates) and executes SQL query.
* @param mixed $args
* @return Result|int result set or number of affected rows * @return Result|int result set or number of affected rows
* @throws Exception * @throws Exception
*/ */