From 8da62b7a481fa6aeb4309bb8edde0e56c9c09a1a Mon Sep 17 00:00:00 2001 From: Tigrov Date: Mon, 29 Jan 2024 13:11:52 +0700 Subject: [PATCH 01/10] Fix psalm issues --- src/Command.php | 3 ++- src/DDLQueryBuilder.php | 16 ++++++++-------- src/DMLQueryBuilder.php | 1 + src/Dsn.php | 2 +- src/Quoter.php | 2 +- src/Schema.php | 2 +- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Command.php b/src/Command.php index 69e30f76d..5f8f71af4 100644 --- a/src/Command.php +++ b/src/Command.php @@ -14,8 +14,9 @@ final class Command extends AbstractPdoCommand { public function insertWithReturningPks(string $table, array $columns): bool|array { + /** @psalm-suppress RiskyTruthyFalsyComparison */ if (empty($this->db->getSchema()->getTableSchema($table)?->getPrimaryKey())) { - if (parent::insert($table, $columns)->execute() === 0) { + if ($this->insert($table, $columns)->execute() === 0) { return false; } diff --git a/src/DDLQueryBuilder.php b/src/DDLQueryBuilder.php index ad610ec0f..6796a2c43 100644 --- a/src/DDLQueryBuilder.php +++ b/src/DDLQueryBuilder.php @@ -191,17 +191,17 @@ private function buildAddCommentSql(string $comment, string $table, string $colu throw new InvalidArgumentException("Table not found: $table"); } - $schemaName = $tableSchema->getSchemaName() + $schemaName = $tableSchema->getSchemaName() !== null ? "N'" . (string) $tableSchema->getSchemaName() . "'" : 'SCHEMA_NAME()'; $tableName = 'N' . (string) $this->quoter->quoteValue($tableSchema->getName()); - $columnName = $column ? 'N' . (string) $this->quoter->quoteValue($column) : null; + $columnName = $column !== null ? 'N' . (string) $this->quoter->quoteValue($column) : null; $comment = 'N' . (string) $this->quoter->quoteValue($comment); $functionParams = " @name = N'MS_description', @value = $comment, @level0type = N'SCHEMA', @level0name = $schemaName, @level1type = N'TABLE', @level1name = $tableName" - . ($column ? ", @level2type = N'COLUMN', @level2name = $columnName" : '') . ';'; + . ($column !== null ? ", @level2type = N'COLUMN', @level2name = $columnName" : '') . ';'; return " IF NOT EXISTS ( @@ -210,7 +210,7 @@ private function buildAddCommentSql(string $comment, string $table, string $colu N'MS_description', 'SCHEMA', $schemaName, 'TABLE', $tableName, - " . ($column ? "'COLUMN', $columnName " : ' DEFAULT, DEFAULT ') . " + " . ($column !== null ? "'COLUMN', $columnName " : ' DEFAULT, DEFAULT ') . " ) ) EXEC sys.sp_addextendedproperty $functionParams @@ -242,10 +242,10 @@ private function buildRemoveCommentSql(string $table, string $column = null): st throw new InvalidArgumentException("Table not found: $table"); } - $schemaName = $tableSchema->getSchemaName() + $schemaName = $tableSchema->getSchemaName() !== null ? "N'" . (string) $tableSchema->getSchemaName() . "'" : 'SCHEMA_NAME()'; $tableName = 'N' . (string) $this->quoter->quoteValue($tableSchema->getName()); - $columnName = $column ? 'N' . (string) $this->quoter->quoteValue($column) : null; + $columnName = $column !== null ? 'N' . (string) $this->quoter->quoteValue($column) : null; return " IF EXISTS ( @@ -254,14 +254,14 @@ private function buildRemoveCommentSql(string $table, string $column = null): st N'MS_description', 'SCHEMA', $schemaName, 'TABLE', $tableName, - " . ($column ? "'COLUMN', $columnName " : ' DEFAULT, DEFAULT ') . " + " . ($column !== null ? "'COLUMN', $columnName " : ' DEFAULT, DEFAULT ') . " ) ) EXEC sys.sp_dropextendedproperty @name = N'MS_description', @level0type = N'SCHEMA', @level0name = $schemaName, @level1type = N'TABLE', @level1name = $tableName" - . ($column ? ", @level2type = N'COLUMN', @level2name = $columnName" : '') . ';'; + . ($column !== null ? ", @level2type = N'COLUMN', @level2name = $columnName" : '') . ';'; } /** diff --git a/src/DMLQueryBuilder.php b/src/DMLQueryBuilder.php index 4d68e3d62..d0d33393d 100644 --- a/src/DMLQueryBuilder.php +++ b/src/DMLQueryBuilder.php @@ -35,6 +35,7 @@ public function insertWithReturningPks(string $table, QueryInterface|array $colu $tableSchema = $this->schema->getTableSchema($table); $primaryKeys = $tableSchema?->getPrimaryKey(); + /** @psalm-suppress RiskyTruthyFalsyComparison */ if (empty($primaryKeys)) { return $this->insert($table, $columns, $params); } diff --git a/src/Dsn.php b/src/Dsn.php index 24137bb01..b786aff14 100644 --- a/src/Dsn.php +++ b/src/Dsn.php @@ -45,7 +45,7 @@ public function asString(): string $server = "Server=$this->host;"; } - if (!empty($this->databaseName)) { + if ($this->databaseName !== null) { $dsn = "$this->driver:" . $server . "Database=$this->databaseName"; } else { $dsn = "$this->driver:" . $server; diff --git a/src/Quoter.php b/src/Quoter.php index 2a7fd735d..5ab8d06c8 100644 --- a/src/Quoter.php +++ b/src/Quoter.php @@ -25,7 +25,7 @@ public function quoteColumnName(string $name): string public function getTableNameParts(string $name, bool $withColumn = false): array { - if (preg_match_all('/([^.\[\]]+)|\[([^\[\]]+)]/', $name, $matches)) { + if (preg_match_all('/([^.\[\]]+)|\[([^\[\]]+)]/', $name, $matches) > 0) { $parts = array_slice($matches[0], -4, 4); } else { $parts = [$name]; diff --git a/src/Schema.php b/src/Schema.php index ca7a32c52..1215fa520 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -186,7 +186,7 @@ protected function findSchemaNames(): array */ protected function findTableComment(TableSchemaInterface $tableSchema): void { - $schemaName = $tableSchema->getSchemaName() + $schemaName = $tableSchema->getSchemaName() !== null ? "N'" . (string) $tableSchema->getSchemaName() . "'" : 'SCHEMA_NAME()'; $tableName = 'N' . (string) $this->db->getQuoter()->quoteValue($tableSchema->getName()); From 3a306cc2791c55e5f09cb1849b9fb697a4072ea1 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Mon, 29 Jan 2024 13:30:58 +0700 Subject: [PATCH 02/10] Fix psalm issues --- src/Dsn.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Dsn.php b/src/Dsn.php index b786aff14..99c456fd4 100644 --- a/src/Dsn.php +++ b/src/Dsn.php @@ -45,7 +45,8 @@ public function asString(): string $server = "Server=$this->host;"; } - if ($this->databaseName !== null) { + /** @psalm-suppress RiskyTruthyFalsyComparison */ + if (!empty($this->databaseName)) { $dsn = "$this->driver:" . $server . "Database=$this->databaseName"; } else { $dsn = "$this->driver:" . $server; From 87bac6a81d2238c06de051f993e33b51bc1a66c6 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Mon, 29 Jan 2024 13:32:35 +0700 Subject: [PATCH 03/10] Fix psalm issues --- src/Dsn.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Dsn.php b/src/Dsn.php index 99c456fd4..b7ee023cc 100644 --- a/src/Dsn.php +++ b/src/Dsn.php @@ -46,7 +46,7 @@ public function asString(): string } /** @psalm-suppress RiskyTruthyFalsyComparison */ - if (!empty($this->databaseName)) { + if ($this->databaseName !== null && $this->databaseName !== '') { $dsn = "$this->driver:" . $server . "Database=$this->databaseName"; } else { $dsn = "$this->driver:" . $server; From 6e0aeef50eacf045a6808213b06b681452bfd736 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Mon, 29 Jan 2024 13:33:58 +0700 Subject: [PATCH 04/10] Fix psalm issues --- src/Dsn.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Dsn.php b/src/Dsn.php index b7ee023cc..21907955a 100644 --- a/src/Dsn.php +++ b/src/Dsn.php @@ -45,7 +45,6 @@ public function asString(): string $server = "Server=$this->host;"; } - /** @psalm-suppress RiskyTruthyFalsyComparison */ if ($this->databaseName !== null && $this->databaseName !== '') { $dsn = "$this->driver:" . $server . "Database=$this->databaseName"; } else { From 58df64c26c1eb283535c8e20630641ae68c91673 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Mon, 29 Jan 2024 21:22:52 +0700 Subject: [PATCH 05/10] Revert some changes and add suppression of `RiskyTruthyFalsyComparison` to config --- psalm.xml | 3 +++ src/Command.php | 1 - src/DDLQueryBuilder.php | 16 ++++++++-------- src/DMLQueryBuilder.php | 1 - src/Dsn.php | 2 +- src/Schema.php | 2 +- 6 files changed, 13 insertions(+), 12 deletions(-) diff --git a/psalm.xml b/psalm.xml index 23bfcce17..5001d0a76 100644 --- a/psalm.xml +++ b/psalm.xml @@ -14,4 +14,7 @@ + + + diff --git a/src/Command.php b/src/Command.php index 5f8f71af4..c7c665b7b 100644 --- a/src/Command.php +++ b/src/Command.php @@ -14,7 +14,6 @@ final class Command extends AbstractPdoCommand { public function insertWithReturningPks(string $table, array $columns): bool|array { - /** @psalm-suppress RiskyTruthyFalsyComparison */ if (empty($this->db->getSchema()->getTableSchema($table)?->getPrimaryKey())) { if ($this->insert($table, $columns)->execute() === 0) { return false; diff --git a/src/DDLQueryBuilder.php b/src/DDLQueryBuilder.php index 6796a2c43..ad610ec0f 100644 --- a/src/DDLQueryBuilder.php +++ b/src/DDLQueryBuilder.php @@ -191,17 +191,17 @@ private function buildAddCommentSql(string $comment, string $table, string $colu throw new InvalidArgumentException("Table not found: $table"); } - $schemaName = $tableSchema->getSchemaName() !== null + $schemaName = $tableSchema->getSchemaName() ? "N'" . (string) $tableSchema->getSchemaName() . "'" : 'SCHEMA_NAME()'; $tableName = 'N' . (string) $this->quoter->quoteValue($tableSchema->getName()); - $columnName = $column !== null ? 'N' . (string) $this->quoter->quoteValue($column) : null; + $columnName = $column ? 'N' . (string) $this->quoter->quoteValue($column) : null; $comment = 'N' . (string) $this->quoter->quoteValue($comment); $functionParams = " @name = N'MS_description', @value = $comment, @level0type = N'SCHEMA', @level0name = $schemaName, @level1type = N'TABLE', @level1name = $tableName" - . ($column !== null ? ", @level2type = N'COLUMN', @level2name = $columnName" : '') . ';'; + . ($column ? ", @level2type = N'COLUMN', @level2name = $columnName" : '') . ';'; return " IF NOT EXISTS ( @@ -210,7 +210,7 @@ private function buildAddCommentSql(string $comment, string $table, string $colu N'MS_description', 'SCHEMA', $schemaName, 'TABLE', $tableName, - " . ($column !== null ? "'COLUMN', $columnName " : ' DEFAULT, DEFAULT ') . " + " . ($column ? "'COLUMN', $columnName " : ' DEFAULT, DEFAULT ') . " ) ) EXEC sys.sp_addextendedproperty $functionParams @@ -242,10 +242,10 @@ private function buildRemoveCommentSql(string $table, string $column = null): st throw new InvalidArgumentException("Table not found: $table"); } - $schemaName = $tableSchema->getSchemaName() !== null + $schemaName = $tableSchema->getSchemaName() ? "N'" . (string) $tableSchema->getSchemaName() . "'" : 'SCHEMA_NAME()'; $tableName = 'N' . (string) $this->quoter->quoteValue($tableSchema->getName()); - $columnName = $column !== null ? 'N' . (string) $this->quoter->quoteValue($column) : null; + $columnName = $column ? 'N' . (string) $this->quoter->quoteValue($column) : null; return " IF EXISTS ( @@ -254,14 +254,14 @@ private function buildRemoveCommentSql(string $table, string $column = null): st N'MS_description', 'SCHEMA', $schemaName, 'TABLE', $tableName, - " . ($column !== null ? "'COLUMN', $columnName " : ' DEFAULT, DEFAULT ') . " + " . ($column ? "'COLUMN', $columnName " : ' DEFAULT, DEFAULT ') . " ) ) EXEC sys.sp_dropextendedproperty @name = N'MS_description', @level0type = N'SCHEMA', @level0name = $schemaName, @level1type = N'TABLE', @level1name = $tableName" - . ($column !== null ? ", @level2type = N'COLUMN', @level2name = $columnName" : '') . ';'; + . ($column ? ", @level2type = N'COLUMN', @level2name = $columnName" : '') . ';'; } /** diff --git a/src/DMLQueryBuilder.php b/src/DMLQueryBuilder.php index d0d33393d..4d68e3d62 100644 --- a/src/DMLQueryBuilder.php +++ b/src/DMLQueryBuilder.php @@ -35,7 +35,6 @@ public function insertWithReturningPks(string $table, QueryInterface|array $colu $tableSchema = $this->schema->getTableSchema($table); $primaryKeys = $tableSchema?->getPrimaryKey(); - /** @psalm-suppress RiskyTruthyFalsyComparison */ if (empty($primaryKeys)) { return $this->insert($table, $columns, $params); } diff --git a/src/Dsn.php b/src/Dsn.php index 21907955a..24137bb01 100644 --- a/src/Dsn.php +++ b/src/Dsn.php @@ -45,7 +45,7 @@ public function asString(): string $server = "Server=$this->host;"; } - if ($this->databaseName !== null && $this->databaseName !== '') { + if (!empty($this->databaseName)) { $dsn = "$this->driver:" . $server . "Database=$this->databaseName"; } else { $dsn = "$this->driver:" . $server; diff --git a/src/Schema.php b/src/Schema.php index 1215fa520..ca7a32c52 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -186,7 +186,7 @@ protected function findSchemaNames(): array */ protected function findTableComment(TableSchemaInterface $tableSchema): void { - $schemaName = $tableSchema->getSchemaName() !== null + $schemaName = $tableSchema->getSchemaName() ? "N'" . (string) $tableSchema->getSchemaName() . "'" : 'SCHEMA_NAME()'; $tableName = 'N' . (string) $this->db->getQuoter()->quoteValue($tableSchema->getName()); From 2841c02b9a4bd47599c95cea393106a1653ddca3 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Tue, 30 Jan 2024 11:04:26 +0700 Subject: [PATCH 06/10] Run psalm on php 8.3 --- .github/workflows/static.yml | 4 +--- composer.json | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 0b1707659..92e84f9b8 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -36,9 +36,7 @@ jobs: - ubuntu-latest php: - - '8.0' - - '8.1' - - '8.2' + - '8.3' steps: - name: Checkout. diff --git a/composer.json b/composer.json index 29561e3e5..6e95e6921 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,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": "^5.20", "yiisoft/aliases": "^2.0", "yiisoft/cache-file": "^3.1", "yiisoft/var-dumper": "^1.5" From 5ef337487d42a3b796940c565e90f708ce745ecf Mon Sep 17 00:00:00 2001 From: Tigrov Date: Tue, 30 Jan 2024 15:52:28 +0700 Subject: [PATCH 07/10] Change to `"vimeo/psalm": "^4.30|^5.20",` --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6e95e6921..90826a545 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "rector/rector": "^0.19", "roave/infection-static-analysis-plugin": "^1.16", "spatie/phpunit-watcher": "^1.23", - "vimeo/psalm": "^5.20", + "vimeo/psalm": "^4.30|^5.20", "yiisoft/aliases": "^2.0", "yiisoft/cache-file": "^3.1", "yiisoft/var-dumper": "^1.5" From 4893366a86e2d311998f29c97f0a393d60b4875b Mon Sep 17 00:00:00 2001 From: Tigrov Date: Tue, 30 Jan 2024 17:19:53 +0700 Subject: [PATCH 08/10] Add line to CHANGELOG.md [skip ci] --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e15aba88..9ca3a0308 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Enh #286: Change property `Schema::$typeMap` to constant `Schema::TYPE_MAP` (@Tigrov) - Bug #287: Fix `DMLQueryBuilder::insertWithReturningPks()` and `Command::insertWithReturningPks()` methods (@Tigrov) +- Enh #292: Resolve psalm issues (@Tigrov) ## 1.1.0 November 12, 2023 From e7506ebffb22faf121574f74925cca2899a3e899 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 31 Jan 2024 10:39:36 +0700 Subject: [PATCH 09/10] Run psalm on PHP 8.0, 8.1, 8.2, 8.3 --- .github/workflows/static.yml | 11 ++++++++--- psalm4.xml | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 psalm4.xml diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 92e84f9b8..bba896424 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -36,6 +36,9 @@ jobs: - ubuntu-latest php: + - '8.0' + - '8.1' + - '8.2' - '8.3' steps: @@ -64,8 +67,10 @@ jobs: FULL_BRANCH_NAME: ${{ env.FULL_BRANCH_NAME }} WORK_PACKAGE_URL: ${{ env.WORK_PACKAGE_URL }} - - name: Install dependencies with composer. - 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/psalm4.xml b/psalm4.xml new file mode 100644 index 000000000..23bfcce17 --- /dev/null +++ b/psalm4.xml @@ -0,0 +1,17 @@ + + + + + + + + + From 82cbc83a9cf41629e1e29d115beb7ed89fa3b8eb Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Sat, 3 Feb 2024 10:16:48 +0700 Subject: [PATCH 10/10] Update CHANGELOG.md [skip ci] Co-authored-by: Sergei Predvoditelev --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ca3a0308..b2b24f215 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ - Enh #286: Change property `Schema::$typeMap` to constant `Schema::TYPE_MAP` (@Tigrov) - Bug #287: Fix `DMLQueryBuilder::insertWithReturningPks()` and `Command::insertWithReturningPks()` methods (@Tigrov) -- Enh #292: Resolve psalm issues (@Tigrov) +- Enh #292: Minor refactoring of `Command` and `Quoter` (@Tigrov) ## 1.1.0 November 12, 2023