1
0
mirror of https://github.com/dg/dibi.git synced 2025-07-31 19:30:30 +02:00

coding style: fixes in code

This commit is contained in:
David Grudl
2017-07-21 22:28:27 +02:00
parent ebf0be1fd0
commit 4f75637b63
35 changed files with 116 additions and 101 deletions

View File

@@ -143,7 +143,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

@@ -48,7 +48,6 @@ class Connection
* - run (bool) => enable profiler?
* - file => file to log
* - substitutes (array) => map of driver specific substitutes (under development)
* @param mixed connection parameters
* @param string connection name
* @throws Exception

View File

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

View File

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

View File

@@ -713,9 +713,9 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/
public function getTriggers($table = null)
{
$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);
@@ -786,9 +786,9 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/
public function getProcedures()
{
$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)) {
@@ -804,10 +804,10 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/
public function getGenerators()
{
$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)) {
@@ -823,10 +823,10 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/
public function getFunctions()
{
$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

@@ -357,7 +357,7 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
/**
* Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to
* @return boolean true on success, false if unable to seek to specified record
* @return bool true on success, false if unable to seek to specified record
*/
public function seek($row)
{

View File

@@ -69,12 +69,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 = false;
}
} else {
$count = intval($row[0]);
$count = (int) ($row[0]);
}
return $count;

View File

@@ -274,7 +274,7 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return $value->format("#m/d/Y#");
return $value->format('#m/d/Y#');
}
@@ -287,7 +287,7 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
if (!$value instanceof \DateTime && !$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

@@ -41,7 +41,8 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
private $autocommit = true;
/** @var string Date and datetime format */
private $fmtDate, $fmtDateTime;
private $fmtDate;
private $fmtDateTime;
/** @var int|false Number of affected rows */
private $affectedRows = false;

View File

@@ -348,7 +348,7 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
if (!$value instanceof \DateTime && !$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'");
}
@@ -474,8 +474,7 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
}
break;
}
// intentionally break omitted
// break omitted
case 'odbc':
if ($offset) {
throw new Dibi\NotSupportedException('Offset is not supported by this database.');
@@ -484,8 +483,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

@@ -37,10 +37,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

@@ -105,7 +105,7 @@ class SqlsrvReflector implements Dibi\Reflector
*/
public function getIndexes($table)
{
$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

@@ -38,7 +38,7 @@ class Fluent implements IDataSource
/** @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

@@ -76,7 +76,7 @@ class Column
public function getTable()
{
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

@@ -340,14 +340,12 @@ final class Translator
case 'in': // deprecated
trigger_error('Modifier %in is deprecated, use %iN.', E_USER_DEPRECATED);
// intentionally break omitted
// break omitted
case 'iN': // signed int or null
if ($value == '') {
$value = null;
}
// intentionally break omitted
// break omitted
case 'i': // signed int
case 'u': // unsigned int, ignored
if ($value === null) {
@@ -360,7 +358,7 @@ final class Translator
} else {
return (string) (int) $value;
}
// break omitted
case 'f': // float
if ($value === null) {
return 'NULL';
@@ -369,7 +367,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
@@ -378,7 +376,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

@@ -49,12 +49,6 @@ class dibi
FIELD_DATETIME = Type::DATETIME,
FIELD_TIME = Type::TIME;
/** @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;
@@ -70,6 +64,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

@@ -166,7 +166,7 @@ interface ResultDriver
/**
* Moves cursor position without fetching row.
* @param int the 0-based cursor pos to seek to
* @return boolean true on success, false if unable to seek to specified record
* @return bool true on success, false if unable to seek to specified record
* @throws Exception
*/
function seek($row);

View File

@@ -4,8 +4,8 @@
* @dataProvider ../databases.ini
*/
use Tester\Assert;
use Dibi\Connection;
use Tester\Assert;
require __DIR__ . '/bootstrap.php';

View File

@@ -4,8 +4,8 @@
* @dataProvider ../databases.ini
*/
use Tester\Assert;
use Dibi\Row;
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
@@ -61,9 +61,9 @@ Assert::equal([
// more complex association array
function query($conn) {
return $conn->query(in_array($conn->getConfig('system'), ['odbc', 'sqlsrv']) ? '
function query($conn)
{
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])
@@ -170,11 +170,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

@@ -1,7 +1,7 @@
<?php
use Tester\Assert;
use Dibi\Row;
use Tester\Assert;
require __DIR__ . '/bootstrap.php';

View File

@@ -1,7 +1,7 @@
<?php
use Tester\Assert;
use Dibi\Fluent;
use Tester\Assert;
require __DIR__ . '/bootstrap.php';

View File

@@ -7,23 +7,29 @@ require __DIR__ . '/bootstrap.php';
class MockDriver extends Dibi\Drivers\SqlsrvDriver
{
function __construct()
{}
public function __construct()
{
}
function connect(array &$config)
{}
function query($sql)
public function connect(array &$config)
{
}
public function query($sql)
{
return $this;
}
function getResultColumns()
public function getResultColumns()
{
return [];
}
function fetch($assoc)
public function fetch($assoc)
{
return false;
}

View File

@@ -4,8 +4,8 @@
* @dataProvider ../databases.ini
*/
use Tester\Assert;
use Dibi\Row;
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
@@ -28,7 +28,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

@@ -25,7 +25,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)
@@ -36,18 +36,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

@@ -1,17 +1,19 @@
<?php
use Tester\Assert;
use Dibi\Type;
use Tester\Assert;
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', 'normalize');
$normalize->setAccessible(true);

View File

@@ -9,7 +9,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

@@ -11,27 +11,37 @@ class TestClass
public $public;
protected $protected;
public static $publicStatic;
protected $protected;
public function publicMethod()
{}
{
}
public static function publicMethodStatic()
{}
{
}
protected function protectedMethod()
{}
{
}
protected static function protectedMethodS()
{}
{
}
public function getBar()
{
return 123;
}
public function isFoo()
{
return 456;

View File

@@ -4,8 +4,8 @@
* @dataProvider ../databases.ini
*/
use Tester\Assert;
use Dibi\DateTime;
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
@@ -95,7 +95,7 @@ Assert::same(
$conn->translate('TEST %and', ['[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()'])
);
//
$where = [];
$where[] = '[age] > 20';
$where[] = '[email] IS NOT NULL';
@@ -156,7 +156,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)
);
@@ -549,7 +549,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

@@ -63,7 +63,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

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