diff --git a/src/DDLQueryBuilder.php b/src/DDLQueryBuilder.php index f812754c..10221199 100644 --- a/src/DDLQueryBuilder.php +++ b/src/DDLQueryBuilder.php @@ -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); @@ -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) @@ -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]; } } diff --git a/src/DMLQueryBuilder.php b/src/DMLQueryBuilder.php index 516b76aa..28806500 100644 --- a/src/DMLQueryBuilder.php +++ b/src/DMLQueryBuilder.php @@ -79,6 +79,7 @@ public function upsert( } } + /** @psalm-suppress RiskyTruthyFalsyComparison */ if (empty($updateColumns)) { return str_replace('INSERT INTO', 'INSERT IGNORE INTO', $insertSql); } @@ -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 ()', []]; } diff --git a/src/Schema.php b/src/Schema.php index e6e9b5b3..072bdfb4 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -66,7 +66,7 @@ * key: string, * default: string|null, * extra: string, - * extra_default_value: string|null, + * extra_default_value: string, * privileges: string, * comment: string * } @@ -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], '`"'))); @@ -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, @@ -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); } @@ -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]; }