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 #364

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/Dibi/Drivers/FirebirdDriver.php
Original file line number Diff line number Diff line change
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
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)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no reason for this change, in_array checks type

Copy link
Contributor Author

@janbarasek janbarasek May 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter $code can be int or string. In the case of string the if statements can be skipped (better performance and code readability).

Snímek obrazovky 2020-05-07 v 22 35 17

What do you think?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in mysql driver it is always int.

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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes behavior

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested all combinations and I think not. The result of getResultResource() is always true, so it should be called in if statement.

Snímek obrazovky 2020-05-07 v 22 39 54

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is right.

@$this->free();
}
}
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)
janbarasek marked this conversation as resolved.
Show resolved Hide resolved
* @method Fluent asc()
* @method Fluent desc()
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Dibi/Result.php
Original file line number Diff line number Diff line change
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
janbarasek marked this conversation as resolved.
Show resolved Hide resolved
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
2 changes: 1 addition & 1 deletion src/Dibi/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ 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);
throw new Exception('SQL translate error: ' . trim((string) reset($this->errors), '*'), 0, $sql);
Copy link
Owner

@dg dg May 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it changes behavior, can be null or empty array. And original code is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted original condition but kept (string) for PhpStan because function reset() can return mixed value.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For nonempty string[]? I think it must be always string.

}

// apply limit
Expand Down