1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-04 21:28:02 +02:00

coding style: fixes in code

This commit is contained in:
David Grudl
2017-07-11 12:29:13 +02:00
parent a299c622c3
commit dac0a116a8
32 changed files with 88 additions and 86 deletions

View File

@@ -141,7 +141,7 @@ class Panel implements Tracy\IBarPanel
#tracy-debug tracy-DibiProfiler tr table { margin: 8px 0; max-height: 150px; overflow:auto } </style>
<h1>Queries: ' . count($this->events)
. ($totalTime === null ? '' : ', time: ' . number_format($totalTime * 1000, 1, '.', '') . 'ms') . ', '
. htmlSpecialChars($connection->getConfig('driver') . ($connection->getConfig('name') ? '/' . $connection->getConfig('name') : '')
. htmlspecialchars($connection->getConfig('driver') . ($connection->getConfig('name') ? '/' . $connection->getConfig('name') : '')
. ($connection->getConfig('host') ? '@' . $connection->getConfig('host') : '')) . '</h1>
<div class="tracy-inner tracy-DibiProfiler">
<table>

View File

@@ -12,7 +12,6 @@ namespace Dibi;
/**
* Default implementation of IDataSource for dibi.
*
*/
class DataSource implements IDataSource
{

View File

@@ -35,7 +35,7 @@ class DateTime extends \DateTime
public function modifyClone(string $modify = '')
{
$dolly = clone($this);
$dolly = clone $this;
return $modify ? $dolly->modify($modify) : $dolly;
}

View File

@@ -29,7 +29,7 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
{
use Dibi\Strict;
const ERROR_EXCEPTION_THROWN = -836;
public const ERROR_EXCEPTION_THROWN = -836;
/** @var resource|null */
private $connection;
@@ -254,7 +254,7 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
public function escapeIdentifier(string $value): string
{
return '"' . str_replace('"', '""', $value). '"';
return '"' . str_replace('"', '""', $value) . '"';
}
@@ -644,9 +644,9 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/
public function getTriggers(string $table = null): array
{
$q = "SELECT TRIM(RDB\$TRIGGER_NAME)
FROM RDB\$TRIGGERS
WHERE RDB\$SYSTEM_FLAG = 0";
$q = 'SELECT TRIM(RDB$TRIGGER_NAME)
FROM RDB$TRIGGERS
WHERE RDB$SYSTEM_FLAG = 0';
$q .= $table === null ? ';' : " AND RDB\$RELATION_NAME = UPPER('$table')";
$res = $this->query($q);
@@ -714,9 +714,9 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/
public function getProcedures(): array
{
$res = $this->query("
SELECT TRIM(RDB\$PROCEDURE_NAME)
FROM RDB\$PROCEDURES;"
$res = $this->query('
SELECT TRIM(RDB$PROCEDURE_NAME)
FROM RDB$PROCEDURES;'
);
$procedures = [];
while ($row = $res->fetch(false)) {
@@ -731,10 +731,10 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/
public function getGenerators(): array
{
$res = $this->query("
SELECT TRIM(RDB\$GENERATOR_NAME)
FROM RDB\$GENERATORS
WHERE RDB\$SYSTEM_FLAG = 0;"
$res = $this->query('
SELECT TRIM(RDB$GENERATOR_NAME)
FROM RDB$GENERATORS
WHERE RDB$SYSTEM_FLAG = 0;'
);
$generators = [];
while ($row = $res->fetch(false)) {
@@ -749,10 +749,10 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/
public function getFunctions(): array
{
$res = $this->query("
SELECT TRIM(RDB\$FUNCTION_NAME)
FROM RDB\$FUNCTIONS
WHERE RDB\$SYSTEM_FLAG = 0;"
$res = $this->query('
SELECT TRIM(RDB$FUNCTION_NAME)
FROM RDB$FUNCTIONS
WHERE RDB$SYSTEM_FLAG = 0;'
);
$functions = [];
while ($row = $res->fetch(false)) {

View File

@@ -68,12 +68,12 @@ class MsSqlReflector implements Dibi\Reflector
if (!is_array($row) || count($row) < 1) {
if ($fallback) {
$row = $this->driver->query("SELECT COUNT(*) FROM {$this->driver->escapeIdentifier($table)}")->fetch(false);
$count = intval($row[0]);
$count = (int) ($row[0]);
} else {
$count = null;
}
} else {
$count = intval($row[0]);
$count = (int) ($row[0]);
}
return $count;

View File

@@ -35,9 +35,9 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
{
use Dibi\Strict;
const ERROR_ACCESS_DENIED = 1045;
const ERROR_DUPLICATE_ENTRY = 1062;
const ERROR_DATA_TRUNCATED = 1265;
public const ERROR_ACCESS_DENIED = 1045;
public const ERROR_DUPLICATE_ENTRY = 1062;
public const ERROR_DATA_TRUNCATED = 1265;
/** @var \mysqli|null */
private $connection;

View File

@@ -246,7 +246,7 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format("#m/d/Y#");
return $value->format('#m/d/Y#');
}
@@ -258,7 +258,7 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format("#m/d/Y H:i:s.u#");
return $value->format('#m/d/Y H:i:s.u#');
}

View File

@@ -322,7 +322,7 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
// see http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
$sql = 'SELECT * FROM (SELECT t.*, ROWNUM AS "__rnum" FROM (' . $sql . ') t '
. ($limit !== null ? 'WHERE ROWNUM <= ' . ((int) $offset + (int) $limit) : '')
. ') WHERE "__rnum" > '. (int) $offset;
. ') WHERE "__rnum" > ' . (int) $offset;
} elseif ($limit !== null) {
$sql = 'SELECT * FROM (' . $sql . ') WHERE ROWNUM <= ' . (int) $limit;

View File

@@ -317,7 +317,7 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
if (!$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format($this->driverName === 'odbc' ? "#m/d/Y H:i:s.u#" : "'Y-m-d H:i:s.u'");
return $value->format($this->driverName === 'odbc' ? '#m/d/Y H:i:s.u#' : "'Y-m-d H:i:s.u'");
}
@@ -407,7 +407,7 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
// see http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
$sql = 'SELECT * FROM (SELECT t.*, ROWNUM AS "__rnum" FROM (' . $sql . ') t '
. ($limit !== null ? 'WHERE ROWNUM <= ' . ((int) $offset + (int) $limit) : '')
. ') WHERE "__rnum" > '. (int) $offset;
. ') WHERE "__rnum" > ' . (int) $offset;
} elseif ($limit !== null) {
$sql = 'SELECT * FROM (' . $sql . ') WHERE ROWNUM <= ' . (int) $limit;
@@ -426,7 +426,7 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
}
break;
}
// intentionally break omitted
// break omitted
case 'odbc':
if ($offset) {
@@ -436,7 +436,7 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ') t';
break;
}
// intentionally break omitted
// break omitted
default:
throw new Dibi\NotSupportedException('PDO or driver does not support applying limit or offset.');

View File

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

View File

@@ -39,10 +39,12 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
private $autoFree = true;
/** @var string Date and datetime format */
private $fmtDate, $fmtDateTime;
private $fmtDate;
private $fmtDateTime;
/** @var string character encoding */
private $dbcharset, $charset;
private $dbcharset;
private $charset;
/**

View File

@@ -102,7 +102,7 @@ class SqlsrvReflector implements Dibi\Reflector
*/
public function getIndexes(string $table): array
{
$keyUsagesRes = $this->driver->query(sprintf("EXEC [sys].[sp_helpindex] @objname = N%s", $this->driver->escapeText($table)));
$keyUsagesRes = $this->driver->query(sprintf('EXEC [sys].[sp_helpindex] @objname = N%s', $this->driver->escapeText($table)));
$keyUsages = [];
while ($row = $keyUsagesRes->fetch(true)) {
$keyUsages[$row['index_name']] = explode(',', $row['index_keys']);

View File

@@ -18,7 +18,8 @@ class Event
use Strict;
/** event type */
const CONNECT = 1,
public const
CONNECT = 1,
SELECT = 4,
INSERT = 8,
DELETE = 16,

View File

@@ -35,12 +35,12 @@ class Fluent implements IDataSource
{
use Strict;
const REMOVE = false;
public const REMOVE = false;
/** @var array */
public static $masks = [
'SELECT' => ['SELECT', 'DISTINCT', 'FROM', 'WHERE', 'GROUP BY',
'HAVING', 'ORDER BY', 'LIMIT', 'OFFSET'],
'HAVING', 'ORDER BY', 'LIMIT', 'OFFSET', ],
'UPDATE' => ['UPDATE', 'SET', 'WHERE', 'ORDER BY', 'LIMIT'],
'INSERT' => ['INSERT', 'INTO', 'VALUES', 'SELECT'],
'DELETE' => ['DELETE', 'FROM', 'USING', 'WHERE', 'ORDER BY', 'LIMIT'],

View File

@@ -51,14 +51,14 @@ class Helpers
if ($i === 0) {
echo "\n<table class=\"dump\">\n<thead>\n\t<tr>\n\t\t<th>#row</th>\n";
foreach ($row as $col => $foo) {
echo "\t\t<th>" . htmlSpecialChars((string) $col) . "</th>\n";
echo "\t\t<th>" . htmlspecialchars((string) $col) . "</th>\n";
}
echo "\t</tr>\n</thead>\n<tbody>\n";
}
echo "\t<tr>\n\t\t<th>", $i, "</th>\n";
foreach ($row as $col) {
echo "\t\t<td>", htmlSpecialChars((string) $col), "</td>\n";
echo "\t\t<td>", htmlspecialchars((string) $col), "</td>\n";
}
echo "\t</tr>\n";
}
@@ -107,7 +107,7 @@ class Helpers
echo trim($sql) . "\n\n";
} else {
$sql = htmlSpecialChars($sql);
$sql = htmlspecialchars($sql);
$sql = preg_replace_callback($highlighter, function ($m) {
if (!empty($m[1])) { // comment
return '<em style="color:gray">' . $m[1] . '</em>';

View File

@@ -66,7 +66,7 @@ class Column
public function getTable(): Table
{
if (empty($this->info['table']) || !$this->reflector) {
throw new Dibi\Exception("Table is unknown or not available.");
throw new Dibi\Exception('Table is unknown or not available.');
}
return new Table($this->reflector, ['name' => $this->info['table']]);
}

View File

@@ -337,10 +337,10 @@ class Result implements IDataSource
} elseif ($as === '=') { // "record" node
if ($x === null) {
$x = $row->toArray();
$x = &$x[ $assoc[$i + 1] ];
$x = &$x[$assoc[$i + 1]];
$x = null; // prepare child node
} else {
$x = &$x[ $assoc[$i + 1] ];
$x = &$x[$assoc[$i + 1]];
}
} elseif ($as === '@') { // "object" node
@@ -420,7 +420,7 @@ class Result implements IDataSource
}
do {
$data[ (string) $row[$key] ] = $row[$value];
$data[(string) $row[$key]] = $row[$value];
} while ($row = $this->fetch());
return $data;

View File

@@ -114,7 +114,7 @@ trait Strict
$class = (new ReflectionClass($class))->getName();
}
$list = & self::$extMethods[strtolower($name)];
$list = &self::$extMethods[strtolower($name)];
if ($callback === null) { // getter
$cache = &$list[''][$class];
if (isset($cache)) {

View File

@@ -340,8 +340,7 @@ final class Translator
if ($value == '') {
$value = null;
}
// intentionally break omitted
// break omitted
case 'i': // signed int
case 'u': // unsigned int, ignored
if ($value === null) {
@@ -351,7 +350,7 @@ final class Translator
} else {
return (string) (int) $value;
}
// break omitted
case 'f': // float
if ($value === null) {
return 'NULL';
@@ -360,7 +359,7 @@ final class Translator
} else {
return rtrim(rtrim(number_format($value + 0, 10, '.', ''), '0'), '.');
}
// break omitted
case 'd': // date
case 't': // datetime
case 'dt': // datetime
@@ -369,7 +368,7 @@ final class Translator
} else {
return $modifier === 'd' ? $this->driver->escapeDate($value) : $this->driver->escapeDateTime($value);
}
// break omitted
case 'by':
case 'n': // composed identifier name
return $this->identifiers->$value;

View File

@@ -15,7 +15,7 @@ namespace Dibi;
*/
class Type
{
const
public const
TEXT = 's', // as 'string'
BINARY = 'bin',
BOOL = 'b',

View File

@@ -16,25 +16,19 @@ class dibi
{
use Dibi\Strict;
const
public const
AFFECTED_ROWS = 'a',
IDENTIFIER = 'n';
/** version */
const
public const
VERSION = '4.0-dev';
/** sorting order */
const
public const
ASC = 'ASC',
DESC = 'DESC';
/** @var Dibi\Connection[] Connection registry storage for DibiConnection objects */
private static $registry = [];
/** @var Dibi\Connection Current connection */
private static $connection;
/** @var string Last SQL command @see dibi::query() */
public static $sql;
@@ -50,6 +44,12 @@ class dibi
/** @var string Default dibi driver */
public static $defaultDriver = 'mysqli';
/** @var Dibi\Connection[] Connection registry storage for DibiConnection objects */
private static $registry = [];
/** @var Dibi\Connection Current connection */
private static $connection;
/**
* Static class - cannot be instantiated.

View File

@@ -5,6 +5,7 @@
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
if (PHP_VERSION_ID < 70100) {
throw new Exception('Dibi requires PHP 7.1 or newer.');

View File

@@ -65,7 +65,7 @@ Assert::equal([
// more complex association array
function query($conn)
{
return $conn->query(in_array($conn->getConfig('system'), ['odbc', 'sqlsrv']) ? '
return $conn->query(in_array($conn->getConfig('system'), ['odbc', 'sqlsrv'], true) ? '
SELECT products.title, customers.name, orders.amount
FROM ([products]
INNER JOIN [orders] ON [products.product_id] = [orders.product_id])
@@ -172,11 +172,11 @@ Assert::equal([
Assert::equal([
new Row(['title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0)]),
new Row([
'title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0)]),
'title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0), ]),
new Row([
'title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0)]),
'title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0), ]),
new Row([
'title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0)]),
'title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0), ]),
], query($conn)->fetchAssoc('@,='));

View File

@@ -8,29 +8,29 @@ require __DIR__ . '/bootstrap.php';
class MockDriver extends Dibi\Drivers\SqlsrvDriver
{
function __construct()
public function __construct()
{
}
function connect(array & $config): void
public function connect(array &$config): void
{
}
function query(string $sql): ?Dibi\ResultDriver
public function query(string $sql): ?Dibi\ResultDriver
{
return $this;
}
function getResultColumns(): array
public function getResultColumns(): array
{
return [];
}
function fetch(bool $type): ?array
public function fetch(bool $type): ?array
{
return null;
}

View File

@@ -30,7 +30,7 @@ Assert::equal([
// more complex association array
if (!in_array($config['system'], ['odbc', 'sqlsrv'])) {
if (!in_array($config['system'], ['odbc', 'sqlsrv'], true)) {
$res = $conn->select(['products.title' => 'title', 'customers.name' => 'name'])->select('orders.amount')->as('amount')
->from('products')
->innerJoin('orders')->using('(product_id)')

View File

@@ -27,7 +27,7 @@ Assert::same(
);
if (!in_array($config['driver'], ['sqlite3', 'pdo', 'sqlsrv'])) {
if (!in_array($config['driver'], ['sqlite3', 'pdo', 'sqlsrv'], true)) {
Assert::same(
['products.product_id', 'orders.order_id', 'customers.name', 'xXx'],
$info->getColumnNames(true)
@@ -38,18 +38,18 @@ if (!in_array($config['driver'], ['sqlite3', 'pdo', 'sqlsrv'])) {
$columns = $info->getColumns();
Assert::same('product_id', $columns[0]->getName());
if (!in_array($config['driver'], ['sqlite3', 'pdo', 'sqlsrv'])) {
if (!in_array($config['driver'], ['sqlite3', 'pdo', 'sqlsrv'], true)) {
Assert::same('products', $columns[0]->getTableName());
}
Assert::null($columns[0]->getVendorInfo('xxx'));
if (!in_array($config['system'], ['sqlite', 'sqlsrv'])) {
if (!in_array($config['system'], ['sqlite', 'sqlsrv'], true)) {
Assert::same('i', $columns[0]->getType());
}
Assert::null($columns[0]->isNullable());
Assert::same('xXx', $columns[3]->getName());
Assert::null($columns[3]->getTableName());
if (!in_array($config['system'], ['sqlite', 'sqlsrv'])) {
if (!in_array($config['system'], ['sqlite', 'sqlsrv'], true)) {
Assert::same('i', $columns[0]->getType());
}
Assert::null($columns[3]->isNullable());

View File

@@ -9,12 +9,12 @@ require __DIR__ . '/bootstrap.php';
class MockResult extends Dibi\Result
{
function __construct()
public function __construct()
{
}
function test($row)
public function test($row)
{
$normalize = new ReflectionMethod(Dibi\Result::class, 'normalize');
$normalize->setAccessible(true);

View File

@@ -11,7 +11,7 @@ use Tester\Assert;
require __DIR__ . '/bootstrap.php';
$conn = new Dibi\Connection($config);
$conn->loadFile(__DIR__ . "/data/sqlsrv.insert.sql");
$conn->loadFile(__DIR__ . '/data/sqlsrv.insert.sql');
for ($i = 1; $i <= 5; $i++) {
$conn->query('INSERT INTO %n DEFAULT VALUES', 'aaa');

View File

@@ -12,10 +12,10 @@ class TestClass
public $public;
protected $protected;
public static $publicStatic;
protected $protected;
public function publicMethod()
{

View File

@@ -97,7 +97,7 @@ Assert::same(
$conn->translate('TEST %and', ['[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()'])
);
//
$where = [];
$where[] = '[age] > 20';
$where[] = '[email] IS NOT NULL';
@@ -158,7 +158,7 @@ if ($config['system'] === 'odbc') {
Assert::same(
reformat([
'sqlsrv' => 'SELECT * FROM [products] OFFSET 1 ROWS FETCH NEXT 2 ROWS ONLY',
'SELECT * FROM [products] LIMIT 2 OFFSET 1'
'SELECT * FROM [products] LIMIT 2 OFFSET 1',
]),
$conn->translate('SELECT * FROM [products] %lmt %ofs', 2, 1)
);
@@ -551,7 +551,7 @@ Assert::same(
);
setLocale(LC_ALL, 'czech');
setlocale(LC_ALL, 'czech');
Assert::same(
reformat("UPDATE [colors] SET [color]='blue', [price]=-12.4, [spec]=-9E-005, [spec2]=1000, [spec3]=10000, [spec4]=10000 WHERE [price]=123.5"),

View File

@@ -60,7 +60,7 @@ function reformat($s)
return strtr($s, '[]', '``');
} elseif ($config['system'] === 'postgre') {
return strtr($s, '[]', '""');
} elseif (in_array($config['system'], ['odbc', 'sqlite', 'sqlsrv'])) {
} elseif (in_array($config['system'], ['odbc', 'sqlite', 'sqlsrv'], true)) {
return $s;
} else {
trigger_error("Unsupported driver $config[system]", E_USER_WARNING);

View File

@@ -17,7 +17,7 @@ $conn->loadFile(__DIR__ . "/data/$config[system].sql");
$e = Assert::exception(function () use ($conn) {
$conn->query('SELECT');
}, Dibi\DriverException::class, "%a% error in your SQL syntax;%a%", 1064);
}, Dibi\DriverException::class, '%a% error in your SQL syntax;%a%', 1064);
Assert::same('SELECT', $e->getSql());