From 9fae32aae71a0959e4d2069fad56c803925c31ba Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Sat, 3 Feb 2024 20:26:21 +0700 Subject: [PATCH] Minor refactoring of `DDLQueryBuilder` and `Schema` + Fix psalm issues (#319) --- .github/workflows/static.yml | 6 ++++++ CHANGELOG.md | 1 + composer.json | 2 +- psalm.xml | 3 +++ psalm4.xml | 17 +++++++++++++++++ src/DDLQueryBuilder.php | 6 +++--- src/Schema.php | 6 +++--- 7 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 psalm4.xml diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 0b170765..b7778798 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -39,6 +39,7 @@ jobs: - '8.0' - '8.1' - '8.2' + - '8.3' steps: - name: Checkout. @@ -70,4 +71,9 @@ jobs: run: composer update --no-interaction --no-progress --optimize-autoloader --ansi - name: Static analysis. + if: ${{ matrix.php != '8.0' }} run: vendor/bin/psalm --config=${{ inputs.psalm-config }} --shepherd --stats --output-format=github --php-version=${{ matrix.php }} + + - name: Static analysis. + if: ${{ matrix.php == '8.0' }} + run: vendor/bin/psalm --config=psalm4.xml --shepherd --stats --output-format=github --php-version=${{ matrix.php }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 148a6d14..51170375 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Enh #312: Change property `Schema::$typeMap` to constant `Schema::TYPE_MAP` (@Tigrov) - Bug #314: Fix `Command::insertWithReturningPks()` method for empty values (@Tigrov) +- Enh #319: Minor refactoring of `DDLQueryBuilder` and `Schema` (@Tigrov) ## 1.1.0 November 12, 2023 diff --git a/composer.json b/composer.json index 1740e712..04a04826 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,7 @@ "rector/rector": "^0.19", "roave/infection-static-analysis-plugin": "^1.16", "spatie/phpunit-watcher": "^1.23", - "vimeo/psalm": "^4.3|^5.6", + "vimeo/psalm": "^4.30|^5.20", "yiisoft/aliases": "^2.0", "yiisoft/log-target-file": "^2.0", "yiisoft/cache-file": "^3.1", diff --git a/psalm.xml b/psalm.xml index 23bfcce1..5001d0a7 100644 --- a/psalm.xml +++ b/psalm.xml @@ -14,4 +14,7 @@ + + + diff --git a/psalm4.xml b/psalm4.xml new file mode 100644 index 00000000..23bfcce1 --- /dev/null +++ b/psalm4.xml @@ -0,0 +1,17 @@ + + + + + + + + + diff --git a/src/DDLQueryBuilder.php b/src/DDLQueryBuilder.php index f812754c..8e3caf17 100644 --- a/src/DDLQueryBuilder.php +++ b/src/DDLQueryBuilder.php @@ -84,9 +84,9 @@ public function createIndex( string $indexType = null, string $indexMethod = null ): string { - return 'CREATE ' . ($indexType ? ($indexType . ' ') : '') . 'INDEX ' + return 'CREATE ' . (!empty($indexType) ? $indexType . ' ' : '') . 'INDEX ' . $this->quoter->quoteTableName($name) - . ($indexMethod !== null ? " USING $indexMethod" : '') + . (!empty($indexMethod) ? " USING $indexMethod" : '') . ' ON ' . $this->quoter->quoteTableName($table) . ' (' . $this->queryBuilder->buildColumns($columns) . ')'; } @@ -180,7 +180,7 @@ public function getColumnDefinition(string $table, string $column): string return ''; } - if (preg_match_all('/^\s*([`"])(.*?)\\1\s+(.*?),?$/m', $sql, $matches)) { + if (preg_match_all('/^\s*([`"])(.*?)\\1\s+(.*?),?$/m', $sql, $matches) > 0) { foreach ($matches[2] as $i => $c) { if ($c === $column) { $result = $matches[3][$i]; diff --git a/src/Schema.php b/src/Schema.php index e6e9b5b3..e377f425 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -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], '`"'))); @@ -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 (!empty($defaultValue) && !empty($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]; }