Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PhpStan fixes #363

Merged
merged 1 commit into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"require-dev": {
"tracy/tracy": "~2.2",
"nette/tester": "~2.0",
"nette/di": "^3.0",
"phpstan/phpstan": "^0.12"
},
"replace": {
Expand Down
3 changes: 2 additions & 1 deletion src/Dibi/Bridges/Tracy/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -129,7 +130,7 @@ public function getPanel(): ?string

$s .= '</td><td class="tracy-DibiProfiler-sql">' . Helpers::dump(strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql, true);
if ($explain) {
$s .= "<div id='tracy-debug-DibiProfiler-row-$counter' class='tracy-collapsed'>{$explain}</div>";
$s .= "<div id='tracy-debug-DibiProfiler-row-" . ($counter ?? 0) . "' class='tracy-collapsed'>{$explain}</div>";
}
if ($event->source) {
$s .= Tracy\Helpers::editorLink($event->source[0], $event->source[1]);//->class('tracy-DibiProfiler-source');
Expand Down
1 change: 1 addition & 0 deletions src/Dibi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions src/Dibi/Drivers/FirebirdDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/FirebirdResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class FirebirdResult implements Dibi\ResultDriver
{
use Dibi\Strict;

/** @var resource */
/** @var resource|null */
private $resultSet;

/** @var bool */
Expand Down
22 changes: 14 additions & 8 deletions src/Dibi/Drivers/MySqliDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
3 changes: 2 additions & 1 deletion src/Dibi/Drivers/MySqliResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Dibi/Drivers/OdbcDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class OdbcDriver implements Dibi\Driver
{
use Dibi\Strict;

/** @var resource */
/** @var resource|null */
private $connection;

/** @var int|null Affected rows */
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/OracleDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class OracleDriver implements Dibi\Driver
{
use Dibi\Strict;

/** @var resource */
/** @var resource|null */
private $connection;

/** @var bool */
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/OracleResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
6 changes: 6 additions & 0 deletions src/Dibi/Fluent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/Dibi/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Result implements IDataSource
{
use Strict;

/** @var ResultDriver */
/** @var ResultDriver|null */
private $driver;

/** @var array Translate table */
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -292,6 +292,7 @@ final public function fetchAssoc(string $assoc): array
} while ($row = $this->fetch());

unset($x);
/** @var mixed[] $data */
return $data;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Dibi/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down