From c7fe0fef21d3ec27daf060164126e82629754ccc Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 3 Sep 2024 16:31:38 +0200 Subject: [PATCH] uses PHP 8.2 features --- examples/using-substitutions.php | 2 +- src/Dibi/Bridges/Tracy/Panel.php | 4 ++-- src/Dibi/Drivers/PostgreDriver.php | 19 +++++++------------ src/Dibi/Drivers/PostgreResult.php | 10 +++------- src/Dibi/Fluent.php | 2 +- src/Dibi/Helpers.php | 2 +- src/Dibi/Translator.php | 6 +++--- 7 files changed, 18 insertions(+), 27 deletions(-) diff --git a/examples/using-substitutions.php b/examples/using-substitutions.php index d10d85a1..058ed47b 100644 --- a/examples/using-substitutions.php +++ b/examples/using-substitutions.php @@ -45,7 +45,7 @@ function substFallBack($expr) // define callback -$dibi->getSubstitutes()->setCallback('substFallBack'); +$dibi->getSubstitutes()->setCallback(substFallBack(...)); // define substitutes as constants define('SUBST_ACCOUNT', 'eshop_'); diff --git a/src/Dibi/Bridges/Tracy/Panel.php b/src/Dibi/Bridges/Tracy/Panel.php index 50071e39..5e3d5ae1 100644 --- a/src/Dibi/Bridges/Tracy/Panel.php +++ b/src/Dibi/Bridges/Tracy/Panel.php @@ -35,8 +35,8 @@ class Panel implements Tracy\IBarPanel public function register(Dibi\Connection $connection): void { Tracy\Debugger::getBar()->addPanel($this); - Tracy\Debugger::getBlueScreen()->addPanel([self::class, 'renderException']); - $connection->onEvent[] = [$this, 'logEvent']; + Tracy\Debugger::getBlueScreen()->addPanel(self::renderException(...)); + $connection->onEvent[] = $this->logEvent(...); } diff --git a/src/Dibi/Drivers/PostgreDriver.php b/src/Dibi/Drivers/PostgreDriver.php index 776e5288..ce3aa43e 100644 --- a/src/Dibi/Drivers/PostgreDriver.php +++ b/src/Dibi/Drivers/PostgreDriver.php @@ -23,13 +23,12 @@ use PgSql; * - schema => the schema search path * - charset => character encoding to set (default is utf8) * - persistent (bool) => try to find a persistent link? - * - resource (resource) => existing connection resource + * - resource (PgSql\Connection) => existing connection resource * - connect_type (int) => see pg_connect() */ class PostgreDriver implements Dibi\Driver { - /** @var resource|PgSql\Connection */ - private $connection; + private PgSql\Connection $connection; private ?int $affectedRows; @@ -72,7 +71,7 @@ class PostgreDriver implements Dibi\Driver restore_error_handler(); } - if (!is_resource($this->connection) && !$this->connection instanceof PgSql\Connection) { + if (!$this->connection instanceof PgSql\Connection) { throw new Dibi\DriverException($error ?: 'Connecting error.'); } @@ -118,7 +117,7 @@ class PostgreDriver implements Dibi\Driver if ($res === false) { throw static::createException(pg_last_error($this->connection), null, $sql); - } elseif (is_resource($res) || $res instanceof PgSql\Result) { + } elseif ($res instanceof PgSql\Result) { $this->affectedRows = Helpers::false2Null(pg_affected_rows($res)); if (pg_num_fields($res)) { return $this->createResultDriver($res); @@ -222,13 +221,10 @@ class PostgreDriver implements Dibi\Driver /** * Returns the connection resource. - * @return resource|null */ - public function getResource(): mixed + public function getResource(): PgSql\Connection { - return is_resource($this->connection) || $this->connection instanceof PgSql\Connection - ? $this->connection - : null; + return $this->connection; } @@ -243,9 +239,8 @@ class PostgreDriver implements Dibi\Driver /** * Result set driver factory. - * @param resource $resource */ - public function createResultDriver($resource): PostgreResult + public function createResultDriver(PgSql\Result $resource): PostgreResult { return new PostgreResult($resource); } diff --git a/src/Dibi/Drivers/PostgreResult.php b/src/Dibi/Drivers/PostgreResult.php index 0f2b00e0..62851e3a 100644 --- a/src/Dibi/Drivers/PostgreResult.php +++ b/src/Dibi/Drivers/PostgreResult.php @@ -20,8 +20,7 @@ use PgSql; class PostgreResult implements Dibi\ResultDriver { public function __construct( - /** @var resource|PgSql\Result */ - private $resultSet, + private PgSql\Result $resultSet, ) { } @@ -88,13 +87,10 @@ class PostgreResult implements Dibi\ResultDriver /** * Returns the result set resource. - * @return resource|PgSql\Result|null */ - public function getResultResource(): mixed + public function getResultResource(): PgSql\Result { - return is_resource($this->resultSet) || $this->resultSet instanceof PgSql\Result - ? $this->resultSet - : null; + return $this->resultSet; } diff --git a/src/Dibi/Fluent.php b/src/Dibi/Fluent.php index 011ba5e2..c24c06e4 100644 --- a/src/Dibi/Fluent.php +++ b/src/Dibi/Fluent.php @@ -113,7 +113,7 @@ class Fluent implements IDataSource $this->connection = $connection; if (!isset(self::$normalizer)) { - self::$normalizer = new HashMap([self::class, '_formatClause']); + self::$normalizer = new HashMap(self::_formatClause(...)); } } diff --git a/src/Dibi/Helpers.php b/src/Dibi/Helpers.php index 82bed671..83fca323 100644 --- a/src/Dibi/Helpers.php +++ b/src/Dibi/Helpers.php @@ -208,7 +208,7 @@ class Helpers public static function getTypeCache(): HashMap { if (!isset(self::$types)) { - self::$types = new HashMap([self::class, 'detectType']); + self::$types = new HashMap(self::detectType(...)); } return self::$types; diff --git a/src/Dibi/Translator.php b/src/Dibi/Translator.php index 35d31c71..e20ee055 100644 --- a/src/Dibi/Translator.php +++ b/src/Dibi/Translator.php @@ -34,7 +34,7 @@ final class Translator { $this->connection = $connection; $this->driver = $connection->getDriver(); - $this->identifiers = new HashMap([$this, 'delimite']); + $this->identifiers = new HashMap($this->delimite(...)); } @@ -88,7 +88,7 @@ final class Translator (\?) ## 11) placeholder )/xs XX, - [$this, 'cb'], + $this->cb(...), substr($arg, $toSkip), ); if (preg_last_error()) { @@ -434,7 +434,7 @@ final class Translator :(\S*?:)([a-zA-Z0-9._]?) )/sx XX, - [$this, 'cb'], + $this->cb(...), substr($value, $toSkip), ); if (preg_last_error()) {