diff options
author | MaurĂcio Meneghini Fauth <mauricio@fauth.dev> | 2022-11-09 04:26:31 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-09 04:26:31 +0300 |
commit | 2cc3bf4778b2e6d6200cccaf89b852839cf9004f (patch) | |
tree | 1d8302d162b8001c138585f225b51ca2c2eced9c | |
parent | 1c49ed0d18f82a1ff18731d5c3035d3462343981 (diff) | |
parent | ba41957e5f51efb63c562e94b986cd8f0912d39b (diff) |
Merge pull request #17881 from kamil-tekiela/Refactor-PrimaryController.php
Redesign PrimaryController
-rw-r--r-- | libraries/classes/Controllers/Table/Structure/PrimaryController.php | 49 | ||||
-rw-r--r-- | phpstan-baseline.neon | 15 | ||||
-rw-r--r-- | 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 e93e3935d7..b99d24aa1e 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1748,21 +1748,6 @@ parameters: - 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 path: libraries/classes/Controllers/Table/Structure/ReservedWordCheckController.php - diff --git a/psalm-baseline.xml b/psalm-baseline.xml index e95e287fca..26b70f9a7a 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -3998,21 +3998,9 @@ </RedundantConditionGivenDocblockType> </file> <file src="libraries/classes/Controllers/Table/Structure/PrimaryController.php"> - <MixedArgument occurrences="2"> - <code>$field</code> - <code>$selected</code> - </MixedArgument> - <MixedAssignment occurrences="6"> + <MixedAssignment occurrences="1"> <code>$GLOBALS['errorUrl']</code> - <code>$field</code> - <code>$mult_btn</code> - <code>$selected</code> - <code>$selected</code> - <code>$selected_fld</code> </MixedAssignment> - <PossiblyNullOperand occurrences="1"> - <code>$row['Column_name']</code> - </PossiblyNullOperand> </file> <file src="libraries/classes/Controllers/Table/Structure/ReservedWordCheckController.php"> <MixedArgument occurrences="1"> |