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

type improvements

This commit is contained in:
David Grudl
2018-05-01 13:24:22 +02:00
parent 9bcca8feb0
commit ca6f1c819a
18 changed files with 50 additions and 42 deletions

View File

@@ -23,7 +23,7 @@ class Connection implements IConnection
use Strict; use Strict;
/** @var array of function (Event $event); Occurs after query is executed */ /** @var array of function (Event $event); Occurs after query is executed */
public $onEvent; public $onEvent = [];
/** @var array Current connection configuration */ /** @var array Current connection configuration */
private $config; private $config;
@@ -104,7 +104,7 @@ class Connection implements IConnection
$this->onEvent[] = [new Loggers\FileLogger($config['profiler']['file'], $filter), 'logEvent']; $this->onEvent[] = [new Loggers\FileLogger($config['profiler']['file'], $filter), 'logEvent'];
} }
$this->substitutes = new HashMap(function ($expr) { return ":$expr:"; }); $this->substitutes = new HashMap(function (string $expr) { return ":$expr:"; });
if (!empty($config['substitutes'])) { if (!empty($config['substitutes'])) {
foreach ($config['substitutes'] as $key => $value) { foreach ($config['substitutes'] as $key => $value) {
$this->substitutes->$key = $value; $this->substitutes->$key = $value;
@@ -141,7 +141,7 @@ class Connection implements IConnection
$this->onEvent($event->done()); $this->onEvent($event->done());
} }
} catch (Exception $e) { } catch (DriverException $e) {
if ($event) { if ($event) {
$this->onEvent($event->done($e)); $this->onEvent($event->done($e));
} }
@@ -197,7 +197,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|null result set or number of affected rows
* @throws Exception * @throws Exception
*/ */
final public function query(...$args) final public function query(...$args)
@@ -281,7 +281,7 @@ class Connection implements IConnection
try { try {
$res = $this->driver->query($sql); $res = $this->driver->query($sql);
} catch (Exception $e) { } catch (DriverException $e) {
if ($event) { if ($event) {
$this->onEvent($event->done($e)); $this->onEvent($event->done($e));
} }
@@ -494,7 +494,7 @@ class Connection implements IConnection
{ {
return strpos($value, ':') === false return strpos($value, ':') === false
? $value ? $value
: preg_replace_callback('#:([^:\s]*):#', function ($m) { return $this->substitutes->{$m[1]}; }, $value); : preg_replace_callback('#:([^:\s]*):#', function (array $m) { return $this->substitutes->{$m[1]}; }, $value);
} }
@@ -605,7 +605,7 @@ class Connection implements IConnection
protected function onEvent($arg): void protected function onEvent($arg): void
{ {
foreach ($this->onEvent ?: [] as $handler) { foreach ($this->onEvent as $handler) {
$handler($arg); $handler($arg);
} }
} }

View File

@@ -80,7 +80,7 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
} }
} }
set_error_handler(function ($severity, string $message) use (&$error) { set_error_handler(function (int $severity, string $message) use (&$error) {
$error = $message; $error = $message;
}); });
if (empty($config['persistent'])) { if (empty($config['persistent'])) {

View File

@@ -37,9 +37,10 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** @var bool */ /** @var bool */
private $autoFree = true; private $autoFree = true;
/** @var string Date and datetime format */ /** @var string Date format */
private $fmtDate; private $fmtDate;
/** @var string Datetime format */
private $fmtDateTime; private $fmtDateTime;

View File

@@ -49,7 +49,7 @@ class Event
/** @var int|null */ /** @var int|null */
public $count; public $count;
/** @var array */ /** @var array|null */
public $source; public $source;
@@ -82,6 +82,9 @@ class Event
} }
/**
* @param Result|DriverException|null $result
*/
public function done($result = null): self public function done($result = null): self
{ {
$this->result = $result; $this->result = $result;

View File

@@ -88,7 +88,7 @@ class Fluent implements IDataSource
/** @var array */ /** @var array */
private $setups = []; private $setups = [];
/** @var string */ /** @var string|null */
private $command; private $command;
/** @var array */ /** @var array */
@@ -251,7 +251,7 @@ class Fluent implements IDataSource
/** /**
* Returns SQL command. * Returns SQL command.
*/ */
final public function getCommand(): string final public function getCommand(): ?string
{ {
return $this->command; return $this->command;
} }
@@ -281,7 +281,7 @@ class Fluent implements IDataSource
/** /**
* Generates and executes SQL query. * Generates and executes 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
*/ */
public function execute(string $return = null) public function execute(string $return = null)

View File

@@ -16,6 +16,7 @@ namespace Dibi;
*/ */
abstract class HashMapBase abstract class HashMapBase
{ {
/** @var callable */
private $callback; private $callback;
@@ -45,18 +46,18 @@ abstract class HashMapBase
*/ */
final class HashMap extends HashMapBase final class HashMap extends HashMapBase
{ {
public function __set($nm, $val) public function __set(string $nm, $val)
{ {
if ($nm == '') { if ($nm === '') {
$nm = "\xFF"; $nm = "\xFF";
} }
$this->$nm = $val; $this->$nm = $val;
} }
public function __get($nm) public function __get(string $nm)
{ {
if ($nm == '') { if ($nm === '') {
$nm = "\xFF"; $nm = "\xFF";
return isset($this->$nm) ? $this->$nm : $this->$nm = $this->getCallback()(''); return isset($this->$nm) ? $this->$nm : $this->$nm = $this->getCallback()('');
} else { } else {

View File

@@ -89,7 +89,7 @@ class Helpers
$highlighter = "#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is"; $highlighter = "#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is";
if (PHP_SAPI === 'cli') { if (PHP_SAPI === 'cli') {
if (substr((string) getenv('TERM'), 0, 5) === 'xterm') { if (substr((string) getenv('TERM'), 0, 5) === 'xterm') {
$sql = preg_replace_callback($highlighter, function ($m) { $sql = preg_replace_callback($highlighter, function (array $m) {
if (!empty($m[1])) { // comment if (!empty($m[1])) { // comment
return "\033[1;30m" . $m[1] . "\033[0m"; return "\033[1;30m" . $m[1] . "\033[0m";
@@ -108,7 +108,7 @@ class Helpers
} else { } else {
$sql = htmlspecialchars($sql); $sql = htmlspecialchars($sql);
$sql = preg_replace_callback($highlighter, function ($m) { $sql = preg_replace_callback($highlighter, function (array $m) {
if (!empty($m[1])) { // comment if (!empty($m[1])) { // comment
return '<em style="color:gray">' . $m[1] . '</em>'; return '<em style="color:gray">' . $m[1] . '</em>';
@@ -139,7 +139,7 @@ class Helpers
* Finds the best suggestion. * Finds the best suggestion.
* @internal * @internal
*/ */
public static function getSuggestion(array $items, $value): ?string public static function getSuggestion(array $items, string $value): ?string
{ {
$best = null; $best = null;
$min = (strlen($value) / 4 + 1) * 10 + .1; $min = (strlen($value) / 4 + 1) * 10 + .1;
@@ -204,7 +204,7 @@ class Helpers
/** /**
* @internal * @internal
*/ */
public static function getTypeCache() public static function getTypeCache(): HashMap
{ {
if (self::$types === null) { if (self::$types === null) {
self::$types = new HashMap([__CLASS__, 'detectType']); self::$types = new HashMap([__CLASS__, 'detectType']);

View File

@@ -29,7 +29,7 @@ class Database
/** @var string|null */ /** @var string|null */
private $name; private $name;
/** @var array */ /** @var Table[]|null */
private $tables; private $tables;

View File

@@ -25,10 +25,10 @@ class Result
/** @var Dibi\ResultDriver */ /** @var Dibi\ResultDriver */
private $driver; private $driver;
/** @var array */ /** @var Column[]|null */
private $columns; private $columns;
/** @var array */ /** @var string[]|null */
private $names; private $names;

View File

@@ -36,16 +36,16 @@ class Table
/** @var bool */ /** @var bool */
private $view; private $view;
/** @var array */ /** @var Column[]|null */
private $columns; private $columns;
/** @var array */ /** @var ForeignKey[]|null */
private $foreignKeys; private $foreignKeys;
/** @var array */ /** @var Index[]|null */
private $indexes; private $indexes;
/** @var Index */ /** @var Index|null */
private $primaryKey; private $primaryKey;

View File

@@ -32,7 +32,7 @@ class Result implements IDataSource
{ {
use Strict; use Strict;
/** @var ResultDriver|null */ /** @var ResultDriver */
private $driver; private $driver;
/** @var array Translate table */ /** @var array Translate table */

View File

@@ -33,7 +33,7 @@ class ResultIterator implements \Iterator, \Countable
private $row; private $row;
/** @var int */ /** @var int */
private $pointer; private $pointer = 0;
public function __construct(Result $result) public function __construct(Result $result)

View File

@@ -46,7 +46,7 @@ class Row implements \ArrayAccess, \IteratorAggregate, \Countable
} }
public function __get($key) public function __get(string $key)
{ {
$hint = Helpers::getSuggestion(array_keys((array) $this), $key); $hint = Helpers::getSuggestion(array_keys((array) $this), $key);
trigger_error("Attempt to read missing column '$key'" . ($hint ? ", did you mean '$hint'?" : '.'), E_USER_NOTICE); trigger_error("Attempt to read missing column '$key'" . ($hint ? ", did you mean '$hint'?" : '.'), E_USER_NOTICE);

View File

@@ -27,7 +27,7 @@ trait Strict
* Call to undefined method. * Call to undefined method.
* @throws \LogicException * @throws \LogicException
*/ */
public function __call($name, $args) public function __call(string $name, array $args)
{ {
$class = get_class($this); $class = get_class($this);
if ($cb = self::extensionMethod($class . '::' . $name)) { // back compatiblity if ($cb = self::extensionMethod($class . '::' . $name)) { // back compatiblity
@@ -46,7 +46,7 @@ trait Strict
* Call to undefined static method. * Call to undefined static method.
* @throws \LogicException * @throws \LogicException
*/ */
public static function __callStatic($name, $args) public static function __callStatic(string $name, array $args)
{ {
$rc = new ReflectionClass(get_called_class()); $rc = new ReflectionClass(get_called_class());
$items = array_intersect($rc->getMethods(ReflectionMethod::IS_PUBLIC), $rc->getMethods(ReflectionMethod::IS_STATIC)); $items = array_intersect($rc->getMethods(ReflectionMethod::IS_PUBLIC), $rc->getMethods(ReflectionMethod::IS_STATIC));
@@ -59,7 +59,7 @@ trait Strict
* Access to undeclared property. * Access to undeclared property.
* @throws \LogicException * @throws \LogicException
*/ */
public function &__get($name) public function &__get(string $name)
{ {
if ((method_exists($this, $m = 'get' . $name) || method_exists($this, $m = 'is' . $name)) if ((method_exists($this, $m = 'get' . $name) || method_exists($this, $m = 'is' . $name))
&& (new ReflectionMethod($this, $m))->isPublic() && (new ReflectionMethod($this, $m))->isPublic()
@@ -78,7 +78,7 @@ trait Strict
* Access to undeclared property. * Access to undeclared property.
* @throws \LogicException * @throws \LogicException
*/ */
public function __set($name, $value) public function __set(string $name, $value)
{ {
$rc = new ReflectionClass($this); $rc = new ReflectionClass($this);
$items = array_diff($rc->getProperties(ReflectionProperty::IS_PUBLIC), $rc->getProperties(ReflectionProperty::IS_STATIC)); $items = array_diff($rc->getProperties(ReflectionProperty::IS_PUBLIC), $rc->getProperties(ReflectionProperty::IS_STATIC));
@@ -87,7 +87,7 @@ trait Strict
} }
public function __isset($name): bool public function __isset(string $name): bool
{ {
return false; return false;
} }
@@ -97,7 +97,7 @@ trait Strict
* Access to undeclared property. * Access to undeclared property.
* @throws \LogicException * @throws \LogicException
*/ */
public function __unset($name) public function __unset(string $name)
{ {
$class = get_class($this); $class = get_class($this);
throw new \LogicException("Attempt to unset undeclared property $class::$$name."); throw new \LogicException("Attempt to unset undeclared property $class::$$name.");

View File

@@ -70,6 +70,7 @@ final class Translator
$args = array_values($args[0]); $args = array_values($args[0]);
} }
$this->args = $args; $this->args = $args;
$this->errors = [];
$commandIns = null; $commandIns = null;
$lastArr = null; $lastArr = null;

View File

@@ -13,8 +13,8 @@ declare(strict_types=1);
* store connections info. * store connections info.
* *
* @method void disconnect() * @method void disconnect()
* @method Dibi\Result|int query(...$args) * @method Dibi\Result|int|null query(...$args)
* @method Dibi\Result|int nativeQuery(...$args) * @method Dibi\Result|int|null nativeQuery(...$args)
* @method bool test(...$args) * @method bool test(...$args)
* @method Dibi\DataSource dataSource(...$args) * @method Dibi\DataSource dataSource(...$args)
* @method Dibi\Row|null fetch(...$args) * @method Dibi\Row|null fetch(...$args)
@@ -85,6 +85,7 @@ class dibi
/** /**
* Creates a new Connection object and connects it to specified database. * Creates a new Connection object and connects it to specified database.
* @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($config = [], string $name = '0'): Dibi\Connection
@@ -139,7 +140,7 @@ class dibi
/** /**
* Monostate for Dibi\Connection. * Monostate for Dibi\Connection.
*/ */
public static function __callStatic($name, $args) public static function __callStatic(string $name, array $args)
{ {
return self::getConnection()->$name(...$args); return self::getConnection()->$name(...$args);
} }

View File

@@ -21,6 +21,7 @@ class Exception extends \Exception
/** /**
* Construct a dibi exception. * Construct a dibi exception.
* @param int|string $code
*/ */
public function __construct(string $message = '', $code = 0, string $sql = null, \Throwable $previous = null) public function __construct(string $message = '', $code = 0, string $sql = null, \Throwable $previous = null)
{ {
@@ -105,7 +106,7 @@ class ProcedureException extends Exception
*/ */
public function getSeverity(): string public function getSeverity(): string
{ {
$this->severity; return $this->severity;
} }
} }

View File

@@ -220,7 +220,7 @@ interface IConnection
/** /**
* Generates (translates) and executes SQL query. * Generates (translates) and executes 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
*/ */
function query(...$args); function query(...$args);