diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index e5b2227864..2e3ece07e6 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -4955,6 +4955,11 @@ parameters: count: 1 path: src/Controllers/Table/IndexesController.php + - + message: "#^Parameter \\#1 \\$oldIndexName of method PhpMyAdmin\\\\Table\\\\Indexes\\:\\:getSqlQueryForIndexCreateOrEdit\\(\\) expects string\\|null, mixed given\\.$#" + count: 1 + path: src/Controllers/Table/IndexesController.php + - message: "#^Parameter \\#1 \\$params of static method PhpMyAdmin\\\\Url\\:\\:getCommon\\(\\) expects array\\, mixed given\\.$#" count: 1 @@ -15170,11 +15175,6 @@ parameters: count: 1 path: src/Table/Indexes.php - - - message: "#^Parameter \\#1 \\$identifier of static method PhpMyAdmin\\\\Util\\:\\:backquote\\(\\) expects string\\|Stringable\\|null, mixed given\\.$#" - count: 1 - path: src/Table/Indexes.php - - message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#" count: 2 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index e5a783b6a4..b852af6f2a 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -3596,6 +3596,7 @@ + $oldIndex @@ -11462,9 +11463,6 @@ - - $oldIndex - getSubPart()]]> diff --git a/src/Controllers/Table/IndexesController.php b/src/Controllers/Table/IndexesController.php index 784487827b..368d4fd098 100644 --- a/src/Controllers/Table/IndexesController.php +++ b/src/Controllers/Table/IndexesController.php @@ -103,8 +103,18 @@ class IndexesController extends AbstractController if (isset($_POST['do_save_data'])) { $previewSql = $request->hasBodyParam('preview_sql'); + if (isset($_POST['old_index'])) { + $oldIndex = is_array($_POST['old_index']) ? $_POST['old_index']['Key_name'] : $_POST['old_index']; + } else { + $oldIndex = null; + } - $sqlQuery = $this->indexes->getSqlQueryForIndexCreateOrEdit(Current::$database, Current::$table, $index); + $sqlQuery = $this->indexes->getSqlQueryForIndexCreateOrEdit( + $oldIndex, + $index, + Current::$database, + Current::$table, + ); // If there is a request for SQL previewing. if ($previewSql) { diff --git a/src/Table/Indexes.php b/src/Table/Indexes.php index 6887cfe6be..cd22adf816 100644 --- a/src/Table/Indexes.php +++ b/src/Table/Indexes.php @@ -15,7 +15,6 @@ use PhpMyAdmin\Util; use function __; use function implode; use function in_array; -use function is_array; use function sprintf; final class Indexes @@ -36,8 +35,12 @@ final class Indexes * * @param Index $index current index */ - public function getSqlQueryForIndexCreateOrEdit(string $dbName, string $tableName, Index $index): string - { + public function getSqlQueryForIndexCreateOrEdit( + string|null $oldIndexName, + Index $index, + string $dbName, + string $tableName, + ): string { // $sql_query is the one displayed in the query box $sqlQuery = sprintf( 'ALTER TABLE %s.%s', @@ -46,14 +49,13 @@ final class Indexes ); // Drops the old index - if (isset($_POST['old_index'])) { - $oldIndex = is_array($_POST['old_index']) ? $_POST['old_index']['Key_name'] : $_POST['old_index']; - if ($oldIndex === 'PRIMARY') { + if ($oldIndexName !== null) { + if ($oldIndexName === 'PRIMARY') { $sqlQuery .= ' DROP PRIMARY KEY,'; } else { $sqlQuery .= sprintf( ' DROP INDEX %s,', - Util::backquote($oldIndex), + Util::backquote($oldIndexName), ); } } @@ -147,7 +149,7 @@ final class Indexes public function getSqlQueryForRename(string $oldIndexName, Index $index, string $db, string $table): string { if (! Compatibility::isCompatibleRenameIndex($this->dbi->getVersion())) { - return $this->getSqlQueryForIndexCreateOrEdit($db, $table, $index); + return $this->getSqlQueryForIndexCreateOrEdit($oldIndexName, $index, $db, $table); } if ($oldIndexName === 'PRIMARY') { diff --git a/tests/classes/Table/IndexesTest.php b/tests/classes/Table/IndexesTest.php index 0f146b041c..cada27e4ba 100644 --- a/tests/classes/Table/IndexesTest.php +++ b/tests/classes/Table/IndexesTest.php @@ -70,15 +70,14 @@ class IndexesTest extends AbstractTestCase $sqlQueryExpected = 'ALTER TABLE `pma_db`.`pma_table` DROP PRIMARY KEY, ADD PRIMARY KEY (`id`);'; $_POST['old_index'] = 'PRIMARY'; - $this->assertEquals($sqlQueryExpected, $indexes->getSqlQueryForIndexCreateOrEdit($db, $table, $index)); - - $_POST['old_index'] = []; - $_POST['old_index']['Key_name'] = 'PRIMARY'; - $this->assertEquals($sqlQueryExpected, $indexes->getSqlQueryForIndexCreateOrEdit($db, $table, $index)); + $this->assertEquals( + $sqlQueryExpected, + $indexes->getSqlQueryForIndexCreateOrEdit('PRIMARY', $index, $db, $table), + ); // Error message $index->setName('NOT PRIMARY'); // Cannot rename primary so the operation should fail - $indexes->getSqlQueryForIndexCreateOrEdit($db, $table, $index); + $indexes->getSqlQueryForIndexCreateOrEdit('PRIMARY', $index, $db, $table); $this->assertInstanceOf(Message::class, $indexes->getError()); } }