From 93b64a3722b061e8248e891413960f67839f1a92 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Tue, 13 Feb 2024 00:33:59 +0100 Subject: [PATCH 1/2] Refactor CentralColumns Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 122 ++------- psalm-baseline.xml | 88 ++---- .../Database/CentralColumnsController.php | 36 +-- .../CentralColumns/AddController.php | 3 +- .../Structure/CentralColumnsAddController.php | 8 +- src/Controllers/Table/StructureController.php | 5 +- src/Database/CentralColumns.php | 255 ++++++++---------- tests/classes/Database/CentralColumnsTest.php | 124 +++------ 8 files changed, 213 insertions(+), 428 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 8ee5779a8c..566d7731d9 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1415,11 +1415,6 @@ parameters: count: 1 path: src/Controllers/Database/CentralColumnsController.php - - - message: "#^Parameter \\#1 \\$fieldSelect of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:syncUniqueColumns\\(\\) expects array\\, array\\ given\\.$#" - count: 1 - path: src/Controllers/Database/CentralColumnsController.php - - message: "#^Parameter \\#1 \\$string of function parse_str expects string, mixed given\\.$#" count: 1 @@ -1435,6 +1430,11 @@ parameters: count: 2 path: src/Controllers/Database/CentralColumnsController.php + - + message: "#^Parameter \\#2 \\$fieldSelect of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:syncUniqueColumns\\(\\) expects array\\, array\\ given\\.$#" + count: 1 + path: src/Controllers/Database/CentralColumnsController.php + - message: "#^Parameter \\#2 \\$origColName of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:updateOneColumn\\(\\) expects string, mixed given\\.$#" count: 1 @@ -1466,13 +1466,13 @@ parameters: path: src/Controllers/Database/CentralColumnsController.php - - message: "#^Parameter \\#3 \\$table of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:syncUniqueColumns\\(\\) expects string\\|null, mixed given\\.$#" - count: 1 + message: "#^Parameter \\#4 \\$colType of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:updateOneColumn\\(\\) expects string, mixed given\\.$#" + count: 2 path: src/Controllers/Database/CentralColumnsController.php - - message: "#^Parameter \\#4 \\$colType of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:updateOneColumn\\(\\) expects string, mixed given\\.$#" - count: 2 + message: "#^Parameter \\#4 \\$containingTable of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:syncUniqueColumns\\(\\) expects string, mixed given\\.$#" + count: 1 path: src/Controllers/Database/CentralColumnsController.php - @@ -5055,6 +5055,11 @@ parameters: count: 1 path: src/Controllers/Table/Structure/CentralColumnsAddController.php + - + message: "#^Parameter \\#4 \\$containingTable of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:syncUniqueColumns\\(\\) expects string, mixed given\\.$#" + count: 1 + path: src/Controllers/Table/Structure/CentralColumnsAddController.php + - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" count: 1 @@ -6035,44 +6040,24 @@ parameters: count: 1 path: src/CreateAddField.php - - - message: "#^Call to function in_array\\(\\) requires parameter \\#3 to be set\\.$#" - count: 4 - path: src/Database/CentralColumns.php - - message: "#^Cannot access offset 'col_attribute' on mixed\\.$#" - count: 6 + count: 5 path: src/Database/CentralColumns.php - message: "#^Cannot access offset 'col_default' on mixed\\.$#" - count: 2 + count: 1 path: src/Database/CentralColumns.php - message: "#^Cannot access offset 'col_extra' on mixed\\.$#" - count: 3 - path: src/Database/CentralColumns.php - - - - message: "#^Cannot access offset 'col_isNull' on mixed\\.$#" - count: 1 - path: src/Database/CentralColumns.php - - - - message: "#^Cannot access offset 'col_length' on mixed\\.$#" - count: 1 - path: src/Database/CentralColumns.php - - - - message: "#^Cannot access offset 'col_name' on mixed\\.$#" count: 2 path: src/Database/CentralColumns.php - message: "#^Cannot access offset 'col_type' on mixed\\.$#" - count: 2 + count: 1 path: src/Database/CentralColumns.php - @@ -6087,16 +6072,16 @@ parameters: - message: "#^Cannot cast mixed to string\\.$#" - count: 4 - path: src/Database/CentralColumns.php - - - - message: "#^Foreach overwrites \\$table with its value variable\\.$#" count: 2 path: src/Database/CentralColumns.php - - message: "#^Method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:findExistingColNames\\(\\) should return array but returns mixed\\.$#" + message: "#^Method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:findExistingColNames\\(\\) should return array\\ but returns array\\.$#" + count: 1 + path: src/Database/CentralColumns.php + + - + message: "#^Method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:findExistingColumns\\(\\) should return array\\\\> but returns mixed\\.$#" count: 1 path: src/Database/CentralColumns.php @@ -6110,11 +6095,6 @@ parameters: count: 1 path: src/Database/CentralColumns.php - - - message: "#^Method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:getParams\\(\\) should return array\\\\|false but returns mixed\\.$#" - count: 1 - path: src/Database/CentralColumns.php - - message: "#^Only booleans are allowed in a negated boolean, PhpMyAdmin\\\\Dbal\\\\ResultInterface\\|false given\\.$#" count: 3 @@ -6126,55 +6106,25 @@ parameters: path: src/Database/CentralColumns.php - - message: "#^Only booleans are allowed in an if condition, mixed given\\.$#" + message: "#^Only booleans are allowed in an if condition, string\\|null given\\.$#" count: 3 path: src/Database/CentralColumns.php - message: "#^Parameter \\#1 \\$bitDefaultValue of static method PhpMyAdmin\\\\Util\\:\\:convertBitDefaultValue\\(\\) expects string\\|null, mixed given\\.$#" - count: 2 - path: src/Database/CentralColumns.php - - - - message: "#^Parameter \\#1 \\$database of method PhpMyAdmin\\\\DatabaseInterface\\:\\:getColumn\\(\\) expects string, mixed given\\.$#" count: 1 path: src/Database/CentralColumns.php - - - message: "#^Parameter \\#1 \\$database of method PhpMyAdmin\\\\DatabaseInterface\\:\\:getColumns\\(\\) expects string, mixed given\\.$#" - count: 1 - path: src/Database/CentralColumns.php - - - - message: "#^Parameter \\#1 \\$db of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:findExistingColNames\\(\\) expects string, mixed given\\.$#" - count: 2 - path: src/Database/CentralColumns.php - - message: "#^Parameter \\#1 \\$db of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:updateOneColumn\\(\\) expects string, mixed given\\.$#" count: 1 path: src/Database/CentralColumns.php - - - message: "#^Parameter \\#1 \\$dbname of method PhpMyAdmin\\\\DatabaseInterface\\:\\:selectDb\\(\\) expects PhpMyAdmin\\\\Identifiers\\\\DatabaseName\\|string, mixed given\\.$#" - count: 1 - path: src/Database/CentralColumns.php - - message: "#^Parameter \\#1 \\$def of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:getInsertQuery\\(\\) expects PhpMyAdmin\\\\ColumnFull, PhpMyAdmin\\\\ColumnFull\\|null given\\.$#" count: 1 path: src/Database/CentralColumns.php - - - message: "#^Parameter \\#1 \\$identifier of static method PhpMyAdmin\\\\Util\\:\\:backquote\\(\\) expects string\\|Stringable\\|null, mixed given\\.$#" - count: 1 - path: src/Database/CentralColumns.php - - - - message: "#^Parameter \\#1 \\$row of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:getHtmlForEditTableRow\\(\\) expects array, mixed given\\.$#" - count: 1 - path: src/Database/CentralColumns.php - - message: "#^Parameter \\#1 \\$str of method PhpMyAdmin\\\\DatabaseInterface\\:\\:quoteString\\(\\) expects string, mixed given\\.$#" count: 1 @@ -6182,7 +6132,7 @@ parameters: - message: "#^Parameter \\#1 \\$string of function bin2hex expects string, mixed given\\.$#" - count: 2 + count: 1 path: src/Database/CentralColumns.php - @@ -6200,16 +6150,6 @@ parameters: count: 1 path: src/Database/CentralColumns.php - - - message: "#^Parameter \\#2 \\$cols of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:findExistingColNames\\(\\) expects array\\, array\\, mixed\\> given\\.$#" - count: 1 - path: src/Database/CentralColumns.php - - - - message: "#^Parameter \\#2 \\$db of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:getInsertQuery\\(\\) expects string, mixed given\\.$#" - count: 2 - path: src/Database/CentralColumns.php - - message: "#^Parameter \\#2 \\$origColName of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:updateOneColumn\\(\\) expects string, mixed given\\.$#" count: 1 @@ -6220,18 +6160,13 @@ parameters: count: 1 path: src/Database/CentralColumns.php - - - message: "#^Parameter \\#2 \\$table of method PhpMyAdmin\\\\DatabaseInterface\\:\\:getColumn\\(\\) expects string, mixed given\\.$#" - count: 1 - path: src/Database/CentralColumns.php - - message: "#^Parameter \\#3 \\$colName of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:updateOneColumn\\(\\) expects string, mixed given\\.$#" count: 1 path: src/Database/CentralColumns.php - - message: "#^Parameter \\#3 \\$column of method PhpMyAdmin\\\\ConfigStorage\\\\Relation\\:\\:checkChildForeignReferences\\(\\) expects string, mixed given\\.$#" + message: "#^Parameter \\#3 \\$column of method PhpMyAdmin\\\\ConfigStorage\\\\Relation\\:\\:checkChildForeignReferences\\(\\) expects string, string\\|null given\\.$#" count: 1 path: src/Database/CentralColumns.php @@ -15915,11 +15850,6 @@ parameters: count: 1 path: tests/classes/Database/CentralColumnsTest.php - - - message: "#^Cannot access offset 0 on mixed\\.$#" - count: 1 - path: tests/classes/Database/CentralColumnsTest.php - - message: "#^Parameter \\#1 \\$needle of static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertStringContainsString\\(\\) expects string, mixed given\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index e09f2a896f..44375b0391 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -914,14 +914,14 @@ - getParsedBodyParam('cur_table', '')]]> getParsedBodyParam('pos', '')]]> + getParsedBodyParam('table-select')]]> getParsedBodyParam('total_rows', '')]]> $variables - + getParsedBodyParam('column-select')]]]> @@ -3709,6 +3709,9 @@ + + getParsedBodyParam('table')]]> + __construct @@ -4397,11 +4400,7 @@ $columnDefault[$i] $columnExtra[$i] - - - - @@ -4410,30 +4409,15 @@ - $row $type - - $cols - $columnDefault[$i] $columnDefault[$i] $columnDefault[$i] - - - - - - - - - - - @@ -4455,59 +4439,39 @@ - $cols[] - $column $columnDefault $columnDefault[$i] $columnExtra[$i] - $defaultValue $defaultValues[$rowNum] - $field $length - $row $row - $row - $type - - string[]|false - - - - - - - - - - $cfgCentralColumns - + + $hasList + dbi->fetchResult($query, null, null, ConnectionType::ControlUser)]]> + (string|null)[][] + string[] + - $db - $db - $db - $db - $db - $db - $db - $table $tnPageNow - - $db - $db - $db - $db - $db - $db - $table - + $field + + + + + + + + + + @@ -13423,15 +13387,9 @@ $this->centralColumns, CentralColumns::class, 'getHtmlForEditTableRow', - [$listDetailCols[0], 0], + [$this->modifiedColumnData[0], 0], )]]> - - $listDetailCols[0] - - - $listDetailCols - assertIsArray diff --git a/src/Controllers/Database/CentralColumnsController.php b/src/Controllers/Database/CentralColumnsController.php index 29d26aa3af..c88b24516e 100644 --- a/src/Controllers/Database/CentralColumnsController.php +++ b/src/Controllers/Database/CentralColumnsController.php @@ -80,10 +80,12 @@ class CentralColumnsController extends AbstractController } if ($request->hasBodyParam('add_column')) { - $tmpMsg = $this->addColumn([ - 'table-select' => $request->getParsedBodyParam('table-select'), - 'column-select' => $request->getParsedBodyParam('column-select'), - ]); + $tmpMsg = $this->centralColumns->syncUniqueColumns( + DatabaseName::from($request->getParsedBodyParam('db')), + [$request->getParsedBodyParam('column-select')], + false, + $request->getParsedBodyParam('table-select'), + ); } $this->addScriptFiles([ @@ -102,7 +104,7 @@ class CentralColumnsController extends AbstractController } if ($request->hasBodyParam('multi_edit_central_column_save')) { - $GLOBALS['message'] = $this->updateMultipleColumn([ + $GLOBALS['message'] = $this->centralColumns->updateMultipleColumn([ 'db' => $request->getParsedBodyParam('db'), 'orig_col_name' => $request->getParsedBodyParam('orig_col_name'), 'field_name' => $request->getParsedBodyParam('field_name'), @@ -229,20 +231,6 @@ class CentralColumnsController extends AbstractController ); } - /** - * @param mixed[] $params Request parameters - * - * @return true|Message - */ - public function addColumn(array $params): bool|Message - { - return $this->centralColumns->syncUniqueColumns( - [$params['column-select']], - false, - $params['table-select'], - ); - } - /** @param mixed[] $params Request parameters */ public function editPage(array $params): void { @@ -253,16 +241,6 @@ class CentralColumnsController extends AbstractController $this->render('database/central_columns/edit', ['rows' => $rows]); } - /** - * @param mixed[] $params Request parameters - * - * @return true|Message - */ - public function updateMultipleColumn(array $params): bool|Message - { - return $this->centralColumns->updateMultipleColumn($params); - } - /** * @param mixed[] $params Request parameters * diff --git a/src/Controllers/Database/Structure/CentralColumns/AddController.php b/src/Controllers/Database/Structure/CentralColumns/AddController.php index 347d298fbe..56199bc554 100644 --- a/src/Controllers/Database/Structure/CentralColumns/AddController.php +++ b/src/Controllers/Database/Structure/CentralColumns/AddController.php @@ -9,6 +9,7 @@ use PhpMyAdmin\Controllers\Database\StructureController; use PhpMyAdmin\Database\CentralColumns; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Http\ServerRequest; +use PhpMyAdmin\Identifiers\DatabaseName; use PhpMyAdmin\Message; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Template; @@ -44,7 +45,7 @@ final class AddController extends AbstractController Assert::allString($selected); $centralColumns = new CentralColumns($this->dbi); - $error = $centralColumns->syncUniqueColumns($selected); + $error = $centralColumns->syncUniqueColumns(DatabaseName::from($request->getParsedBodyParam('db')), $selected); $GLOBALS['message'] = $error instanceof Message ? $error : Message::success(__('Success!')); diff --git a/src/Controllers/Table/Structure/CentralColumnsAddController.php b/src/Controllers/Table/Structure/CentralColumnsAddController.php index ac606d5331..7555150ec3 100644 --- a/src/Controllers/Table/Structure/CentralColumnsAddController.php +++ b/src/Controllers/Table/Structure/CentralColumnsAddController.php @@ -8,6 +8,7 @@ use PhpMyAdmin\Controllers\AbstractController; use PhpMyAdmin\Controllers\Table\StructureController; use PhpMyAdmin\Database\CentralColumns; use PhpMyAdmin\Http\ServerRequest; +use PhpMyAdmin\Identifiers\DatabaseName; use PhpMyAdmin\Message; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Template; @@ -42,7 +43,12 @@ final class CentralColumnsAddController extends AbstractController Assert::allString($selected); - $centralColsError = $this->centralColumns->syncUniqueColumns($selected, false); + $centralColsError = $this->centralColumns->syncUniqueColumns( + DatabaseName::from($request->getParsedBodyParam('db')), + $selected, + false, + $request->getParsedBodyParam('table'), + ); if ($centralColsError instanceof Message) { $GLOBALS['message'] = $centralColsError; diff --git a/src/Controllers/Table/StructureController.php b/src/Controllers/Table/StructureController.php index c34a711487..e6b4ba600b 100644 --- a/src/Controllers/Table/StructureController.php +++ b/src/Controllers/Table/StructureController.php @@ -176,7 +176,10 @@ class StructureController extends AbstractController } $centralColumns = new CentralColumns($this->dbi); - $centralList = $centralColumns->getFromTable(Current::$database, Current::$table); + $centralList = $centralColumns->findExistingColNames( + Current::$database, + $this->dbi->getColumnNames(Current::$database, Current::$table), + ); /** * Displays Space usage and row statistics diff --git a/src/Database/CentralColumns.php b/src/Database/CentralColumns.php index 25ede8a718..0d30880405 100644 --- a/src/Database/CentralColumns.php +++ b/src/Database/CentralColumns.php @@ -10,6 +10,7 @@ use PhpMyAdmin\Config; use PhpMyAdmin\ConfigStorage\Relation; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Dbal\ConnectionType; +use PhpMyAdmin\Identifiers\DatabaseName; use PhpMyAdmin\Message; use PhpMyAdmin\Template; use PhpMyAdmin\Util; @@ -18,6 +19,7 @@ use function __; use function array_column; use function array_diff; use function array_map; +use function array_merge; use function array_unique; use function array_values; use function bin2hex; @@ -72,32 +74,22 @@ class CentralColumns } /** - * Defines the central_columns parameters for the current user + * Returns the configuration storage settings for central columns or false if no storage is available * - * @return string[]|false the central_columns parameters for the current user + * @return array{user:string, db: string, table:string}|false */ - public function getParams(): array|bool + public function getParams(): array|false { - static $cfgCentralColumns = null; - - if ($cfgCentralColumns !== null) { - return $cfgCentralColumns; - } - $centralColumnsFeature = $this->relation->getRelationParameters()->centralColumnsFeature; if ($centralColumnsFeature === null) { - $cfgCentralColumns = false; - return false; } - $cfgCentralColumns = [ + return [ 'user' => $this->user, 'db' => $centralColumnsFeature->database->getName(), 'table' => $centralColumnsFeature->centralColumns->getName(), ]; - - return $cfgCentralColumns; } /** @@ -119,14 +111,13 @@ class CentralColumns } $pmadb = $cfgCentralColumns['db']; - $this->dbi->selectDb($pmadb, ConnectionType::ControlUser); $centralListTable = $cfgCentralColumns['table']; //get current values of $db from central column list - if ($num == 0) { - $query = 'SELECT * FROM ' . Util::backquote($centralListTable) . ' ' + if ($num === 0) { + $query = 'SELECT * FROM ' . Util::backquote($pmadb) . '.' . Util::backquote($centralListTable) . ' ' . 'WHERE db_name = ' . $this->dbi->quoteString($db, ConnectionType::ControlUser) . ';'; } else { - $query = 'SELECT * FROM ' . Util::backquote($centralListTable) . ' ' + $query = 'SELECT * FROM ' . Util::backquote($pmadb) . '.' . Util::backquote($centralListTable) . ' ' . 'WHERE db_name = ' . $this->dbi->quoteString($db, ConnectionType::ControlUser) . ' ' . 'LIMIT ' . $from . ', ' . $num . ';'; } @@ -152,10 +143,9 @@ class CentralColumns } $pmadb = $cfgCentralColumns['db']; - $this->dbi->selectDb($pmadb, ConnectionType::ControlUser); $centralListTable = $cfgCentralColumns['table']; $query = 'SELECT count(db_name) FROM ' - . Util::backquote($centralListTable) . ' ' + . Util::backquote($pmadb) . '.' . Util::backquote($centralListTable) . ' ' . 'WHERE db_name = ' . $this->dbi->quoteString($db, ConnectionType::ControlUser) . ';'; $res = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser); if (isset($res[0])) { @@ -168,17 +158,14 @@ class CentralColumns /** * return the existing columns in central list among the given list of columns * - * @param string $db the selected database - * @param string[] $cols list of given columns - * @param bool $allFields set if need all the fields of existing columns, - * otherwise only column_name is returned + * @param string $db the selected database + * @param string[] $cols list of given columns * - * @return mixed[] list of columns in central columns among given set of columns + * @return string[] list of columns in central columns among given set of columns */ - private function findExistingColNames( + public function findExistingColNames( string $db, array $cols, - bool $allFields = false, ): array { $cfgCentralColumns = $this->getParams(); if (! is_array($cfgCentralColumns)) { @@ -188,20 +175,41 @@ class CentralColumns $cols = $this->getWhereInColumns($cols); $pmadb = $cfgCentralColumns['db']; - $this->dbi->selectDb($pmadb, ConnectionType::ControlUser); $centralListTable = $cfgCentralColumns['table']; - if ($allFields) { - $query = 'SELECT * FROM ' . Util::backquote($centralListTable) . ' WHERE db_name = ' - . $this->dbi->quoteString($db, ConnectionType::ControlUser) . ' AND col_name IN (' . $cols . ');'; - $hasList = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser); - $this->handleColumnExtra($hasList); - } else { - $query = 'SELECT col_name FROM ' - . Util::backquote($centralListTable) . ' WHERE db_name = ' - . $this->dbi->quoteString($db, ConnectionType::ControlUser) . ' AND col_name IN (' . $cols . ');'; - $hasList = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser); + $query = 'SELECT col_name FROM ' + . Util::backquote($pmadb) . '.' . Util::backquote($centralListTable) . ' WHERE db_name = ' + . $this->dbi->quoteString($db, ConnectionType::ControlUser) . ' AND col_name IN (' . $cols . ');'; + + return $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser); + } + + /** + * return the existing columns in central list among the given list of columns + * + * @param string $db the selected database + * @param string[] $cols list of given columns + * + * @return (string|null)[][] list of columns in central columns among given set of columns + */ + private function findExistingColumns( + string $db, + array $cols, + ): array { + $cfgCentralColumns = $this->getParams(); + if (! is_array($cfgCentralColumns)) { + return []; } + $cols = $this->getWhereInColumns($cols); + + $pmadb = $cfgCentralColumns['db']; + $centralListTable = $cfgCentralColumns['table']; + $query = 'SELECT * FROM ' + . Util::backquote($pmadb) . '.' . Util::backquote($centralListTable) . ' WHERE db_name = ' + . $this->dbi->quoteString($db, ConnectionType::ControlUser) . ' AND col_name IN (' . $cols . ');'; + $hasList = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser); + $this->handleColumnExtra($hasList); + return $hasList; } @@ -219,6 +227,7 @@ class CentralColumns private function getInsertQuery( ColumnFull $def, string $db, + string $centralListDb, string $centralListTable, ): string { $extractedColumnSpec = Util::extractColumnSpec($def->type); @@ -232,7 +241,7 @@ class CentralColumns $default = $def->default ?? ''; return 'INSERT INTO ' - . Util::backquote($centralListTable) . ' ' + . Util::backquote($centralListDb) . '.' . Util::backquote($centralListTable) . ' ' . 'VALUES ( ' . $this->dbi->quoteString($db, ConnectionType::ControlUser) . ' ,' . $this->dbi->quoteString($def->field, ConnectionType::ControlUser) . ',' . $this->dbi->quoteString($type, ConnectionType::ControlUser) . ',' @@ -248,70 +257,69 @@ class CentralColumns * are added to central list otherwise the $field_select is considered as * list of columns and these columns are added to central list if not already added * - * @param string[] $fieldSelect if $isTable is true selected tables list otherwise selected columns list - * @param bool $isTable if passed array is of tables or columns - * @param string|null $table if $isTable is false, then table name to which columns belong + * @param string[] $fieldSelect if $isTable is true selected tables list otherwise selected columns list + * @param bool $isTable if passed array is of tables or columns + * @param string $containingTable if $isTable is false, then table name to which columns belong * * @return true|Message */ public function syncUniqueColumns( + DatabaseName $databaseName, array $fieldSelect, bool $isTable = true, - string|null $table = null, + string $containingTable = '', ): bool|Message { $cfgCentralColumns = $this->getParams(); if (! is_array($cfgCentralColumns)) { - return Message::error( - __('The configuration storage is not ready for the central list of columns feature.'), - ); + return $this->getStorageNotReadyMessage(); } - $db = $_POST['db']; - $pmadb = $cfgCentralColumns['db']; - $centralListTable = $cfgCentralColumns['table']; - $this->dbi->selectDb($db); $existingCols = []; - $cols = []; $insQuery = []; - $fields = []; - $message = true; if ($isTable) { + $cols = []; + $fields = []; foreach ($fieldSelect as $table) { - $fields[$table] = $this->dbi->getColumns($db, $table, true); - foreach (array_column($fields[$table], 'Field') as $field) { - $cols[] = $field; - } + $fields[$table] = $this->dbi->getColumns($databaseName->getName(), $table, true); + $cols = array_merge($cols, array_column($fields[$table], 'field')); } - $hasList = $this->findExistingColNames($db, $cols); + $hasList = $this->findExistingColNames($databaseName->getName(), $cols); foreach ($fieldSelect as $table) { foreach ($fields[$table] as $def) { $field = $def->field; - if (! in_array($field, $hasList)) { + if (! in_array($field, $hasList, true)) { $hasList[] = $field; - $insQuery[] = $this->getInsertQuery($def, $db, $centralListTable); + $insQuery[] = $this->getInsertQuery( + $def, + $databaseName->getName(), + $cfgCentralColumns['db'], + $cfgCentralColumns['table'], + ); } else { $existingCols[] = "'" . $field . "'"; } } } } else { - if ($table === null) { - $table = $_POST['table']; - } - - $hasList = $this->findExistingColNames($db, $fieldSelect); + $hasList = $this->findExistingColNames($databaseName->getName(), $fieldSelect); foreach ($fieldSelect as $column) { - if (! in_array($column, $hasList)) { + if (! in_array($column, $hasList, true)) { $hasList[] = $column; - $field = $this->dbi->getColumn($db, $table, $column, true); - $insQuery[] = $this->getInsertQuery($field, $db, $centralListTable); + $field = $this->dbi->getColumn($databaseName->getName(), $containingTable, $column, true); + $insQuery[] = $this->getInsertQuery( + $field, + $databaseName->getName(), + $cfgCentralColumns['db'], + $cfgCentralColumns['table'], + ); } else { $existingCols[] = "'" . $column . "'"; } } } + $message = true; if ($existingCols !== []) { $existingCols = implode(',', array_unique($existingCols)); $message = Message::notice( @@ -329,14 +337,14 @@ class CentralColumns ); } - $this->dbi->selectDb($pmadb, ConnectionType::ControlUser); foreach ($insQuery as $query) { if (! $this->dbi->tryQuery($query, ConnectionType::ControlUser)) { $message = Message::error(__('Could not add columns!')); $message->addMessage( Message::rawError($this->dbi->getError(ConnectionType::ControlUser)), ); - break; + + return $message; } } @@ -362,30 +370,24 @@ class CentralColumns ): bool|Message { $cfgCentralColumns = $this->getParams(); if (! is_array($cfgCentralColumns)) { - return Message::error( - __('The configuration storage is not ready for the central list of columns feature.'), - ); + return $this->getStorageNotReadyMessage(); } $pmadb = $cfgCentralColumns['db']; $centralListTable = $cfgCentralColumns['table']; - $this->dbi->selectDb($database); - $message = true; $colNotExist = []; - $fields = []; if ($isTable) { + $fields = []; $cols = []; foreach ($fieldSelect as $table) { $fields[$table] = $this->dbi->getColumnNames($database, $table); - foreach ($fields[$table] as $colSelect) { - $cols[] = $colSelect; - } + $cols = array_merge($cols, $fields[$table]); } $hasList = $this->findExistingColNames($database, $cols); foreach ($fieldSelect as $table) { foreach ($fields[$table] as $column) { - if (in_array($column, $hasList)) { + if (in_array($column, $hasList, true)) { continue; } @@ -396,7 +398,7 @@ class CentralColumns $cols = $fieldSelect; $hasList = $this->findExistingColNames($database, $cols); foreach ($fieldSelect as $column) { - if (in_array($column, $hasList)) { + if (in_array($column, $hasList, true)) { continue; } @@ -404,6 +406,7 @@ class CentralColumns } } + $message = true; if ($colNotExist !== []) { $colNotExist = implode(',', array_unique($colNotExist)); $message = Message::notice( @@ -416,10 +419,9 @@ class CentralColumns ); } - $this->dbi->selectDb($pmadb, ConnectionType::ControlUser); - $cols = $this->getWhereInColumns($cols); - $query = 'DELETE FROM ' . Util::backquote($centralListTable) . ' WHERE db_name = ' + $query = 'DELETE FROM ' + . Util::backquote($pmadb) . '.' . Util::backquote($centralListTable) . ' WHERE db_name = ' . $this->dbi->quoteString($database, ConnectionType::ControlUser) . ' AND col_name IN (' . $cols . ');'; if (! $this->dbi->tryQuery($query, ConnectionType::ControlUser)) { @@ -447,10 +449,10 @@ class CentralColumns array $selectedTables, ): bool|Message { $message = true; + $this->dbi->selectDb($db); foreach ($selectedTables as $table) { $query = 'ALTER TABLE ' . Util::backquote($table); - $hasList = $this->getFromTable($db, $table, true); - $this->dbi->selectDb($db); + $hasList = $this->findExistingColumns($db, $this->dbi->getColumnNames($db, $table)); foreach ($hasList as $column) { $columnStatus = $this->relation->checkChildForeignReferences($db, $table, $column['col_name']); //column definition can only be changed if @@ -459,8 +461,7 @@ class CentralColumns continue; } - $query .= ' MODIFY ' . Util::backquote($column['col_name']) . ' ' - . $column['col_type']; + $query .= ' MODIFY ' . Util::backquote($column['col_name']) . ' ' . $column['col_type']; if ($column['col_length']) { $query .= '(' . $column['col_length'] . ')'; } @@ -479,7 +480,7 @@ class CentralColumns && $column['col_default'] !== 'current_timestamp()' ) { $query .= ' DEFAULT ' . $this->dbi->quoteString( - (string) $column['col_default'], + $column['col_default'], ConnectionType::ControlUser, ); } else { @@ -505,33 +506,6 @@ class CentralColumns return $message; } - /** - * return the columns present in central list of columns for a given - * table of a given database - * - * @param string $db given database - * @param string $table given table - * @param bool $allFields set if need all the fields of existing columns, - * otherwise only column_name is returned - * - * @return mixed[] columns present in central list from given table of given db. - */ - public function getFromTable( - string $db, - string $table, - bool $allFields = false, - ): array { - $cfgCentralColumns = $this->getParams(); - if ($cfgCentralColumns === false || $cfgCentralColumns === []) { - return []; - } - - $this->dbi->selectDb($db); - $fields = $this->dbi->getColumnNames($db, $table); - - return $this->findExistingColNames($db, $fields, $allFields); - } - /** * update a column in central columns list if a edit is requested * @@ -562,14 +536,12 @@ class CentralColumns ): bool|Message { $cfgCentralColumns = $this->getParams(); if (! is_array($cfgCentralColumns)) { - return Message::error( - __('The configuration storage is not ready for the central list of columns feature.'), - ); + return $this->getStorageNotReadyMessage(); } + $centralListDb = $cfgCentralColumns['db']; $centralTable = $cfgCentralColumns['table']; - $this->dbi->selectDb($cfgCentralColumns['db'], ConnectionType::ControlUser); - if ($origColName == '') { + if ($origColName === '') { $def = new ColumnFull( $colName, $colType . ($colLength !== '' ? '(' . $colLength . ')' : ''), @@ -581,9 +553,9 @@ class CentralColumns '', '', ); - $query = $this->getInsertQuery($def, $db, $centralTable); + $query = $this->getInsertQuery($def, $db, $centralListDb, $centralTable); } else { - $query = 'UPDATE ' . Util::backquote($centralTable) + $query = 'UPDATE ' . Util::backquote($centralListDb) . '.' . Util::backquote($centralTable) . ' SET col_type = ' . $this->dbi->quoteString($colType, ConnectionType::ControlUser) . ', col_name = ' . $this->dbi->quoteString($colName, ConnectionType::ControlUser) . ', col_length = ' . $this->dbi->quoteString($colLength, ConnectionType::ControlUser) @@ -649,9 +621,8 @@ class CentralColumns /** * build html for editing a row in central columns table * - * @param mixed[] $row array contains complete information of a - * particular row of central list table - * @param int $rowNum position the row in the table + * @param (string|null)[] $row array contains complete information of a particular row of central list table + * @param int $rowNum position the row in the table * * @return string html of a particular row in the central columns table. */ @@ -678,7 +649,7 @@ class CentralColumns if ($typeUpper === 'BIT') { $defaultValue = Util::convertBitDefaultValue($meta['DefaultValue']); - } elseif ($typeUpper == 'BINARY' || $typeUpper == 'VARBINARY') { + } elseif ($typeUpper === 'BINARY' || $typeUpper === 'VARBINARY') { $defaultValue = bin2hex($meta['DefaultValue']); } } @@ -706,8 +677,7 @@ class CentralColumns * @param string $db selected database * @param string $table current table name * - * @return mixed[] encoded list of columns present in central list for the given - * database + * @return mixed[] encoded list of columns present in central list for the given database */ public function getListRaw(string $db, string $table): array { @@ -716,14 +686,14 @@ class CentralColumns return []; } + $pmadb = $cfgCentralColumns['db']; $centralTable = $cfgCentralColumns['table']; if ($table === '') { - $query = 'SELECT * FROM ' . Util::backquote($centralTable) . ' ' + $query = 'SELECT * FROM ' . Util::backquote($pmadb) . '.' . Util::backquote($centralTable) . ' ' . 'WHERE db_name = ' . $this->dbi->quoteString($db, ConnectionType::ControlUser) . ';'; } else { - $this->dbi->selectDb($db); $columns = $this->dbi->getColumnNames($db, $table); - $query = 'SELECT * FROM ' . Util::backquote($centralTable) . ' ' + $query = 'SELECT * FROM ' . Util::backquote($pmadb) . '.' . Util::backquote($centralTable) . ' ' . 'WHERE db_name = ' . $this->dbi->quoteString($db, ConnectionType::ControlUser); if ($columns !== []) { $query .= ' AND col_name NOT IN (' . $this->getWhereInColumns($columns) . ')'; @@ -732,7 +702,6 @@ class CentralColumns $query .= ';'; } - $this->dbi->selectDb($cfgCentralColumns['db'], ConnectionType::ControlUser); $columnsList = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser); $this->handleColumnExtra($columnsList); @@ -777,7 +746,7 @@ class CentralColumns public function getHtmlForEditingPage(array $selectedFld, string $selectedDb): string { $html = ''; - $listDetailCols = $this->findExistingColNames($selectedDb, $selectedFld, true); + $listDetailCols = $this->findExistingColumns($selectedDb, $selectedFld); $rowNum = 0; foreach ($listDetailCols as $row) { $tableHtmlRow = $this->getHtmlForEditTableRow($row, $rowNum); @@ -807,11 +776,10 @@ class CentralColumns } $pmadb = $cfgCentralColumns['db']; - $this->dbi->selectDb($pmadb, ConnectionType::ControlUser); $centralListTable = $cfgCentralColumns['table']; //get current values of $db from central column list - $query = 'SELECT COUNT(db_name) FROM ' . Util::backquote($centralListTable) . ' ' - . 'WHERE db_name = ' . $this->dbi->quoteString($db, ConnectionType::ControlUser) + $query = 'SELECT COUNT(db_name) FROM ' . Util::backquote($pmadb) . '.' . Util::backquote($centralListTable) + . ' WHERE db_name = ' . $this->dbi->quoteString($db, ConnectionType::ControlUser) . ($num === 0 ? '' : 'LIMIT ' . $from . ', ' . $num) . ';'; $result = $this->dbi->fetchResult($query, null, null, ConnectionType::ControlUser); @@ -825,8 +793,7 @@ class CentralColumns /** @return string[] */ public function getColumnsNotInCentralList(string $db, string $table): array { - $existingColumns = $this->getFromTable($db, $table); - $this->dbi->selectDb($db); + $existingColumns = $this->findExistingColNames($db, $this->dbi->getColumnNames($db, $table)); $columnNames = $this->dbi->getColumnNames($db, $table); // returns a list of column names less the ones from $existingColumns @@ -860,7 +827,6 @@ class CentralColumns $tnPageNow, $tnNbTotalPage, ) : ''; - $this->dbi->selectDb($db); $tables = $this->dbi->getTables($db); $rowsList = $this->getColumnsList($db, $pos, $maxRows); @@ -874,7 +840,7 @@ class CentralColumns $rowsMeta[$rowNum]['DefaultType'] = 'NONE'; } elseif ($row['col_default'] === 'CURRENT_TIMESTAMP' || $row['col_default'] === 'current_timestamp()') { $rowsMeta[$rowNum]['DefaultType'] = 'CURRENT_TIMESTAMP'; - } elseif ($row['col_default'] == 'NULL') { + } elseif ($row['col_default'] === 'NULL') { $rowsMeta[$rowNum]['DefaultType'] = $row['col_default']; } else { $rowsMeta[$rowNum]['DefaultType'] = 'USER_DEFINED'; @@ -941,4 +907,11 @@ class CentralColumns $columns, )); } + + private function getStorageNotReadyMessage(): Message + { + return Message::error( + __('The configuration storage is not ready for the central list of columns feature.'), + ); + } } diff --git a/tests/classes/Database/CentralColumnsTest.php b/tests/classes/Database/CentralColumnsTest.php index 8965b07a60..2003971881 100644 --- a/tests/classes/Database/CentralColumnsTest.php +++ b/tests/classes/Database/CentralColumnsTest.php @@ -12,6 +12,7 @@ use PhpMyAdmin\Current; use PhpMyAdmin\Database\CentralColumns; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Dbal\ConnectionType; +use PhpMyAdmin\Identifiers\DatabaseName; use PhpMyAdmin\Tests\AbstractTestCase; use PhpMyAdmin\Tests\Stubs\DummyResult; use PhpMyAdmin\Types; @@ -37,26 +38,26 @@ class CentralColumnsTest extends AbstractTestCase [ 'col_name' => 'id', 'col_type' => 'integer', - 'col_length' => 0, - 'col_isNull' => 0, + 'col_length' => '0', + 'col_isNull' => '0', 'col_extra' => 'UNSIGNED,auto_increment', - 'col_default' => 1, + 'col_default' => '1', 'col_collation' => '', ], [ 'col_name' => 'col1', 'col_type' => 'varchar', - 'col_length' => 100, - 'col_isNull' => 1, + 'col_length' => '100', + 'col_isNull' => '1', 'col_extra' => 'BINARY', - 'col_default' => 1, + 'col_default' => '1', 'col_collation' => '', ], [ 'col_name' => 'col2', 'col_type' => 'DATETIME', - 'col_length' => 0, - 'col_isNull' => 1, + 'col_length' => '0', + 'col_isNull' => '1', 'col_extra' => 'on update CURRENT_TIMESTAMP', 'col_default' => 'CURRENT_TIMESTAMP', 'col_collation' => '', @@ -68,28 +69,28 @@ class CentralColumnsTest extends AbstractTestCase [ 'col_name' => 'id', 'col_type' => 'integer', - 'col_length' => 0, - 'col_isNull' => 0, + 'col_length' => '0', + 'col_isNull' => '0', 'col_extra' => 'auto_increment', - 'col_default' => 1, + 'col_default' => '1', 'col_collation' => '', 'col_attribute' => 'UNSIGNED', ], [ 'col_name' => 'col1', 'col_type' => 'varchar', - 'col_length' => 100, - 'col_isNull' => 1, + 'col_length' => '100', + 'col_isNull' => '1', 'col_extra' => '', - 'col_default' => 1, + 'col_default' => '1', 'col_collation' => '', 'col_attribute' => 'BINARY', ], [ 'col_name' => 'col2', 'col_type' => 'DATETIME', - 'col_length' => 0, - 'col_isNull' => 1, + 'col_length' => '0', + 'col_isNull' => '1', 'col_extra' => '', 'col_default' => 'CURRENT_TIMESTAMP', 'col_collation' => '', @@ -200,7 +201,7 @@ class CentralColumnsTest extends AbstractTestCase $this->dbi->expects(self::once()) ->method('fetchResult') ->with( - 'SELECT count(db_name) FROM `pma_central_columns` WHERE db_name = \'phpmyadmin\';', + 'SELECT count(db_name) FROM `phpmyadmin`.`pma_central_columns` WHERE db_name = \'phpmyadmin\';', null, null, ConnectionType::ControlUser, @@ -218,11 +219,9 @@ class CentralColumnsTest extends AbstractTestCase */ public function testSyncUniqueColumns(): void { - $_POST['db'] = 'PMA_db'; - $_POST['table'] = 'PMA_table'; - self::assertTrue( $this->centralColumns->syncUniqueColumns( + DatabaseName::from('PMA_db'), ['PMA_table'], ), ); @@ -247,61 +246,6 @@ class CentralColumnsTest extends AbstractTestCase ); } - /** - * Test for getFromTable - */ - public function testGetFromTable(): void - { - $db = 'PMA_db'; - $table = 'PMA_table'; - - $this->dbi->expects(self::once()) - ->method('fetchResult') - ->with( - 'SELECT col_name FROM `pma_central_columns` ' - . "WHERE db_name = 'PMA_db' AND col_name IN ('id','col1','col2');", - null, - null, - ConnectionType::ControlUser, - ) - ->willReturn(['id', 'col1']); - self::assertSame( - ['id', 'col1'], - $this->centralColumns->getFromTable( - $db, - $table, - ), - ); - } - - /** - * Test for getFromTable with $allFields = true - */ - public function testGetFromTableWithAllFields(): void - { - $db = 'PMA_db'; - $table = 'PMA_table'; - - $this->dbi->expects(self::once()) - ->method('fetchResult') - ->with( - 'SELECT * FROM `pma_central_columns` ' - . "WHERE db_name = 'PMA_db' AND col_name IN ('id','col1','col2');", - null, - null, - ConnectionType::ControlUser, - ) - ->willReturn(array_slice($this->columnData, 0, 2)); - self::assertSame( - array_slice($this->modifiedColumnData, 0, 2), - $this->centralColumns->getFromTable( - $db, - $table, - true, - ), - ); - } - /** * Test for updateOneColumn */ @@ -365,7 +309,7 @@ class CentralColumnsTest extends AbstractTestCase $this->dbi->expects(self::any()) ->method('fetchResult') ->with( - 'SELECT * FROM `pma_central_columns` ' + 'SELECT * FROM `phpmyadmin`.`pma_central_columns` ' . "WHERE db_name = 'phpmyadmin' AND col_name IN ('col1','col2');", null, null, @@ -376,18 +320,13 @@ class CentralColumnsTest extends AbstractTestCase ['col1', 'col2'], 'phpmyadmin', ); - $listDetailCols = $this->callFunction( - $this->centralColumns, - CentralColumns::class, - 'findExistingColNames', - ['phpmyadmin', ['col1', 'col2'], true], - ); + self::assertStringContainsString( $this->callFunction( $this->centralColumns, CentralColumns::class, 'getHtmlForEditTableRow', - [$listDetailCols[0], 0], + [$this->modifiedColumnData[0], 0], ), $result, ); @@ -401,7 +340,7 @@ class CentralColumnsTest extends AbstractTestCase $this->dbi->expects(self::once()) ->method('fetchResult') ->with( - 'SELECT * FROM `pma_central_columns` WHERE db_name = \'phpmyadmin\';', + 'SELECT * FROM `phpmyadmin`.`pma_central_columns` WHERE db_name = \'phpmyadmin\';', null, null, ConnectionType::ControlUser, @@ -424,7 +363,7 @@ class CentralColumnsTest extends AbstractTestCase $this->dbi->expects(self::once()) ->method('fetchResult') ->with( - 'SELECT * FROM `pma_central_columns` ' + 'SELECT * FROM `phpmyadmin`.`pma_central_columns` ' . "WHERE db_name = 'phpmyadmin' AND col_name " . "NOT IN ('id','col1','col2');", null, @@ -442,26 +381,23 @@ class CentralColumnsTest extends AbstractTestCase } /** - * Test for findExistingColNames + * Test for findExistingColumns */ public function testFindExistingColNames(): void { + $expectedQuery = 'SELECT * FROM `phpmyadmin`.`pma_central_columns`' + . ' WHERE db_name = \'phpmyadmin\' AND col_name IN (\'col1\');'; $this->dbi->expects(self::once()) ->method('fetchResult') - ->with( - 'SELECT * FROM `pma_central_columns` WHERE db_name = \'phpmyadmin\' AND col_name IN (\'col1\');', - null, - null, - ConnectionType::ControlUser, - ) + ->with($expectedQuery, null, null, ConnectionType::ControlUser) ->willReturn(array_slice($this->columnData, 1, 1)); self::assertSame( array_slice($this->modifiedColumnData, 1, 1), $this->callFunction( $this->centralColumns, CentralColumns::class, - 'findExistingColNames', - ['phpmyadmin', ['col1'], true], + 'findExistingColumns', + ['phpmyadmin', ['col1']], ), ); } From b44e8d3d97f5c16106f5b098da24267b4ef76df1 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Tue, 13 Feb 2024 15:34:38 +0100 Subject: [PATCH 2/2] Add strict type for params in controller Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 85 +++++++++--- psalm-baseline.xml | 36 ++--- .../Database/CentralColumnsController.php | 127 ++++++++++-------- 3 files changed, 160 insertions(+), 88 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 566d7731d9..295692385b 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1410,6 +1410,16 @@ parameters: count: 1 path: src/Controllers/Database/CentralColumns/PopulateColumnsController.php + - + message: "#^Parameter \\#1 \\$colName of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:addNewColumn\\(\\) expects string, mixed given\\.$#" + count: 1 + path: src/Controllers/Database/CentralColumnsController.php + + - + message: "#^Parameter \\#1 \\$colName of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:editSave\\(\\) expects string, mixed given\\.$#" + count: 1 + path: src/Controllers/Database/CentralColumnsController.php + - message: "#^Parameter \\#1 \\$database of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:deleteColumnsFromList\\(\\) expects string, mixed given\\.$#" count: 1 @@ -1426,8 +1436,13 @@ parameters: path: src/Controllers/Database/CentralColumnsController.php - - message: "#^Parameter \\#10 \\$colDefault of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:updateOneColumn\\(\\) expects string, mixed given\\.$#" - count: 2 + message: "#^Parameter \\#10 \\$collation of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:editSave\\(\\) expects string, mixed given\\.$#" + count: 1 + path: src/Controllers/Database/CentralColumnsController.php + + - + message: "#^Parameter \\#2 \\$colDefault of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:addNewColumn\\(\\) expects string, mixed given\\.$#" + count: 1 path: src/Controllers/Database/CentralColumnsController.php - @@ -1436,7 +1451,7 @@ parameters: path: src/Controllers/Database/CentralColumnsController.php - - message: "#^Parameter \\#2 \\$origColName of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:updateOneColumn\\(\\) expects string, mixed given\\.$#" + message: "#^Parameter \\#2 \\$origColName of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:editSave\\(\\) expects string, mixed given\\.$#" count: 1 path: src/Controllers/Database/CentralColumnsController.php @@ -1461,13 +1476,23 @@ parameters: path: src/Controllers/Database/CentralColumnsController.php - - message: "#^Parameter \\#3 \\$colName of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:updateOneColumn\\(\\) expects string, mixed given\\.$#" - count: 2 + message: "#^Parameter \\#3 \\$colDefault of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:editSave\\(\\) expects string, mixed given\\.$#" + count: 1 path: src/Controllers/Database/CentralColumnsController.php - - message: "#^Parameter \\#4 \\$colType of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:updateOneColumn\\(\\) expects string, mixed given\\.$#" - count: 2 + message: "#^Parameter \\#3 \\$colDefaultSel of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:addNewColumn\\(\\) expects string, mixed given\\.$#" + count: 1 + path: src/Controllers/Database/CentralColumnsController.php + + - + message: "#^Parameter \\#4 \\$colDefaultSel of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:editSave\\(\\) expects string, mixed given\\.$#" + count: 1 + path: src/Controllers/Database/CentralColumnsController.php + + - + message: "#^Parameter \\#4 \\$colExtra of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:addNewColumn\\(\\) expects string\\|null, mixed given\\.$#" + count: 1 path: src/Controllers/Database/CentralColumnsController.php - @@ -1476,23 +1501,53 @@ parameters: path: src/Controllers/Database/CentralColumnsController.php - - message: "#^Parameter \\#5 \\$colAttribute of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:updateOneColumn\\(\\) expects string, mixed given\\.$#" - count: 2 + message: "#^Parameter \\#5 \\$colExtra of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:editSave\\(\\) expects string\\|null, mixed given\\.$#" + count: 1 path: src/Controllers/Database/CentralColumnsController.php - - message: "#^Parameter \\#6 \\$colLength of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:updateOneColumn\\(\\) expects string, mixed given\\.$#" - count: 2 + message: "#^Parameter \\#5 \\$colIsNull of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:addNewColumn\\(\\) expects string\\|null, mixed given\\.$#" + count: 1 path: src/Controllers/Database/CentralColumnsController.php - - message: "#^Parameter \\#8 \\$collation of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:updateOneColumn\\(\\) expects string, mixed given\\.$#" - count: 2 + message: "#^Parameter \\#6 \\$colIsNull of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:editSave\\(\\) expects string\\|null, mixed given\\.$#" + count: 1 path: src/Controllers/Database/CentralColumnsController.php - - message: "#^Parameter \\#9 \\$colExtra of method PhpMyAdmin\\\\Database\\\\CentralColumns\\:\\:updateOneColumn\\(\\) expects string, mixed given\\.$#" - count: 2 + message: "#^Parameter \\#6 \\$colLength of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:addNewColumn\\(\\) expects string, mixed given\\.$#" + count: 1 + path: src/Controllers/Database/CentralColumnsController.php + + - + message: "#^Parameter \\#7 \\$colAttribute of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:addNewColumn\\(\\) expects string, mixed given\\.$#" + count: 1 + path: src/Controllers/Database/CentralColumnsController.php + + - + message: "#^Parameter \\#7 \\$colLength of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:editSave\\(\\) expects string, mixed given\\.$#" + count: 1 + path: src/Controllers/Database/CentralColumnsController.php + + - + message: "#^Parameter \\#8 \\$colAttribute of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:editSave\\(\\) expects string, mixed given\\.$#" + count: 1 + path: src/Controllers/Database/CentralColumnsController.php + + - + message: "#^Parameter \\#8 \\$colType of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:addNewColumn\\(\\) expects string, mixed given\\.$#" + count: 1 + path: src/Controllers/Database/CentralColumnsController.php + + - + message: "#^Parameter \\#9 \\$colType of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:editSave\\(\\) expects string, mixed given\\.$#" + count: 1 + path: src/Controllers/Database/CentralColumnsController.php + + - + message: "#^Parameter \\#9 \\$collation of method PhpMyAdmin\\\\Controllers\\\\Database\\\\CentralColumnsController\\:\\:addNewColumn\\(\\) expects string, mixed given\\.$#" + count: 1 path: src/Controllers/Database/CentralColumnsController.php - diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 44375b0391..2bcc751f23 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -896,25 +896,29 @@ Config::getInstance() - $columnDefault - $columnDefault - - - - - - - - - - - - - + getParsedBodyParam('col_attribute')]]> + getParsedBodyParam('col_attribute')]]> + getParsedBodyParam('col_default')]]> + getParsedBodyParam('col_default')]]> + getParsedBodyParam('col_default_sel')]]> + getParsedBodyParam('col_default_sel')]]> + getParsedBodyParam('col_extra')]]> + getParsedBodyParam('col_extra')]]> + getParsedBodyParam('col_isNull')]]> + getParsedBodyParam('col_isNull')]]> + getParsedBodyParam('col_length')]]> + getParsedBodyParam('col_length')]]> + getParsedBodyParam('col_name')]]> + getParsedBodyParam('col_name')]]> + getParsedBodyParam('col_type')]]> + getParsedBodyParam('col_type')]]> + getParsedBodyParam('collation')]]> + getParsedBodyParam('collation')]]> getParsedBodyParam('cur_table', '')]]> + getParsedBodyParam('orig_col_name')]]> getParsedBodyParam('pos', '')]]> getParsedBodyParam('table-select')]]> getParsedBodyParam('total_rows', '')]]> @@ -925,8 +929,6 @@ - $columnDefault - $columnDefault __construct diff --git a/src/Controllers/Database/CentralColumnsController.php b/src/Controllers/Database/CentralColumnsController.php index c88b24516e..2610059a76 100644 --- a/src/Controllers/Database/CentralColumnsController.php +++ b/src/Controllers/Database/CentralColumnsController.php @@ -40,34 +40,36 @@ class CentralColumnsController extends AbstractController $db = DatabaseName::from($request->getParam('db')); if ($request->hasBodyParam('edit_save')) { - $this->response->addHTML((string) $this->editSave([ - 'col_name' => $request->getParsedBodyParam('col_name'), - 'orig_col_name' => $request->getParsedBodyParam('orig_col_name'), - 'col_default' => $request->getParsedBodyParam('col_default'), - 'col_default_sel' => $request->getParsedBodyParam('col_default_sel'), - 'col_extra' => $request->getParsedBodyParam('col_extra'), - 'col_isNull' => $request->getParsedBodyParam('col_isNull'), - 'col_length' => $request->getParsedBodyParam('col_length'), - 'col_attribute' => $request->getParsedBodyParam('col_attribute'), - 'col_type' => $request->getParsedBodyParam('col_type'), - 'collation' => $request->getParsedBodyParam('collation'), - ], $db)); + $this->response->addHTML((string) $this->editSave( + $request->getParsedBodyParam('col_name'), + $request->getParsedBodyParam('orig_col_name'), + $request->getParsedBodyParam('col_default'), + $request->getParsedBodyParam('col_default_sel'), + $request->getParsedBodyParam('col_extra'), + $request->getParsedBodyParam('col_isNull'), + $request->getParsedBodyParam('col_length'), + $request->getParsedBodyParam('col_attribute'), + $request->getParsedBodyParam('col_type'), + $request->getParsedBodyParam('collation'), + $db, + )); return; } if ($request->hasBodyParam('add_new_column')) { - $tmpMsg = $this->addNewColumn([ - 'col_name' => $request->getParsedBodyParam('col_name'), - 'col_default' => $request->getParsedBodyParam('col_default'), - 'col_default_sel' => $request->getParsedBodyParam('col_default_sel'), - 'col_extra' => $request->getParsedBodyParam('col_extra'), - 'col_isNull' => $request->getParsedBodyParam('col_isNull'), - 'col_length' => $request->getParsedBodyParam('col_length'), - 'col_attribute' => $request->getParsedBodyParam('col_attribute'), - 'col_type' => $request->getParsedBodyParam('col_type'), - 'collation' => $request->getParsedBodyParam('collation'), - ], $db); + $tmpMsg = $this->addNewColumn( + $request->getParsedBodyParam('col_name'), + $request->getParsedBodyParam('col_default'), + $request->getParsedBodyParam('col_default_sel'), + $request->getParsedBodyParam('col_extra'), + $request->getParsedBodyParam('col_isNull'), + $request->getParsedBodyParam('col_length'), + $request->getParsedBodyParam('col_attribute'), + $request->getParsedBodyParam('col_type'), + $request->getParsedBodyParam('collation'), + $db, + ); } if ($request->hasBodyParam('getColumnList')) { @@ -179,54 +181,67 @@ class CentralColumnsController extends AbstractController $this->render('database/central_columns/main', $variables); } - /** - * @param mixed[] $params Request parameters - * - * @return true|Message - */ - public function editSave(array $params, DatabaseName $db): bool|Message - { - $columnDefault = $params['col_default']; - if ($columnDefault === 'NONE' && $params['col_default_sel'] !== 'USER_DEFINED') { + /** @return true|Message */ + public function editSave( + string $colName, + string $origColName, + string $colDefault, + string $colDefaultSel, + string|null $colExtra, + string|null $colIsNull, + string $colLength, + string $colAttribute, + string $colType, + string $collation, + DatabaseName $db, + ): bool|Message { + $columnDefault = $colDefault; + if ($columnDefault === 'NONE' && $colDefaultSel !== 'USER_DEFINED') { $columnDefault = ''; } return $this->centralColumns->updateOneColumn( $db->getName(), - $params['orig_col_name'], - $params['col_name'], - $params['col_type'], - $params['col_attribute'], - $params['col_length'], - isset($params['col_isNull']), - $params['collation'], - $params['col_extra'] ?? '', + $origColName, + $colName, + $colType, + $colAttribute, + $colLength, + $colIsNull !== null, + $collation, + $colExtra ?? '', $columnDefault, ); } - /** - * @param mixed[] $params Request parameters - * - * @return true|Message - */ - public function addNewColumn(array $params, DatabaseName $db): bool|Message - { - $columnDefault = $params['col_default']; - if ($columnDefault === 'NONE' && $params['col_default_sel'] !== 'USER_DEFINED') { + /** @return true|Message */ + public function addNewColumn( + string $colName, + string $colDefault, + string $colDefaultSel, + string|null $colExtra, + string|null $colIsNull, + string $colLength, + string $colAttribute, + string $colType, + string $collation, + DatabaseName $db, + ): bool|Message { + $columnDefault = $colDefault; + if ($columnDefault === 'NONE' && $colDefaultSel !== 'USER_DEFINED') { $columnDefault = ''; } return $this->centralColumns->updateOneColumn( $db->getName(), '', - $params['col_name'], - $params['col_type'], - $params['col_attribute'], - $params['col_length'], - isset($params['col_isNull']), - $params['collation'], - $params['col_extra'] ?? '', + $colName, + $colType, + $colAttribute, + $colLength, + $colIsNull !== null, + $collation, + $colExtra ?? '', $columnDefault, ); }