Skip to content

Commit

Permalink
Fix psalm issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Jan 29, 2024
1 parent fa3c327 commit 4aa78ac
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
19 changes: 8 additions & 11 deletions src/DDLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function addCommentOnColumn(string $table, string $column, string $commen
. $this->quoter->quoteColumnName($column)
. ' '
. $this->quoter->quoteColumnName($column)
. (empty($definition) ? '' : ' ' . $definition)
. ($definition === '' ? '' : ' ' . $definition)
. ' COMMENT '
. (string) $this->quoter->quoteValue($comment);

Expand Down Expand Up @@ -84,7 +84,7 @@ public function createIndex(
string $indexType = null,
string $indexMethod = null
): string {
return 'CREATE ' . ($indexType ? ($indexType . ' ') : '') . 'INDEX '
return 'CREATE ' . ($indexType !== null ? "$indexType " : '') . 'INDEX '
. $this->quoter->quoteTableName($name)
. ($indexMethod !== null ? " USING $indexMethod" : '')
. ' ON ' . $this->quoter->quoteTableName($table)
Expand Down Expand Up @@ -173,21 +173,18 @@ public function renameColumn(string $table, string $oldName, string $newName): s
*/
public function getColumnDefinition(string $table, string $column): string
{
$result = '';
$sql = $this->schema->getTableSchema($table)?->getCreateSql();

if (empty($sql)) {
if ($sql === null) {
return '';
}

if (preg_match_all('/^\s*([`"])(.*?)\\1\s+(.*?),?$/m', $sql, $matches)) {
foreach ($matches[2] as $i => $c) {
if ($c === $column) {
$result = $matches[3][$i];
}
}
$quotedColumn = preg_quote($column, '/');

if (preg_match("/^\s*([`\"])$quotedColumn\\1\s+(.*?),?$/m", $sql, $matches) !== 1) {
return '';
}

return $result;
return $matches[2];
}
}
3 changes: 2 additions & 1 deletion src/DMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function upsert(
}
}

/** @psalm-suppress RiskyTruthyFalsyComparison */
if (empty($updateColumns)) {
return str_replace('INSERT INTO', 'INSERT IGNORE INTO', $insertSql);
}
Expand Down Expand Up @@ -107,7 +108,7 @@ public function upsert(
*/
protected function prepareInsertValues(string $table, QueryInterface|array $columns, array $params = []): array
{
if (empty($columns)) {
if ($columns === []) {
return [[], [], 'VALUES ()', []];
}

Expand Down
10 changes: 5 additions & 5 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
* key: string,
* default: string|null,
* extra: string,
* extra_default_value: string|null,
* extra_default_value: string,
* privileges: string,
* comment: string
* }
Expand Down Expand Up @@ -161,7 +161,7 @@ public function findUniqueIndexes(TableSchemaInterface $table): array
$uniqueIndexes = [];
$regexp = '/UNIQUE KEY\s+[`"](.+)[`"]\s*\(([`"].+[`"])+\)/mi';

if (preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER)) {
if (preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER) > 0) {
foreach ($matches as $match) {
$indexName = $match[1];
$indexColumns = array_map('trim', preg_split('/[`"],[`"]/', trim($match[2], '`"')));
Expand Down Expand Up @@ -529,7 +529,7 @@ protected function loadColumnSchema(array $info): ColumnSchemaInterface
$extra = $info['extra'];
if (
empty($extra)
&& !empty($info['extra_default_value'])
&& $info['extra_default_value'] !== ''
&& !str_starts_with($info['extra_default_value'], '\'')
&& in_array($column->getType(), [
self::TYPE_CHAR, self::TYPE_STRING, self::TYPE_TEXT,
Expand Down Expand Up @@ -575,7 +575,7 @@ private function normalizeDefaultValue(?string $defaultValue, ColumnSchemaInterf
return new Expression('CURRENT_TIMESTAMP' . (!empty($matches[1]) ? '(' . $matches[1] . ')' : ''));
}

if (!empty($column->getExtra()) && !empty($defaultValue)) {
if ($defaultValue !== '' && $column->getExtra() !== '') {
return new Expression($defaultValue);
}

Expand Down Expand Up @@ -927,7 +927,7 @@ private function getJsonColumns(TableSchemaInterface $table): array
$result = [];
$regexp = '/json_valid\([\`"](.+)[\`"]\s*\)/mi';

if (preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER)) {
if (preg_match_all($regexp, $sql, $matches, PREG_SET_ORDER) > 0) {
foreach ($matches as $match) {
$result[] = $match[1];
}
Expand Down

0 comments on commit 4aa78ac

Please sign in to comment.