Skip to content

Commit

Permalink
Increase code-coverage, errorLevel Psalm to 1. (#76)
Browse files Browse the repository at this point in the history
* Increase code-coverage, errorLevel Psalm to 1.
* Fix minor corrections PHP 8.0.
* Add @psalm-type in Schema::class.
* Fix phpdocs.
* Remove psalm-suppress.
  • Loading branch information
terabytesoftw committed Jan 31, 2021
1 parent bdae869 commit 2afcb6b
Show file tree
Hide file tree
Showing 13 changed files with 431 additions and 63 deletions.
2 changes: 1 addition & 1 deletion psalm.xml
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="5"
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand Down
5 changes: 4 additions & 1 deletion src/ColumnSchema.php
Expand Up @@ -11,6 +11,9 @@

use function json_decode;

/**
* The class ColumnSchema for Mysql database.
*/
final class ColumnSchema extends AbstractColumnSchema
{
/**
Expand All @@ -31,7 +34,7 @@ public function phpTypecast($value)
}

if ($this->getType() === Schema::TYPE_JSON) {
return json_decode($value, true, 512, JSON_THROW_ON_ERROR);
return json_decode((string) $value, true, 512, JSON_THROW_ON_ERROR);
}

return parent::phpTypecast($value);
Expand Down
10 changes: 8 additions & 2 deletions src/ColumnSchemaBuilder.php
Expand Up @@ -9,10 +9,16 @@
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Schema\ColumnSchemaBuilder as AbstractColumnSchemaBuilder;

/**
* The class ColumnSchemaBuilder for Mysql database.
*/
final class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder
{
private ConnectionInterface $db;

/**
* @param array|int|string|null $length column size or precision definition.
*/
public function __construct(string $type, $length, ConnectionInterface $db)
{
$this->db = $db;
Expand Down Expand Up @@ -40,7 +46,7 @@ protected function buildAfterString(): string
/** @var Connection $db */
$db = $this->db;

return $this->getAfter() !== null ? ' AFTER ' . $db->quoteColumnName($this->getAfter()) : '';
return $this->getAfter() !== null ? ' AFTER ' . $db->quoteColumnName((string) $this->getAfter()) : '';
}

/**
Expand All @@ -65,7 +71,7 @@ protected function buildCommentString(): string
/** @var Connection $db */
$db = $this->db;

return $this->getComment() !== null ? ' COMMENT ' . $db->quoteValue($this->getComment()) : '';
return $this->getComment() !== null ? ' COMMENT ' . $db->quoteValue((string) $this->getComment()) : '';
}

public function __toString(): string
Expand Down
8 changes: 5 additions & 3 deletions src/Connection.php
Expand Up @@ -11,7 +11,7 @@
use function constant;

/**
* Database connection class prefilled for MYSQL Server.
* The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
*/
final class Connection extends AbstractConnection
{
Expand Down Expand Up @@ -79,8 +79,10 @@ protected function initConnection(): void
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
}

if ($this->getCharset() !== null) {
$pdo->exec('SET NAMES ' . $pdo->quote($this->getCharset()));
$charset = $this->getCharset();

if ($charset !== null) {
$pdo->exec('SET NAMES ' . $pdo->quote($charset));
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/JsonExpressionBuilder.php
Expand Up @@ -18,6 +18,9 @@

use function count;

/**
* The class JsonExpressionBuilder builds {@see JsonExpression} for Mysql database.
*/
final class JsonExpressionBuilder implements ExpressionBuilderInterface
{
use ExpressionBuilderTrait;
Expand All @@ -34,6 +37,10 @@ final class JsonExpressionBuilder implements ExpressionBuilderInterface
*/
public function build(ExpressionInterface $expression, array &$params = []): string
{
/**
* @var JsonExpression $expression
* @var mixed|Query $value
*/
$value = $expression->getValue();

if ($value instanceof Query) {
Expand Down
63 changes: 42 additions & 21 deletions src/QueryBuilder.php
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Expression\ExpressionBuilder;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Expression\JsonExpression;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Query\QueryBuilder as AbstractQueryBuilder;
Expand All @@ -28,6 +29,9 @@
use function trim;
use function version_compare;

/**
* The class QueryBuilder is the query builder for Mysql databases.
*/
final class QueryBuilder extends AbstractQueryBuilder
{
/**
Expand Down Expand Up @@ -88,12 +92,9 @@ public function renameColumn(string $table, string $oldName, string $newName): s
{
$quotedTable = $this->getDb()->quoteTableName($table);

/** @psalm-var array<array-key, string> $row */
$row = $this->getDb()->createCommand('SHOW CREATE TABLE ' . $quotedTable)->queryOne();

if ($row === false) {
throw new Exception("Unable to find column '$oldName' in table '$table'.");
}

if (isset($row['Create Table'])) {
$sql = $row['Create Table'];
} else {
Expand Down Expand Up @@ -129,6 +130,8 @@ public function renameColumn(string $table, string $oldName, string $newName): s
* method, unless a parenthesis is found in the name.
* @param bool $unique whether to add UNIQUE constraint on the created index.
*
* @psalm-param array<array-key, ExpressionInterface|string>|string $columns
*
* @throws Exception|InvalidArgumentException
*
* @return string the SQL statement for creating a new index.
Expand Down Expand Up @@ -239,8 +242,8 @@ public function resetSequence(string $tableName, $value = null): string

if ($value === null) {
$pk = $table->getPrimaryKey();
$key = reset($pk);
$value = $this->getDb()->createCommand("SELECT MAX(`$key`) FROM $tableName")->queryScalar() + 1;
$key = (string) reset($pk);
$value = (int) $this->getDb()->createCommand("SELECT MAX(`$key`) FROM $tableName")->queryScalar() + 1;
} else {
$value = (int) $value;
}
Expand Down Expand Up @@ -270,8 +273,8 @@ public function checkIntegrity(string $schema = '', string $table = '', bool $ch
}

/**
* @param int|object|null $limit
* @param int|object|null $offset
* @param Expression|int|null $limit
* @param Expression|int|null $offset
*
* @return string the LIMIT and OFFSET clauses.
*/
Expand All @@ -280,10 +283,10 @@ public function buildLimit($limit, $offset): string
$sql = '';

if ($this->hasLimit($limit)) {
$sql = 'LIMIT ' . $limit;
$sql = 'LIMIT ' . (string) $limit;

if ($this->hasOffset($offset)) {
$sql .= ' OFFSET ' . $offset;
$sql .= ' OFFSET ' . (string) $offset;
}
} elseif ($this->hasOffset($offset)) {
/**
Expand Down Expand Up @@ -341,13 +344,19 @@ protected function hasOffset($offset): bool
*/
protected function prepareInsertValues(string $table, $columns, array $params = []): array
{
/**
* @var array $names
* @var array $placeholders
*/
[$names, $placeholders, $values, $params] = parent::prepareInsertValues($table, $columns, $params);
if (!$columns instanceof Query && empty($names)) {
$tableSchema = $this->getDb()->getSchema()->getTableSchema($table);
$columns = $tableSchema->getColumns();

if ($tableSchema !== null) {
$columns = $tableSchema->getColumns();
$columns = !empty($tableSchema->getPrimaryKey())
? $tableSchema->getPrimaryKey() : [reset($columns)->getName()];
/** @var string $name */
foreach ($columns as $name) {
$names[] = $this->getDb()->quoteColumnName($name);
$placeholders[] = 'DEFAULT';
Expand Down Expand Up @@ -394,6 +403,7 @@ public function upsert(string $table, $insertColumns, $updateColumns, array &$pa
{
$insertSql = $this->insert($table, $insertColumns, $params);

/** @var array $uniqueNames */
[$uniqueNames, , $updateNames] = $this->prepareUpsertColumns($table, $insertColumns, $updateColumns);

if (empty($uniqueNames)) {
Expand All @@ -402,14 +412,20 @@ public function upsert(string $table, $insertColumns, $updateColumns, array &$pa

if ($updateColumns === true) {
$updateColumns = [];
/** @var string $name */
foreach ($updateNames as $name) {
$updateColumns[$name] = new Expression('VALUES(' . $this->getDb()->quoteColumnName($name) . ')');
}
} elseif ($updateColumns === false) {
$name = $this->getDb()->quoteColumnName(reset($uniqueNames));
$columnName = (string) reset($uniqueNames);
$name = $this->getDb()->quoteColumnName($columnName);
$updateColumns = [$name => new Expression($this->getDb()->quoteTableName($table) . '.' . $name)];
}

/**
* @psalm-var array<array-key, mixed> $updates
* @psalm-var array<string, ExpressionInterface|string> $updateColumns
*/
[$updates, $params] = $this->prepareUpdateSets($table, $updateColumns, $params);

return $insertSql . ' ON DUPLICATE KEY UPDATE ' . implode(', ', $updates);
Expand Down Expand Up @@ -516,20 +532,17 @@ public function dropCommentFromTable(string $table): string
*
* @throws Exception|Throwable in case when table does not contain column.
*
* @return string|null the column definition.
* @return string the column definition.
*/
private function getColumnDefinition(string $table, string $column): ?string
private function getColumnDefinition(string $table, string $column): string
{
$result = null;
$result = '';

$quotedTable = $this->getDb()->quoteTableName($table);

/** @var array<array-key, string> $row */
$row = $this->getDb()->createCommand('SHOW CREATE TABLE ' . $quotedTable)->queryOne();

if ($row === false) {
throw new Exception("Unable to find column '$column' in table '$table'.");
}

if (!isset($row['Create Table'])) {
$row = array_values($row);
$sql = $row[1];
Expand Down Expand Up @@ -643,8 +656,16 @@ private function defaultTimeTypeMap(): array
*/
private function supportsFractionalSeconds(): bool
{
$version = $this->getDb()->getSlavePdo()->getAttribute(PDO::ATTR_SERVER_VERSION);
$result = false;

$slavePdo = $this->getDb()->getSlavePdo();

return version_compare($version, '5.6.4', '>=');
if ($slavePdo !== null) {
/** @var string $version */
$version = $slavePdo->getAttribute(PDO::ATTR_SERVER_VERSION);
$result = version_compare($version, '5.6.4', '>=');
}

return $result;
}
}

0 comments on commit 2afcb6b

Please sign in to comment.