From e45638eab47c9ce76dd6816322866e7999e59abb Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 26 Sep 2023 01:46:03 +0200 Subject: [PATCH] cs --- src/Dibi/Bridges/Tracy/Panel.php | 6 +++--- src/Dibi/Drivers/OdbcDriver.php | 2 +- src/Dibi/Drivers/PdoDriver.php | 23 +++++++---------------- src/Dibi/Fluent.php | 13 +++++-------- src/Dibi/HashMap.php | 4 ++-- src/Dibi/Row.php | 3 ++- tests/dibi/Row.phpt | 32 ++++++++++++++++++++------------ tests/dibi/bootstrap.php | 2 +- 8 files changed, 41 insertions(+), 44 deletions(-) diff --git a/src/Dibi/Bridges/Tracy/Panel.php b/src/Dibi/Bridges/Tracy/Panel.php index 76bda79c..452bc180 100644 --- a/src/Dibi/Bridges/Tracy/Panel.php +++ b/src/Dibi/Bridges/Tracy/Panel.php @@ -62,7 +62,7 @@ class Panel implements Tracy\IBarPanel if ($e instanceof Dibi\Exception && $e->getSql()) { return [ 'tab' => 'SQL', - 'panel' => Helpers::dump($e->getSql(), true), + 'panel' => Helpers::dump($e->getSql(), return: true), ]; } @@ -118,7 +118,7 @@ class Panel implements Tracy\IBarPanel ? $this->explain : ($connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN'); try { - $explain = @Helpers::dump($connection->nativeQuery("$cmd $event->sql"), true); + $explain = @Helpers::dump($connection->nativeQuery("$cmd $event->sql"), return: true); } catch (Dibi\Exception $e) { } @@ -132,7 +132,7 @@ class Panel implements Tracy\IBarPanel $s .= "
explain"; } - $s .= '' . Helpers::dump(strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql, true); + $s .= '' . Helpers::dump(strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql, return: true); if ($explain) { $s .= "
{$explain}
"; } diff --git a/src/Dibi/Drivers/OdbcDriver.php b/src/Dibi/Drivers/OdbcDriver.php index 5e9df989..a5471c35 100644 --- a/src/Dibi/Drivers/OdbcDriver.php +++ b/src/Dibi/Drivers/OdbcDriver.php @@ -119,7 +119,7 @@ class OdbcDriver implements Dibi\Driver */ public function begin(?string $savepoint = null): void { - if (!odbc_autocommit($this->connection, false)) { + if (!odbc_autocommit($this->connection)) { throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection)); } } diff --git a/src/Dibi/Drivers/PdoDriver.php b/src/Dibi/Drivers/PdoDriver.php index ca833798..bca0e1d0 100644 --- a/src/Dibi/Drivers/PdoDriver.php +++ b/src/Dibi/Drivers/PdoDriver.php @@ -94,22 +94,13 @@ class PdoDriver implements Dibi\Driver [$sqlState, $code, $message] = $this->connection->errorInfo(); $message = "SQLSTATE[$sqlState]: $message"; - switch ($this->driverName) { - case 'mysql': - throw MySqliDriver::createException($message, $code, $sql); - - case 'oci': - throw OracleDriver::createException($message, $code, $sql); - - case 'pgsql': - throw PostgreDriver::createException($message, $sqlState, $sql); - - case 'sqlite': - throw SqliteDriver::createException($message, $code, $sql); - - default: - throw new Dibi\DriverException($message, $code, $sql); - } + throw match ($this->driverName) { + 'mysql' => MySqliDriver::createException($message, $code, $sql), + 'oci' => OracleDriver::createException($message, $code, $sql), + 'pgsql' => PostgreDriver::createException($message, $sqlState, $sql), + 'sqlite' => SqliteDriver::createException($message, $code, $sql), + default => new Dibi\DriverException($message, $code, $sql), + }; } diff --git a/src/Dibi/Fluent.php b/src/Dibi/Fluent.php index 2cbc0232..7edb45ab 100644 --- a/src/Dibi/Fluent.php +++ b/src/Dibi/Fluent.php @@ -285,14 +285,11 @@ class Fluent implements IDataSource public function execute(?string $return = null): Result|int|null { $res = $this->query($this->_export()); - switch ($return) { - case \dibi::IDENTIFIER: - return $this->connection->getInsertId(); - case \dibi::AFFECTED_ROWS: - return $this->connection->getAffectedRows(); - default: - return $res; - } + return match ($return) { + \dibi::IDENTIFIER => $this->connection->getInsertId(), + \dibi::AFFECTED_ROWS => $this->connection->getAffectedRows(), + default => $res, + }; } diff --git a/src/Dibi/HashMap.php b/src/Dibi/HashMap.php index df556e9d..54f0f7f8 100644 --- a/src/Dibi/HashMap.php +++ b/src/Dibi/HashMap.php @@ -46,7 +46,7 @@ abstract class HashMapBase */ final class HashMap extends HashMapBase { - public function __set(string $nm, $val) + public function __set(string $nm, mixed $val): void { if ($nm === '') { $nm = "\xFF"; @@ -56,7 +56,7 @@ final class HashMap extends HashMapBase } - public function __get(string $nm) + public function __get(string $nm): mixed { if ($nm === '') { $nm = "\xFF"; diff --git a/src/Dibi/Row.php b/src/Dibi/Row.php index 8a99d4e8..369d9d1b 100644 --- a/src/Dibi/Row.php +++ b/src/Dibi/Row.php @@ -48,10 +48,11 @@ class Row implements \ArrayAccess, \IteratorAggregate, \Countable } - public function __get(string $key) + public function __get(string $key): mixed { $hint = Helpers::getSuggestion(array_keys((array) $this), $key); trigger_error("Attempt to read missing column '$key'" . ($hint ? ", did you mean '$hint'?" : '.'), E_USER_NOTICE); + return null; } diff --git a/tests/dibi/Row.phpt b/tests/dibi/Row.phpt index 87287211..ff75363f 100644 --- a/tests/dibi/Row.phpt +++ b/tests/dibi/Row.phpt @@ -24,13 +24,17 @@ Assert::true(isset($row['title'])); // missing -Assert::error(function () use ($row) { - $x = $row->missing; -}, E_USER_NOTICE, "Attempt to read missing column 'missing'."); +Assert::error( + fn() => $x = $row->missing, + E_USER_NOTICE, + "Attempt to read missing column 'missing'.", +); -Assert::error(function () use ($row) { - $x = $row['missing']; -}, E_USER_NOTICE, "Attempt to read missing column 'missing'."); +Assert::error( + fn() => $x = $row['missing'], + E_USER_NOTICE, + "Attempt to read missing column 'missing'.", +); Assert::false(isset($row->missing)); Assert::false(isset($row['missing'])); @@ -41,13 +45,17 @@ Assert::same(123, $row['missing'] ?? 123); // suggestions -Assert::error(function () use ($row) { - $x = $row->tilte; -}, E_USER_NOTICE, "Attempt to read missing column 'tilte', did you mean 'title'?"); +Assert::error( + fn() => $x = $row->tilte, + E_USER_NOTICE, + "Attempt to read missing column 'tilte', did you mean 'title'?", +); -Assert::error(function () use ($row) { - $x = $row['tilte']; -}, E_USER_NOTICE, "Attempt to read missing column 'tilte', did you mean 'title'?"); +Assert::error( + fn() => $x = $row['tilte'], + E_USER_NOTICE, + "Attempt to read missing column 'tilte', did you mean 'title'?", +); // to array diff --git a/tests/dibi/bootstrap.php b/tests/dibi/bootstrap.php index 1d7963fb..655c4054 100644 --- a/tests/dibi/bootstrap.php +++ b/tests/dibi/bootstrap.php @@ -20,7 +20,7 @@ date_default_timezone_set('Europe/Prague'); try { $config = Tester\Environment::loadData(); } catch (Throwable $e) { - $config = parse_ini_file(__DIR__ . '/../databases.ini', true); + $config = parse_ini_file(__DIR__ . '/../databases.ini', process_sections: true); $config = reset($config); }