mirror of
https://github.com/dg/dibi.git
synced 2025-08-08 07:06:52 +02:00
type improvements
This commit is contained in:
@@ -23,7 +23,7 @@ class Connection implements IConnection
|
||||
use Strict;
|
||||
|
||||
/** @var array of function (Event $event); Occurs after query is executed */
|
||||
public $onEvent;
|
||||
public $onEvent = [];
|
||||
|
||||
/** @var array Current connection configuration */
|
||||
private $config;
|
||||
@@ -104,7 +104,7 @@ class Connection implements IConnection
|
||||
$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'])) {
|
||||
foreach ($config['substitutes'] as $key => $value) {
|
||||
$this->substitutes->$key = $value;
|
||||
@@ -141,7 +141,7 @@ class Connection implements IConnection
|
||||
$this->onEvent($event->done());
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
} catch (DriverException $e) {
|
||||
if ($event) {
|
||||
$this->onEvent($event->done($e));
|
||||
}
|
||||
@@ -197,7 +197,7 @@ class Connection implements IConnection
|
||||
/**
|
||||
* Generates (translates) and executes SQL query.
|
||||
* @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
|
||||
*/
|
||||
final public function query(...$args)
|
||||
@@ -281,7 +281,7 @@ class Connection implements IConnection
|
||||
try {
|
||||
$res = $this->driver->query($sql);
|
||||
|
||||
} catch (Exception $e) {
|
||||
} catch (DriverException $e) {
|
||||
if ($event) {
|
||||
$this->onEvent($event->done($e));
|
||||
}
|
||||
@@ -494,7 +494,7 @@ class Connection implements IConnection
|
||||
{
|
||||
return strpos($value, ':') === false
|
||||
? $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
|
||||
{
|
||||
foreach ($this->onEvent ?: [] as $handler) {
|
||||
foreach ($this->onEvent as $handler) {
|
||||
$handler($arg);
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
});
|
||||
if (empty($config['persistent'])) {
|
||||
|
@@ -37,9 +37,10 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
|
||||
/** @var bool */
|
||||
private $autoFree = true;
|
||||
|
||||
/** @var string Date and datetime format */
|
||||
/** @var string Date format */
|
||||
private $fmtDate;
|
||||
|
||||
/** @var string Datetime format */
|
||||
private $fmtDateTime;
|
||||
|
||||
|
||||
|
@@ -49,7 +49,7 @@ class Event
|
||||
/** @var int|null */
|
||||
public $count;
|
||||
|
||||
/** @var array */
|
||||
/** @var array|null */
|
||||
public $source;
|
||||
|
||||
|
||||
@@ -82,6 +82,9 @@ class Event
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Result|DriverException|null $result
|
||||
*/
|
||||
public function done($result = null): self
|
||||
{
|
||||
$this->result = $result;
|
||||
|
@@ -88,7 +88,7 @@ class Fluent implements IDataSource
|
||||
/** @var array */
|
||||
private $setups = [];
|
||||
|
||||
/** @var string */
|
||||
/** @var string|null */
|
||||
private $command;
|
||||
|
||||
/** @var array */
|
||||
@@ -251,7 +251,7 @@ class Fluent implements IDataSource
|
||||
/**
|
||||
* Returns SQL command.
|
||||
*/
|
||||
final public function getCommand(): string
|
||||
final public function getCommand(): ?string
|
||||
{
|
||||
return $this->command;
|
||||
}
|
||||
@@ -281,7 +281,7 @@ class Fluent implements IDataSource
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function execute(string $return = null)
|
||||
|
@@ -16,6 +16,7 @@ namespace Dibi;
|
||||
*/
|
||||
abstract class HashMapBase
|
||||
{
|
||||
/** @var callable */
|
||||
private $callback;
|
||||
|
||||
|
||||
@@ -45,18 +46,18 @@ abstract class HashMapBase
|
||||
*/
|
||||
final class HashMap extends HashMapBase
|
||||
{
|
||||
public function __set($nm, $val)
|
||||
public function __set(string $nm, $val)
|
||||
{
|
||||
if ($nm == '') {
|
||||
if ($nm === '') {
|
||||
$nm = "\xFF";
|
||||
}
|
||||
$this->$nm = $val;
|
||||
}
|
||||
|
||||
|
||||
public function __get($nm)
|
||||
public function __get(string $nm)
|
||||
{
|
||||
if ($nm == '') {
|
||||
if ($nm === '') {
|
||||
$nm = "\xFF";
|
||||
return isset($this->$nm) ? $this->$nm : $this->$nm = $this->getCallback()('');
|
||||
} else {
|
||||
|
@@ -89,7 +89,7 @@ class Helpers
|
||||
$highlighter = "#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is";
|
||||
if (PHP_SAPI === 'cli') {
|
||||
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
|
||||
return "\033[1;30m" . $m[1] . "\033[0m";
|
||||
|
||||
@@ -108,7 +108,7 @@ class Helpers
|
||||
|
||||
} else {
|
||||
$sql = htmlspecialchars($sql);
|
||||
$sql = preg_replace_callback($highlighter, function ($m) {
|
||||
$sql = preg_replace_callback($highlighter, function (array $m) {
|
||||
if (!empty($m[1])) { // comment
|
||||
return '<em style="color:gray">' . $m[1] . '</em>';
|
||||
|
||||
@@ -139,7 +139,7 @@ class Helpers
|
||||
* Finds the best suggestion.
|
||||
* @internal
|
||||
*/
|
||||
public static function getSuggestion(array $items, $value): ?string
|
||||
public static function getSuggestion(array $items, string $value): ?string
|
||||
{
|
||||
$best = null;
|
||||
$min = (strlen($value) / 4 + 1) * 10 + .1;
|
||||
@@ -204,7 +204,7 @@ class Helpers
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public static function getTypeCache()
|
||||
public static function getTypeCache(): HashMap
|
||||
{
|
||||
if (self::$types === null) {
|
||||
self::$types = new HashMap([__CLASS__, 'detectType']);
|
||||
|
@@ -29,7 +29,7 @@ class Database
|
||||
/** @var string|null */
|
||||
private $name;
|
||||
|
||||
/** @var array */
|
||||
/** @var Table[]|null */
|
||||
private $tables;
|
||||
|
||||
|
||||
|
@@ -25,10 +25,10 @@ class Result
|
||||
/** @var Dibi\ResultDriver */
|
||||
private $driver;
|
||||
|
||||
/** @var array */
|
||||
/** @var Column[]|null */
|
||||
private $columns;
|
||||
|
||||
/** @var array */
|
||||
/** @var string[]|null */
|
||||
private $names;
|
||||
|
||||
|
||||
|
@@ -36,16 +36,16 @@ class Table
|
||||
/** @var bool */
|
||||
private $view;
|
||||
|
||||
/** @var array */
|
||||
/** @var Column[]|null */
|
||||
private $columns;
|
||||
|
||||
/** @var array */
|
||||
/** @var ForeignKey[]|null */
|
||||
private $foreignKeys;
|
||||
|
||||
/** @var array */
|
||||
/** @var Index[]|null */
|
||||
private $indexes;
|
||||
|
||||
/** @var Index */
|
||||
/** @var Index|null */
|
||||
private $primaryKey;
|
||||
|
||||
|
||||
|
@@ -32,7 +32,7 @@ class Result implements IDataSource
|
||||
{
|
||||
use Strict;
|
||||
|
||||
/** @var ResultDriver|null */
|
||||
/** @var ResultDriver */
|
||||
private $driver;
|
||||
|
||||
/** @var array Translate table */
|
||||
|
@@ -33,7 +33,7 @@ class ResultIterator implements \Iterator, \Countable
|
||||
private $row;
|
||||
|
||||
/** @var int */
|
||||
private $pointer;
|
||||
private $pointer = 0;
|
||||
|
||||
|
||||
public function __construct(Result $result)
|
||||
|
@@ -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);
|
||||
trigger_error("Attempt to read missing column '$key'" . ($hint ? ", did you mean '$hint'?" : '.'), E_USER_NOTICE);
|
||||
|
@@ -27,7 +27,7 @@ trait Strict
|
||||
* Call to undefined method.
|
||||
* @throws \LogicException
|
||||
*/
|
||||
public function __call($name, $args)
|
||||
public function __call(string $name, array $args)
|
||||
{
|
||||
$class = get_class($this);
|
||||
if ($cb = self::extensionMethod($class . '::' . $name)) { // back compatiblity
|
||||
@@ -46,7 +46,7 @@ trait Strict
|
||||
* Call to undefined static method.
|
||||
* @throws \LogicException
|
||||
*/
|
||||
public static function __callStatic($name, $args)
|
||||
public static function __callStatic(string $name, array $args)
|
||||
{
|
||||
$rc = new ReflectionClass(get_called_class());
|
||||
$items = array_intersect($rc->getMethods(ReflectionMethod::IS_PUBLIC), $rc->getMethods(ReflectionMethod::IS_STATIC));
|
||||
@@ -59,7 +59,7 @@ trait Strict
|
||||
* Access to undeclared property.
|
||||
* @throws \LogicException
|
||||
*/
|
||||
public function &__get($name)
|
||||
public function &__get(string $name)
|
||||
{
|
||||
if ((method_exists($this, $m = 'get' . $name) || method_exists($this, $m = 'is' . $name))
|
||||
&& (new ReflectionMethod($this, $m))->isPublic()
|
||||
@@ -78,7 +78,7 @@ trait Strict
|
||||
* Access to undeclared property.
|
||||
* @throws \LogicException
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
public function __set(string $name, $value)
|
||||
{
|
||||
$rc = new ReflectionClass($this);
|
||||
$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;
|
||||
}
|
||||
@@ -97,7 +97,7 @@ trait Strict
|
||||
* Access to undeclared property.
|
||||
* @throws \LogicException
|
||||
*/
|
||||
public function __unset($name)
|
||||
public function __unset(string $name)
|
||||
{
|
||||
$class = get_class($this);
|
||||
throw new \LogicException("Attempt to unset undeclared property $class::$$name.");
|
||||
|
@@ -70,6 +70,7 @@ final class Translator
|
||||
$args = array_values($args[0]);
|
||||
}
|
||||
$this->args = $args;
|
||||
$this->errors = [];
|
||||
|
||||
$commandIns = null;
|
||||
$lastArr = null;
|
||||
|
@@ -13,8 +13,8 @@ declare(strict_types=1);
|
||||
* store connections info.
|
||||
*
|
||||
* @method void disconnect()
|
||||
* @method Dibi\Result|int query(...$args)
|
||||
* @method Dibi\Result|int nativeQuery(...$args)
|
||||
* @method Dibi\Result|int|null query(...$args)
|
||||
* @method Dibi\Result|int|null nativeQuery(...$args)
|
||||
* @method bool test(...$args)
|
||||
* @method Dibi\DataSource dataSource(...$args)
|
||||
* @method Dibi\Row|null fetch(...$args)
|
||||
@@ -85,6 +85,7 @@ class dibi
|
||||
|
||||
/**
|
||||
* Creates a new Connection object and connects it to specified database.
|
||||
* @param array $config connection parameters
|
||||
* @throws Dibi\Exception
|
||||
*/
|
||||
public static function connect($config = [], string $name = '0'): Dibi\Connection
|
||||
@@ -139,7 +140,7 @@ class dibi
|
||||
/**
|
||||
* Monostate for Dibi\Connection.
|
||||
*/
|
||||
public static function __callStatic($name, $args)
|
||||
public static function __callStatic(string $name, array $args)
|
||||
{
|
||||
return self::getConnection()->$name(...$args);
|
||||
}
|
||||
|
@@ -21,6 +21,7 @@ class Exception extends \Exception
|
||||
|
||||
/**
|
||||
* Construct a dibi exception.
|
||||
* @param int|string $code
|
||||
*/
|
||||
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
|
||||
{
|
||||
$this->severity;
|
||||
return $this->severity;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -220,7 +220,7 @@ interface IConnection
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
function query(...$args);
|
||||
|
Reference in New Issue
Block a user