From eaa57fbe167039c60010fca35d42bbe94c4a9718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Wed, 22 Apr 2020 13:56:25 +0200 Subject: [PATCH 1/4] PhpStan fixes --- composer.json | 1 + src/Dibi/Bridges/Tracy/Panel.php | 3 ++- src/Dibi/Connection.php | 1 + src/Dibi/Drivers/FirebirdDriver.php | 5 +++-- src/Dibi/Drivers/FirebirdResult.php | 2 +- src/Dibi/Drivers/MySqliDriver.php | 22 ++++++++++++++-------- src/Dibi/Drivers/MySqliResult.php | 3 ++- src/Dibi/Drivers/OdbcDriver.php | 3 ++- src/Dibi/Drivers/OracleDriver.php | 2 +- src/Dibi/Drivers/OracleResult.php | 2 +- src/Dibi/Fluent.php | 6 ++++++ src/Dibi/Helpers.php | 2 +- src/Dibi/Result.php | 5 +++-- src/Dibi/Translator.php | 4 ++-- 14 files changed, 40 insertions(+), 21 deletions(-) diff --git a/composer.json b/composer.json index c0947cbe5..aece27b47 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,7 @@ "require-dev": { "tracy/tracy": "~2.2", "nette/tester": "~2.0", + "nette/di": "^3.0", "phpstan/phpstan": "^0.12" }, "replace": { diff --git a/src/Dibi/Bridges/Tracy/Panel.php b/src/Dibi/Bridges/Tracy/Panel.php index 0c59bbd1c..574961766 100644 --- a/src/Dibi/Bridges/Tracy/Panel.php +++ b/src/Dibi/Bridges/Tracy/Panel.php @@ -104,6 +104,7 @@ public function getPanel(): ?string return null; } + /** @var float|null $totalTime */ $totalTime = $s = null; foreach ($this->events as $event) { $totalTime += $event->time; @@ -129,7 +130,7 @@ public function getPanel(): ?string $s .= '' . Helpers::dump(strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql, true); if ($explain) { - $s .= "
{$explain}
"; + $s .= "
{$explain}
"; } if ($event->source) { $s .= Tracy\Helpers::editorLink($event->source[0], $event->source[1]);//->class('tracy-DibiProfiler-source'); diff --git a/src/Dibi/Connection.php b/src/Dibi/Connection.php index 141c6f0d8..b87d07af2 100644 --- a/src/Dibi/Connection.php +++ b/src/Dibi/Connection.php @@ -126,6 +126,7 @@ final public function connect(): void $event = $this->onEvent ? new Event($this, Event::CONNECT) : null; try { + /** @var string $class */ $this->driver = new $class($this->config); $this->translator = new Translator($this); diff --git a/src/Dibi/Drivers/FirebirdDriver.php b/src/Dibi/Drivers/FirebirdDriver.php index eb2ff8173..dfca9f14a 100644 --- a/src/Dibi/Drivers/FirebirdDriver.php +++ b/src/Dibi/Drivers/FirebirdDriver.php @@ -30,7 +30,7 @@ class FirebirdDriver implements Dibi\Driver public const ERROR_EXCEPTION_THROWN = -836; - /** @var resource */ + /** @var resource|null */ private $connection; /** @var resource|null */ @@ -91,6 +91,7 @@ public function disconnect(): void public function query(string $sql): ?Dibi\ResultDriver { $resource = $this->inTransaction ? $this->transaction : $this->connection; + /** @var resource|false $res */ $res = ibase_query($resource, $sql); if ($res === false) { @@ -136,7 +137,7 @@ public function begin(string $savepoint = null): void if ($savepoint !== null) { throw new Dibi\NotSupportedException('Savepoints are not supported in Firebird/Interbase.'); } - $this->transaction = ibase_trans($this->getResource()); + $this->transaction = ibase_trans((int) $this->getResource()); $this->inTransaction = true; } diff --git a/src/Dibi/Drivers/FirebirdResult.php b/src/Dibi/Drivers/FirebirdResult.php index b4aaa8f4c..109956f1c 100644 --- a/src/Dibi/Drivers/FirebirdResult.php +++ b/src/Dibi/Drivers/FirebirdResult.php @@ -20,7 +20,7 @@ class FirebirdResult implements Dibi\ResultDriver { use Dibi\Strict; - /** @var resource */ + /** @var resource|null */ private $resultSet; /** @var bool */ diff --git a/src/Dibi/Drivers/MySqliDriver.php b/src/Dibi/Drivers/MySqliDriver.php index 847fc24ac..b0aa0abc4 100644 --- a/src/Dibi/Drivers/MySqliDriver.php +++ b/src/Dibi/Drivers/MySqliDriver.php @@ -148,20 +148,26 @@ public function query(string $sql): ?Dibi\ResultDriver } + /** + * @param int|string $code + */ public static function createException(string $message, $code, string $sql): Dibi\DriverException { - if (in_array($code, [1216, 1217, 1451, 1452, 1701], true)) { - return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql); + if (is_int($code) === true) { + if (in_array($code, [1216, 1217, 1451, 1452, 1701], true)) { + return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql); - } elseif (in_array($code, [1062, 1557, 1569, 1586], true)) { - return new Dibi\UniqueConstraintViolationException($message, $code, $sql); + } + if (in_array($code, [1062, 1557, 1569, 1586], true)) { + return new Dibi\UniqueConstraintViolationException($message, $code, $sql); - } elseif (in_array($code, [1048, 1121, 1138, 1171, 1252, 1263, 1566], true)) { - return new Dibi\NotNullConstraintViolationException($message, $code, $sql); + } + if (in_array($code, [1048, 1121, 1138, 1171, 1252, 1263, 1566], true)) { + return new Dibi\NotNullConstraintViolationException($message, $code, $sql); - } else { - return new Dibi\DriverException($message, $code, $sql); + } } + return new Dibi\DriverException($message, $code, $sql); } diff --git a/src/Dibi/Drivers/MySqliResult.php b/src/Dibi/Drivers/MySqliResult.php index 67a5a86e2..8452288a4 100644 --- a/src/Dibi/Drivers/MySqliResult.php +++ b/src/Dibi/Drivers/MySqliResult.php @@ -41,7 +41,8 @@ public function __construct(\mysqli_result $resultSet, bool $buffered) */ public function __destruct() { - if ($this->autoFree && $this->getResultResource()) { + if ($this->autoFree) { + $this->getResultResource(); @$this->free(); } } diff --git a/src/Dibi/Drivers/OdbcDriver.php b/src/Dibi/Drivers/OdbcDriver.php index 6af92a730..a7c7e33b7 100644 --- a/src/Dibi/Drivers/OdbcDriver.php +++ b/src/Dibi/Drivers/OdbcDriver.php @@ -27,7 +27,7 @@ class OdbcDriver implements Dibi\Driver { use Dibi\Strict; - /** @var resource */ + /** @var resource|null */ private $connection; /** @var int|null Affected rows */ @@ -87,6 +87,7 @@ public function disconnect(): void public function query(string $sql): ?Dibi\ResultDriver { $this->affectedRows = null; + /** @var resource|false $res */ $res = @odbc_exec($this->connection, $sql); // intentionally @ if ($res === false) { diff --git a/src/Dibi/Drivers/OracleDriver.php b/src/Dibi/Drivers/OracleDriver.php index ba2a3d551..f09f85cca 100644 --- a/src/Dibi/Drivers/OracleDriver.php +++ b/src/Dibi/Drivers/OracleDriver.php @@ -29,7 +29,7 @@ class OracleDriver implements Dibi\Driver { use Dibi\Strict; - /** @var resource */ + /** @var resource|null */ private $connection; /** @var bool */ diff --git a/src/Dibi/Drivers/OracleResult.php b/src/Dibi/Drivers/OracleResult.php index 65b90a5cc..8a76b6830 100644 --- a/src/Dibi/Drivers/OracleResult.php +++ b/src/Dibi/Drivers/OracleResult.php @@ -111,7 +111,7 @@ public function getResultColumns(): array public function getResultResource() { $this->autoFree = false; - return is_resource($this->resultSet) ? $this->resultSet : null; + return $this->resultSet; } diff --git a/src/Dibi/Fluent.php b/src/Dibi/Fluent.php index fc8c0d6fb..4cb809086 100644 --- a/src/Dibi/Fluent.php +++ b/src/Dibi/Fluent.php @@ -32,6 +32,12 @@ * @method Fluent and(...$cond) * @method Fluent or(...$cond) * @method Fluent using(...$cond) + * @method Fluent update(...$cond) + * @method Fluent insert(...$cond) + * @method Fluent delete(...$cond) + * @method Fluent into(...$cond) + * @method Fluent values(...$cond) + * @method Fluent set(...$args) * @method Fluent asc() * @method Fluent desc() */ diff --git a/src/Dibi/Helpers.php b/src/Dibi/Helpers.php index c212d7f3f..39dc284e9 100644 --- a/src/Dibi/Helpers.php +++ b/src/Dibi/Helpers.php @@ -290,7 +290,7 @@ public static function intVal($value): int if (is_int($value)) { return $value; } elseif (is_string($value) && preg_match('#-?\d++\z#A', $value)) { - if (is_float($value * 1)) { + if ($value > PHP_INT_MAX) { throw new Exception("Number $value is greater than integer."); } return (int) $value; diff --git a/src/Dibi/Result.php b/src/Dibi/Result.php index c7a66f74b..ab4dc2356 100644 --- a/src/Dibi/Result.php +++ b/src/Dibi/Result.php @@ -19,7 +19,7 @@ class Result implements IDataSource { use Strict; - /** @var ResultDriver */ + /** @var ResultDriver|null */ private $driver; /** @var array Translate table */ @@ -251,7 +251,7 @@ final public function fetchAssoc(string $assoc): array } } - if ($as === '->') { // must not be last + if (($as ?? '') === '->') { // must not be last array_pop($assoc); } @@ -292,6 +292,7 @@ final public function fetchAssoc(string $assoc): array } while ($row = $this->fetch()); unset($x); + /** @var mixed[] $data */ return $data; } diff --git a/src/Dibi/Translator.php b/src/Dibi/Translator.php index 6ed3fadd2..7d80b1411 100644 --- a/src/Dibi/Translator.php +++ b/src/Dibi/Translator.php @@ -153,8 +153,8 @@ public function translate(array $args): string $sql = trim(implode(' ', $sql), ' '); - if ($this->errors) { - throw new Exception('SQL translate error: ' . trim(reset($this->errors), '*'), 0, $sql); + if ($this->errors !== []) { + throw new Exception('SQL translate error: ' . trim((string) reset($this->errors), '*'), 0, $sql); } // apply limit From bd470797248cec6be8d252f2dce8355862c5cb40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Thu, 7 May 2020 22:47:11 +0200 Subject: [PATCH 2/4] Fix DG comments --- src/Dibi/Drivers/FirebirdResult.php | 4 ++-- src/Dibi/Translator.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Dibi/Drivers/FirebirdResult.php b/src/Dibi/Drivers/FirebirdResult.php index 109956f1c..79c115a72 100644 --- a/src/Dibi/Drivers/FirebirdResult.php +++ b/src/Dibi/Drivers/FirebirdResult.php @@ -20,7 +20,7 @@ class FirebirdResult implements Dibi\ResultDriver { use Dibi\Strict; - /** @var resource|null */ + /** @var resource */ private $resultSet; /** @var bool */ @@ -104,7 +104,7 @@ public function free(): void public function getResultResource() { $this->autoFree = false; - return is_resource($this->resultSet) ? $this->resultSet : null; + return $this->resultSet; } diff --git a/src/Dibi/Translator.php b/src/Dibi/Translator.php index 7d80b1411..91387fcae 100644 --- a/src/Dibi/Translator.php +++ b/src/Dibi/Translator.php @@ -153,7 +153,7 @@ public function translate(array $args): string $sql = trim(implode(' ', $sql), ' '); - if ($this->errors !== []) { + if ($this->errors) { throw new Exception('SQL translate error: ' . trim((string) reset($this->errors), '*'), 0, $sql); } From 9e1fdd2338bd6bc46149f5c300ece4cb7ab92d3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Thu, 7 May 2020 22:56:01 +0200 Subject: [PATCH 3/4] Reverted by DG codereview --- src/Dibi/Drivers/FirebirdDriver.php | 2 +- src/Dibi/Drivers/OdbcDriver.php | 3 +-- src/Dibi/Drivers/OracleDriver.php | 2 +- src/Dibi/Helpers.php | 2 +- src/Dibi/Result.php | 2 +- 5 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Dibi/Drivers/FirebirdDriver.php b/src/Dibi/Drivers/FirebirdDriver.php index dfca9f14a..9f33d52d1 100644 --- a/src/Dibi/Drivers/FirebirdDriver.php +++ b/src/Dibi/Drivers/FirebirdDriver.php @@ -30,7 +30,7 @@ class FirebirdDriver implements Dibi\Driver public const ERROR_EXCEPTION_THROWN = -836; - /** @var resource|null */ + /** @var resource */ private $connection; /** @var resource|null */ diff --git a/src/Dibi/Drivers/OdbcDriver.php b/src/Dibi/Drivers/OdbcDriver.php index a7c7e33b7..6af92a730 100644 --- a/src/Dibi/Drivers/OdbcDriver.php +++ b/src/Dibi/Drivers/OdbcDriver.php @@ -27,7 +27,7 @@ class OdbcDriver implements Dibi\Driver { use Dibi\Strict; - /** @var resource|null */ + /** @var resource */ private $connection; /** @var int|null Affected rows */ @@ -87,7 +87,6 @@ public function disconnect(): void public function query(string $sql): ?Dibi\ResultDriver { $this->affectedRows = null; - /** @var resource|false $res */ $res = @odbc_exec($this->connection, $sql); // intentionally @ if ($res === false) { diff --git a/src/Dibi/Drivers/OracleDriver.php b/src/Dibi/Drivers/OracleDriver.php index f09f85cca..ba2a3d551 100644 --- a/src/Dibi/Drivers/OracleDriver.php +++ b/src/Dibi/Drivers/OracleDriver.php @@ -29,7 +29,7 @@ class OracleDriver implements Dibi\Driver { use Dibi\Strict; - /** @var resource|null */ + /** @var resource */ private $connection; /** @var bool */ diff --git a/src/Dibi/Helpers.php b/src/Dibi/Helpers.php index 39dc284e9..c212d7f3f 100644 --- a/src/Dibi/Helpers.php +++ b/src/Dibi/Helpers.php @@ -290,7 +290,7 @@ public static function intVal($value): int if (is_int($value)) { return $value; } elseif (is_string($value) && preg_match('#-?\d++\z#A', $value)) { - if ($value > PHP_INT_MAX) { + if (is_float($value * 1)) { throw new Exception("Number $value is greater than integer."); } return (int) $value; diff --git a/src/Dibi/Result.php b/src/Dibi/Result.php index ab4dc2356..7fef8f714 100644 --- a/src/Dibi/Result.php +++ b/src/Dibi/Result.php @@ -19,7 +19,7 @@ class Result implements IDataSource { use Strict; - /** @var ResultDriver|null */ + /** @var ResultDriver */ private $driver; /** @var array Translate table */ From 89591ae76d7ceabdbc4564dd7901282e7f543443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Thu, 7 May 2020 23:00:33 +0200 Subject: [PATCH 4/4] Reverted by DG codereview --- src/Dibi/Drivers/FirebirdResult.php | 2 +- src/Dibi/Drivers/OracleResult.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Dibi/Drivers/FirebirdResult.php b/src/Dibi/Drivers/FirebirdResult.php index 79c115a72..b4aaa8f4c 100644 --- a/src/Dibi/Drivers/FirebirdResult.php +++ b/src/Dibi/Drivers/FirebirdResult.php @@ -104,7 +104,7 @@ public function free(): void public function getResultResource() { $this->autoFree = false; - return $this->resultSet; + return is_resource($this->resultSet) ? $this->resultSet : null; } diff --git a/src/Dibi/Drivers/OracleResult.php b/src/Dibi/Drivers/OracleResult.php index 8a76b6830..65b90a5cc 100644 --- a/src/Dibi/Drivers/OracleResult.php +++ b/src/Dibi/Drivers/OracleResult.php @@ -111,7 +111,7 @@ public function getResultColumns(): array public function getResultResource() { $this->autoFree = false; - return $this->resultSet; + return is_resource($this->resultSet) ? $this->resultSet : null; }