From ba41957e5f51efb63c562e94b986cd8f0912d39b Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Sun, 6 Nov 2022 22:48:49 +0000 Subject: Redesign PrimaryController Signed-off-by: Kamil Tekiela --- .../Table/Structure/PrimaryController.php | 49 ++++++++-------------- phpstan-baseline.neon | 15 ------- psalm-baseline.xml | 14 +------ 3 files changed, 19 insertions(+), 59 deletions(-) diff --git a/libraries/classes/Controllers/Table/Structure/PrimaryController.php b/libraries/classes/Controllers/Table/Structure/PrimaryController.php index d218e1d94a..79777e0787 100644 --- a/libraries/classes/Controllers/Table/Structure/PrimaryController.php +++ b/libraries/classes/Controllers/Table/Structure/PrimaryController.php @@ -17,6 +17,7 @@ use PhpMyAdmin\Util; use function __; use function count; +use function is_array; final class PrimaryController extends AbstractController { @@ -43,26 +44,23 @@ final class PrimaryController extends AbstractController $GLOBALS['urlParams'] = $GLOBALS['urlParams'] ?? null; $GLOBALS['errorUrl'] = $GLOBALS['errorUrl'] ?? null; - $selected = $request->getParsedBodyParam('selected', []); - $selected_fld = $request->getParsedBodyParam('selected_fld', []); + /** @var string[]|null $selected */ + $selected = $request->getParsedBodyParam('selected_fld', $request->getParsedBodyParam('selected')); - if (empty($selected) && empty($selected_fld)) { + if (! is_array($selected) || $selected === []) { $this->response->setRequestStatus(false); $this->response->addJSON('message', __('No column selected.')); return; } - $primary = $this->getKeyForTablePrimary(); - if (empty($primary) && ! empty($selected_fld)) { - // no primary key, so we can safely create new - $mult_btn = __('Yes'); - $selected = $selected_fld; - } + $this->dbi->selectDb($GLOBALS['db']); + $hasPrimary = $this->hasPrimaryKey(); - $mult_btn = $_POST['mult_btn'] ?? $mult_btn ?? ''; + /** @var string|null $deletionConfirmed */ + $deletionConfirmed = $request->getParsedBodyParam('mult_btn', null); - if (! empty($selected_fld) && ! empty($primary)) { + if ($hasPrimary && $deletionConfirmed === null) { $this->checkParameters(['db', 'table']); $GLOBALS['urlParams'] = ['db' => $GLOBALS['db'], 'table' => $GLOBALS['table']]; @@ -74,15 +72,15 @@ final class PrimaryController extends AbstractController $this->render('table/structure/primary', [ 'db' => $GLOBALS['db'], 'table' => $GLOBALS['table'], - 'selected' => $selected_fld, + 'selected' => $selected, ]); return; } - if ($mult_btn === __('Yes')) { + if ($deletionConfirmed === __('Yes') || ! $hasPrimary) { $GLOBALS['sql_query'] = 'ALTER TABLE ' . Util::backquote($GLOBALS['table']); - if (! empty($primary)) { + if ($hasPrimary) { $GLOBALS['sql_query'] .= ' DROP PRIMARY KEY,'; } @@ -110,27 +108,16 @@ final class PrimaryController extends AbstractController ($this->structureController)($request); } - /** - * Gets table primary key - * - * @return string - */ - private function getKeyForTablePrimary() + private function hasPrimaryKey(): bool { - $this->dbi->selectDb($GLOBALS['db']); - $result = $this->dbi->query( - 'SHOW KEYS FROM ' . Util::backquote($GLOBALS['table']) . ';' - ); - $primary = ''; + $result = $this->dbi->query('SHOW KEYS FROM ' . Util::backquote($GLOBALS['table'])); + foreach ($result as $row) { - // Backups the list of primary keys - if ($row['Key_name'] !== 'PRIMARY') { - continue; + if ($row['Key_name'] === 'PRIMARY') { + return true; } - - $primary .= $row['Column_name'] . ', '; } - return $primary; + return false; } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 810b20150f..11394018d2 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1730,21 +1730,6 @@ parameters: count: 1 path: libraries/classes/Controllers/Table/Structure/PartitioningController.php - - - message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#" - count: 1 - path: libraries/classes/Controllers/Table/Structure/PrimaryController.php - - - - message: "#^Parameter \\#1 \\$identifier of static method PhpMyAdmin\\\\Util\\:\\:backquote\\(\\) expects string\\|Stringable\\|null, mixed given\\.$#" - count: 1 - path: libraries/classes/Controllers/Table/Structure/PrimaryController.php - - - - message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#" - count: 1 - path: libraries/classes/Controllers/Table/Structure/PrimaryController.php - - message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index d888ab2316..cf4e514f5a 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -4028,21 +4028,9 @@ - - $field - $selected - - + $GLOBALS['errorUrl'] - $field - $mult_btn - $selected - $selected - $selected_fld - - $row['Column_name'] - -- cgit v1.2.3