From b1a82bf08b597a60789c93f44aa414916cbf7d4f Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Mon, 6 Dec 2021 14:45:00 +0000 Subject: [PATCH 01/18] Refactor Sql.php Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 44 +++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index 552d987778..38e8446643 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -39,7 +39,6 @@ use function session_write_close; use function sprintf; use function str_contains; use function str_replace; -use function strlen; use function ucwords; /** @@ -645,7 +644,7 @@ class Sql private function getNumberOfRowsAffectedOrChanged($isAffected, $result) { if ($isAffected) { - return @$this->dbi->affectedRows(); + return $this->dbi->affectedRows(); } if ($result) { @@ -663,16 +662,16 @@ class Sql * * @return bool whether to reload the navigation(1) or not(0) */ - private function hasCurrentDbChanged($db): bool + private function hasCurrentDbChanged(string $db): bool { - if (strlen($db) > 0) { - $currentDb = $this->dbi->fetchValue('SELECT DATABASE()'); - - // $current_db is false, except when a USE statement was sent - return ($currentDb != false) && ($db !== $currentDb); + if ($db === '') { + return false; } - return false; + $currentDb = $this->dbi->fetchValue('SELECT DATABASE()'); + + // $current_db is false, except when a USE statement was sent + return ($currentDb != false) && ($db !== $currentDb); } /** @@ -683,14 +682,14 @@ class Sql * @param string|null $column current column * @param bool $purge whether purge set or not */ - private function cleanupRelations($db, $table, ?string $column, $purge): void + private function cleanupRelations(string $db, string $table, ?string $column, $purge): void { - if (empty($purge) || strlen($db) <= 0) { + if (empty($purge) || $db === '') { return; } - if (strlen($table) > 0) { - if (isset($column) && strlen($column) > 0) { + if ($table !== '') { + if ($column !== null && $column !== '') { $this->relationCleanup->column($db, $table, $column); } else { $this->relationCleanup->table($db, $table); @@ -802,7 +801,7 @@ class Sql * @param array $analyzedSqlResults analyzed sql results * @param string $fullSqlQuery full sql query * @param bool $isGotoFile whether to go to a file - * @param string|null $db current database + * @param string $db current database * @param string|null $table current table * @param bool|null $findRealEnd whether to find the real end * @param string $sqlQueryForBookmark sql query to be stored as bookmark @@ -814,8 +813,8 @@ class Sql array $analyzedSqlResults, $fullSqlQuery, $isGotoFile, - $db, - $table, + string $db, + ?string $table, ?bool $findRealEnd, $sqlQueryForBookmark, $extraData @@ -869,12 +868,11 @@ class Sql $unlimNumRows = $this->countQueryResults($numRows, $justBrowsing, $db, $table, $analyzedSqlResults); - $this->cleanupRelations($db ?? '', $table ?? '', $_POST['dropped_column'] ?? null, $_POST['purge'] ?? null); + $this->cleanupRelations($db, $table ?? '', $_POST['dropped_column'] ?? null, $_POST['purge'] ?? null); if ( isset($_POST['dropped_column']) - && isset($db) && strlen($db) > 0 - && isset($table) && strlen($table) > 0 + && $db !== '' && $table !== null && $table !== '' ) { // to refresh the list of indexes (Ajax mode) @@ -1417,7 +1415,7 @@ class Sql // Gets the list of fields properties $fieldsMeta = []; - if (isset($result) && ! is_bool($result)) { + if ($result !== null && ! is_bool($result)) { $fieldsMeta = $this->dbi->getFieldsMeta($result); } @@ -1680,7 +1678,7 @@ class Sql // Handling is also not required if we came from the "Sort by key" // drop-down. if ( - ! empty($analyzedSqlResults) + $analyzedSqlResults !== [] && $this->isRememberSortingOrder($analyzedSqlResults) && empty($analyzedSqlResults['union']) && ! isset($_POST['sort_by_key']) @@ -1711,7 +1709,7 @@ class Sql } $GLOBALS['reload'] = $this->hasCurrentDbChanged($db); - $this->dbi->selectDb($db ?? ''); + $this->dbi->selectDb($db); [ $result, @@ -1756,7 +1754,7 @@ class Sql $htmlOutput = $this->getQueryResponseForResultsReturned( $result, $analyzedSqlResults, - $db ?? '', + $db, $table, $sqlData ?? null, $displayResultsObject, From 34b0a92a8799734c69fbd292eafbdfb3cf77c917 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 5 Jan 2022 01:27:23 +0000 Subject: [PATCH 02/18] Merge if statements into one Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index 38e8446643..b2c1f145cf 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -912,10 +912,12 @@ class Sql $statement = $analyzedSqlResults['statement']; if ($statement instanceof AlterStatement) { - if (! empty($statement->altered[0]) && $statement->altered[0]->options->has('DROP')) { - if (! empty($statement->altered[0]->field->column)) { - $this->transformations->clear($db, $table, $statement->altered[0]->field->column); - } + if ( + ! empty($statement->altered[0]) + && $statement->altered[0]->options->has('DROP') + && ! empty($statement->altered[0]->field->column) + ) { + $this->transformations->clear($db, $table, $statement->altered[0]->field->column); } } elseif ($statement instanceof DropStatement) { $this->transformations->clear($db, $table); @@ -1440,11 +1442,9 @@ class Sql $statement = $analyzedSqlResults['statement'] ?? null; if ($statement instanceof SelectStatement) { - if (! empty($statement->expr)) { - if ($statement->expr[0]->expr === '*' && ! empty($table)) { - $_table = new Table($table, $db); - $updatableView = $_table->isUpdatableView(); - } + if (! empty($statement->expr) && $statement->expr[0]->expr === '*' && ! empty($table)) { + $_table = new Table($table, $db); + $updatableView = $_table->isUpdatableView(); } if ( From 1e2772dbff6dcc5e5e2882001e454bc9347c4b0c Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 5 Jan 2022 01:32:46 +0000 Subject: [PATCH 03/18] Convert ternary into true boolean expression Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index b2c1f145cf..6ea4073271 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -1456,7 +1456,7 @@ class Sql } } - $hasUnique = empty($table) ? false : $this->resultSetContainsUniqueKey($db, $table, $fieldsMeta); + $hasUnique = !empty($table) && $this->resultSetContainsUniqueKey($db, $table, $fieldsMeta); $editable = ($hasUnique || $GLOBALS['cfg']['RowActionLinksWithoutUnique'] From c9a532dcafd8a048ec6263f0f8253e190dd4204c Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 5 Jan 2022 01:39:04 +0000 Subject: [PATCH 04/18] Remove useless ! empty Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index 6ea4073271..188b39c560 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -492,7 +492,7 @@ class Sql { $bookmark = Bookmark::get($this->dbi, $GLOBALS['cfg']['Server']['user'], $db, $table, 'label', false, true); - if (! empty($bookmark) && ! empty($bookmark->getQuery())) { + if ($bookmark !== null && $bookmark->getQuery() !== '') { $GLOBALS['using_bookmark_message'] = Message::notice( __('Using bookmark "%s" as default browse query.') ); @@ -845,7 +845,7 @@ class Sql // If there are no errors and bookmarklabel was given, // store the query as a bookmark - if (! empty($_POST['bkm_label']) && ! empty($sqlQueryForBookmark)) { + if (! empty($_POST['bkm_label']) && $sqlQueryForBookmark) { $bookmarkFeature = $this->relation->getRelationParameters()->bookmarkFeature; $this->storeTheQueryAsBookmark( $bookmarkFeature, @@ -971,7 +971,7 @@ class Sql // fact that $message_to_show is sent for every case. // The $message_to_show containing a success message and sent with // the form should not have priority over errors - } elseif (! empty($messageToShow) && $analyzedSqlResults['querytype'] !== 'SELECT') { + } elseif ($messageToShow && $analyzedSqlResults['querytype'] !== 'SELECT') { $message = Message::rawSuccess(htmlspecialchars($messageToShow)); } elseif (! empty($GLOBALS['show_as_php'])) { $message = Message::success(__('Showing as PHP code')); @@ -1113,7 +1113,7 @@ class Sql $bookmarkFeature !== null && $displayParts['bkm_form'] == '1' && empty($_GET['id_bookmark']) - && ! empty($sqlQuery) + && $sqlQuery ) { $bookmark = $this->template->render('sql/bookmark', [ 'db' => $db, @@ -1339,7 +1339,7 @@ class Sql private function getMessageIfMissingColumnIndex(?string $table, string $database, $editable, $hasUniqueKey): string { $output = ''; - if (! empty($table) && (Utilities::isSystemSchema($database) || ! $editable)) { + if ($table && (Utilities::isSystemSchema($database) || ! $editable)) { $output = Message::notice( sprintf( __( @@ -1353,7 +1353,7 @@ class Sql ) ) )->getDisplay(); - } elseif (! empty($table) && ! $hasUniqueKey) { + } elseif ($table && ! $hasUniqueKey) { $output = Message::notice( sprintf( __( @@ -1442,7 +1442,7 @@ class Sql $statement = $analyzedSqlResults['statement'] ?? null; if ($statement instanceof SelectStatement) { - if (! empty($statement->expr) && $statement->expr[0]->expr === '*' && ! empty($table)) { + if ($statement->expr && $statement->expr[0]->expr === '*' && $table) { $_table = new Table($table, $db); $updatableView = $_table->isUpdatableView(); } @@ -1456,7 +1456,7 @@ class Sql } } - $hasUnique = !empty($table) && $this->resultSetContainsUniqueKey($db, $table, $fieldsMeta); + $hasUnique = $table && $this->resultSetContainsUniqueKey($db, $table, $fieldsMeta); $editable = ($hasUnique || $GLOBALS['cfg']['RowActionLinksWithoutUnique'] @@ -1517,7 +1517,7 @@ class Sql ); $profilingChartHtml = ''; - if (! empty($profilingResults)) { + if ($profilingResults) { $profiling = $this->getDetailedProfilingStats($profilingResults); $profilingChartHtml = $this->template->render('sql/profiling_chart', ['profiling' => $profiling]); } @@ -1543,7 +1543,7 @@ class Sql $bookmarkFeature !== null && $displayParts['bkm_form'] == '1' && empty($_GET['id_bookmark']) - && ! empty($sqlQuery) + && $sqlQuery ) { $bookmarkSupportHtml = $this->template->render('sql/bookmark', [ 'db' => $db, @@ -1610,7 +1610,7 @@ class Sql $tableFromSql, ] = ParseAnalyze::sqlQuery($sqlQuery, $db); - if ($table != $tableFromSql && ! empty($tableFromSql)) { + if ($table != $tableFromSql && $tableFromSql) { $table = $tableFromSql; } } From 18a16915dfaa34000c59f6297ce54b2aa82291f2 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 5 Jan 2022 01:42:21 +0000 Subject: [PATCH 05/18] Use elvis operator instead of if Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index 188b39c560..9618c9f195 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -1610,9 +1610,7 @@ class Sql $tableFromSql, ] = ParseAnalyze::sqlQuery($sqlQuery, $db); - if ($table != $tableFromSql && $tableFromSql) { - $table = $tableFromSql; - } + $table = $tableFromSql ?: $table; } return $this->executeQueryAndGetQueryResponse( From aa49e38fc00b77ec523506629e772cd900ff37dd Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 5 Jan 2022 01:52:36 +0000 Subject: [PATCH 06/18] Invert the condition within foreach Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index 9618c9f195..b27e425f81 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -197,13 +197,15 @@ class Sql $indexColumns = $index->getColumns(); $numberFound = 0; foreach (array_keys($indexColumns) as $indexColumnName) { - if (in_array($indexColumnName, $resultSetColumnNames)) { - $numberFound++; - } elseif (! in_array($indexColumnName, $columns)) { - $numberFound++; - } elseif (str_contains($columns[$indexColumnName]['Extra'], 'INVISIBLE')) { - $numberFound++; + if ( + ! in_array($indexColumnName, $resultSetColumnNames) + && in_array($indexColumnName, $columns) + && ! str_contains($columns[$indexColumnName]['Extra'], 'INVISIBLE') + ) { + continue; } + + $numberFound++; } if ($numberFound == count($indexColumns)) { From 1df141dcd67d5eb866e1e31581956589b680fb22 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 5 Jan 2022 01:56:34 +0000 Subject: [PATCH 07/18] Remove unused parameter Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index b27e425f81..7fc6fd8f54 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -1783,15 +1783,12 @@ class Sql * Function to define pos to display a row * * @param int $numberOfLine Number of the line to display - * @param int $maxRows Number of rows by page * * @return int Start position to display the line */ - private function getStartPosToDisplayRow($numberOfLine, $maxRows = null) + private function getStartPosToDisplayRow($numberOfLine) { - if ($maxRows === null) { - $maxRows = $_SESSION['tmpval']['max_rows']; - } + $maxRows = $_SESSION['tmpval']['max_rows']; return @((int) ceil($numberOfLine / $maxRows) - 1) * $maxRows; } From 53518dc2662900eb8f0103926d124ac1bc39a098 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 5 Jan 2022 01:58:11 +0000 Subject: [PATCH 08/18] Fix argument type Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index 7fc6fd8f54..c4e0a34be7 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -552,7 +552,7 @@ class Sql $response->setRequestStatus(false); $response->addJSON('message', $message); } else { - Generator::mysqlDie($error, $fullSqlQuery, '', ''); + Generator::mysqlDie($error, $fullSqlQuery, false); } exit; From 7b60bd57fac9bcf4e0ee32c59c0c37ac63a38c02 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 5 Jan 2022 01:59:37 +0000 Subject: [PATCH 09/18] Remove unused pass-by-ref Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index c4e0a34be7..6c03e9f5c1 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -139,7 +139,7 @@ class Sql * * @return string limit clause appended SQL query */ - private function getSqlWithLimitClause(array &$analyzedSqlResults) + private function getSqlWithLimitClause(array $analyzedSqlResults) { return Query::replaceClause( $analyzedSqlResults['statement'], From 51207fff0b3348ea64ca3a2365ca6104371c69c9 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 5 Jan 2022 02:12:29 +0000 Subject: [PATCH 10/18] Set parameter type Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index 6c03e9f5c1..a298972d96 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -449,7 +449,7 @@ class Sql * * @return bool|Message */ - public function setColumnProperty($table, $requestIndex) + public function setColumnProperty(Table $table, string $requestIndex) { $propertyValue = array_map('intval', explode(',', $_POST[$requestIndex])); switch ($requestIndex) { @@ -490,7 +490,7 @@ class Sql * * @return string the default $sql_query for browse page */ - public function getDefaultSqlQueryForBrowse($db, $table) + public function getDefaultSqlQueryForBrowse($db, $table): string { $bookmark = Bookmark::get($this->dbi, $GLOBALS['cfg']['Server']['user'], $db, $table, 'label', false, true); @@ -684,9 +684,9 @@ class Sql * @param string|null $column current column * @param bool $purge whether purge set or not */ - private function cleanupRelations(string $db, string $table, ?string $column, $purge): void + private function cleanupRelations(string $db, string $table, ?string $column, bool $purge): void { - if (empty($purge) || $db === '') { + if (! $purge || $db === '') { return; } @@ -703,7 +703,7 @@ class Sql /** * Function to count the total number of rows for the same 'SELECT' query without - * the 'LIMIT' clause that may have been programatically added + * the 'LIMIT' clause that may have been programmatically added * * @param int|string $numRows number of rows affected/changed by the query * @param bool $justBrowsing whether just browsing or not @@ -717,13 +717,13 @@ class Sql */ private function countQueryResults( $numRows, - $justBrowsing, - $db, - $table, + bool $justBrowsing, + string $db, + string $table, array $analyzedSqlResults ) { /* Shortcut for not analyzed/empty query */ - if (empty($analyzedSqlResults)) { + if ($analyzedSqlResults === []) { return 0; } @@ -806,8 +806,8 @@ class Sql * @param string $db current database * @param string|null $table current table * @param bool|null $findRealEnd whether to find the real end - * @param string $sqlQueryForBookmark sql query to be stored as bookmark - * @param array $extraData extra data + * @param string|null $sqlQueryForBookmark sql query to be stored as bookmark + * @param array|null $extraData extra data * * @return array */ @@ -818,7 +818,7 @@ class Sql string $db, ?string $table, ?bool $findRealEnd, - $sqlQueryForBookmark, + ?string $sqlQueryForBookmark, $extraData ) { $response = ResponseRenderer::getInstance(); @@ -868,9 +868,9 @@ class Sql $justBrowsing = self::isJustBrowsing($analyzedSqlResults, $findRealEnd ?? null); - $unlimNumRows = $this->countQueryResults($numRows, $justBrowsing, $db, $table, $analyzedSqlResults); + $unlimNumRows = $this->countQueryResults($numRows, $justBrowsing, $db, $table ?? '', $analyzedSqlResults); - $this->cleanupRelations($db, $table ?? '', $_POST['dropped_column'] ?? null, $_POST['purge'] ?? null); + $this->cleanupRelations($db, $table ?? '', $_POST['dropped_column'] ?? null, ! empty($_POST['purge'])); if ( isset($_POST['dropped_column']) @@ -906,7 +906,7 @@ class Sql * @param string $table current table * @param array $analyzedSqlResults analyzed sql results */ - private function deleteTransformationInfo($db, $table, array $analyzedSqlResults): void + private function deleteTransformationInfo(string $db, string $table, array $analyzedSqlResults): void { if (! isset($analyzedSqlResults['statement'])) { return; @@ -1041,7 +1041,7 @@ class Sql ?string $completeQuery ) { if ($this->isDeleteTransformationInfo($analyzedSqlResults)) { - $this->deleteTransformationInfo($db, $table, $analyzedSqlResults); + $this->deleteTransformationInfo($db, $table ?? '', $analyzedSqlResults); } if (isset($extraData['error'])) { @@ -1723,9 +1723,9 @@ class Sql $isGotoFile, $db, $table, - $findRealEnd ?? null, - $sqlQueryForBookmark ?? null, - $extraData ?? null + $findRealEnd, + $sqlQueryForBookmark, + $extraData ); if ($this->dbi->moreResults()) { From e8bfa5e4e693ea602dca0c6093afc2e8f57ae8ea Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Fri, 7 Jan 2022 01:26:44 +0000 Subject: [PATCH 11/18] Remove redundant condition Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index a298972d96..7655af9461 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -1113,7 +1113,6 @@ class Sql $bookmarkFeature = $this->relation->getRelationParameters()->bookmarkFeature; if ( $bookmarkFeature !== null - && $displayParts['bkm_form'] == '1' && empty($_GET['id_bookmark']) && $sqlQuery ) { From b20922b7736ca6746ab02436be47e82c8640648a Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Fri, 7 Jan 2022 01:43:08 +0000 Subject: [PATCH 12/18] Simplify getDefaultSqlQueryForBrowse Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 58 +++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index 7655af9461..c777cdb57c 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -502,39 +502,37 @@ class Sql $GLOBALS['using_bookmark_message']->addHtml( MySQLDocumentation::showDocumentation('faq', 'faq6-22') ); - $sqlQuery = $bookmark->getQuery(); - } else { - $defaultOrderByClause = ''; - if ( - isset($GLOBALS['cfg']['TablePrimaryKeyOrder']) - && ($GLOBALS['cfg']['TablePrimaryKeyOrder'] !== 'NONE') - ) { - $primaryKey = null; - $primary = Index::getPrimary($table, $db); - - if ($primary !== false) { - $primarycols = $primary->getColumns(); - - foreach ($primarycols as $col) { - $primaryKey = $col->getName(); - break; - } - - if ($primaryKey != null) { - $defaultOrderByClause = ' ORDER BY ' - . Util::backquote($table) . '.' - . Util::backquote($primaryKey) . ' ' - . $GLOBALS['cfg']['TablePrimaryKeyOrder']; - } - } - } - - $sqlQuery = 'SELECT * FROM ' . Util::backquote($table) - . $defaultOrderByClause; + return $bookmark->getQuery(); } - return $sqlQuery; + $defaultOrderByClause = ''; + + if ( + isset($GLOBALS['cfg']['TablePrimaryKeyOrder']) + && ($GLOBALS['cfg']['TablePrimaryKeyOrder'] !== 'NONE') + ) { + $primaryKey = null; + $primary = Index::getPrimary($table, $db); + + if ($primary !== false) { + $primarycols = $primary->getColumns(); + + foreach ($primarycols as $col) { + $primaryKey = $col->getName(); + break; + } + + if ($primaryKey !== null) { + $defaultOrderByClause = ' ORDER BY ' + . Util::backquote($table) . '.' + . Util::backquote($primaryKey) . ' ' + . $GLOBALS['cfg']['TablePrimaryKeyOrder']; + } + } + } + + return 'SELECT * FROM ' . Util::backquote($table) . $defaultOrderByClause; } /** From 7f8c1355fa8cc58075dc2b30f5f2a1ef05b85aeb Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Fri, 7 Jan 2022 21:00:37 +0000 Subject: [PATCH 13/18] Add check for false value Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index c777cdb57c..248b479698 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -597,6 +597,11 @@ class Sql $bfields, isset($_POST['bkm_all_users']) ); + + if ($bookmark === false) { + return; + } + $bookmark->save(); } From d280d724ed42d411d1e14066a4d800a136e27460 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Fri, 7 Jan 2022 21:05:39 +0000 Subject: [PATCH 14/18] Add return type Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index 248b479698..98ed7ff373 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -811,8 +811,6 @@ class Sql * @param bool|null $findRealEnd whether to find the real end * @param string|null $sqlQueryForBookmark sql query to be stored as bookmark * @param array|null $extraData extra data - * - * @return array */ private function executeTheQuery( array $analyzedSqlResults, @@ -823,7 +821,7 @@ class Sql ?bool $findRealEnd, ?string $sqlQueryForBookmark, $extraData - ) { + ): array { $response = ResponseRenderer::getInstance(); $response->getHeader()->getMenu()->setTable($table ?? ''); @@ -935,14 +933,12 @@ class Sql * @param string $messageToShow message to show * @param array $analyzedSqlResults analyzed sql results * @param int $numRows number of rows - * - * @return Message */ private function getMessageForNoRowsReturned( $messageToShow, array $analyzedSqlResults, $numRows - ) { + ): Message { if ($analyzedSqlResults['querytype'] === 'DELETE"') { $message = Message::getMessageForDeletedRows($numRows); } elseif ($analyzedSqlResults['is_insert']) { @@ -1042,7 +1038,7 @@ class Sql $result, $sqlQuery, ?string $completeQuery - ) { + ): string { if ($this->isDeleteTransformationInfo($analyzedSqlResults)) { $this->deleteTransformationInfo($db, $table ?? '', $analyzedSqlResults); } @@ -1193,8 +1189,6 @@ class Sql * @param ResultInterface|false|null $result result of the executed query * @param array $analyzedSqlResults analyzed sql results * @param bool $isLimitedDisplay Show only limited operations or not - * - * @return string */ private function getHtmlForSqlQueryResultsTable( $displayResultsObject, @@ -1206,7 +1200,7 @@ class Sql $result, array $analyzedSqlResults, $isLimitedDisplay = false - ) { + ): string { $printView = isset($_POST['printview']) && $_POST['printview'] == '1' ? '1' : null; $tableHtml = ''; $isBrowseDistinct = ! empty($_POST['is_browse_distinct']); @@ -1409,7 +1403,7 @@ class Sql ?array $profilingResults, $sqlQuery, ?string $completeQuery - ) { + ): string { global $showtable; // If we are retrieving the full value of a truncated field or the original @@ -1670,7 +1664,7 @@ class Sql $dispMessage, $sqlQuery, ?string $completeQuery - ) { + ): string { // Handle disable/enable foreign key checks $defaultFkCheck = ForeignKey::handleDisableCheckInit(); From caeb5fef298006368a09a79b2d08f108994b17db Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Fri, 7 Jan 2022 21:28:00 +0000 Subject: [PATCH 15/18] Cleanup getHtmlForPreviousUpdateQuery Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index 98ed7ff373..6d86fb619c 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -1304,8 +1304,7 @@ class Sql } /** - * Function to get html for the previous query if there is such. If not will return - * null + * Function to get html for the previous query if there is such. * * @param string|null $displayQuery display query * @param bool $showSql whether to show sql @@ -1315,11 +1314,11 @@ class Sql private function getHtmlForPreviousUpdateQuery( ?string $displayQuery, bool $showSql, - $sqlData, + array $sqlData, $displayMessage ): string { $output = ''; - if (isset($displayQuery) && ($showSql === true) && empty($sqlData)) { + if ($displayQuery !== null && $showSql && $sqlData === []) { $output = Generator::getMessage($displayMessage, $displayQuery, 'success'); } @@ -1508,9 +1507,9 @@ class Sql } $previousUpdateQueryHtml = $this->getHtmlForPreviousUpdateQuery( - $dispQuery ?? null, + $dispQuery, (bool) $GLOBALS['cfg']['ShowSQL'], - $sqlData ?? null, + $sqlData ?? [], $dispMessage ?? '' ); From 8e035557a74fd559bb17c70fcdac148799e60a7b Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Fri, 7 Jan 2022 21:31:17 +0000 Subject: [PATCH 16/18] Refactor getMessageIfMissingColumnIndex Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index 6d86fb619c..6ce2a6021c 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -1333,10 +1333,18 @@ class Sql * @param bool $editable whether the results table can be editable or not * @param bool $hasUniqueKey whether there is a unique key */ - private function getMessageIfMissingColumnIndex(?string $table, string $database, $editable, $hasUniqueKey): string - { + private function getMessageIfMissingColumnIndex( + ?string $table, + string $database, + bool $editable, + bool $hasUniqueKey + ): string { + if ($table === null) { + return ''; + } + $output = ''; - if ($table && (Utilities::isSystemSchema($database) || ! $editable)) { + if (Utilities::isSystemSchema($database) || ! $editable) { $output = Message::notice( sprintf( __( @@ -1350,7 +1358,7 @@ class Sql ) ) )->getDisplay(); - } elseif ($table && ! $hasUniqueKey) { + } elseif (! $hasUniqueKey) { $output = Message::notice( sprintf( __( From 643c642c9dfc97a6574abad51a9cdbdfb6d93273 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Fri, 7 Jan 2022 21:53:09 +0000 Subject: [PATCH 17/18] Remove redundant null coalesce Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index 6ce2a6021c..0536a707ae 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -1743,14 +1743,14 @@ class Sql $analyzedSqlResults, $db, $table, - $messageToShow ?? null, + $messageToShow, $numRows, $displayResultsObject, $extraData, $profilingResults, $result, $sqlQuery, - $completeQuery ?? null + $completeQuery ); } else { // At least one row is returned -> displays a table with results @@ -1759,15 +1759,15 @@ class Sql $analyzedSqlResults, $db, $table, - $sqlData ?? null, + $sqlData, $displayResultsObject, $unlimNumRows, $numRows, - $dispQuery ?? null, - $dispMessage ?? null, + $dispQuery, + $dispMessage, $profilingResults, $sqlQuery, - $completeQuery ?? null + $completeQuery ); } From 84e899bb869f5e3c05eebfa6635d81d8b116cab9 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Fri, 7 Jan 2022 21:14:27 +0000 Subject: [PATCH 18/18] Fix parameter types Signed-off-by: Kamil Tekiela --- libraries/classes/Sql.php | 36 ++++++++++++++------- phpstan-baseline.neon | 66 ++++++++------------------------------- psalm-baseline.xml | 49 +++++++---------------------- 3 files changed, 49 insertions(+), 102 deletions(-) diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index 0536a707ae..85ac0fc728 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -811,6 +811,14 @@ class Sql * @param bool|null $findRealEnd whether to find the real end * @param string|null $sqlQueryForBookmark sql query to be stored as bookmark * @param array|null $extraData extra data + * + * @psalm-return array{ + * ResultInterface|false|null, + * int|numeric-string, + * int|numeric-string, + * array|null, + * array|null + * } */ private function executeTheQuery( array $analyzedSqlResults, @@ -830,6 +838,7 @@ class Sql $result = null; $numRows = 0; $unlimNumRows = 0; + $profilingResults = null; } else { // If we don't ask to see the php code Profiling::enable($this->dbi); @@ -895,7 +904,7 @@ class Sql $result, $numRows, $unlimNumRows, - $profilingResults ?? null, + $profilingResults, $extraData, ]; } @@ -930,12 +939,12 @@ class Sql /** * Function to get the message for the no rows returned case * - * @param string $messageToShow message to show - * @param array $analyzedSqlResults analyzed sql results - * @param int $numRows number of rows + * @param string|null $messageToShow message to show + * @param array $analyzedSqlResults analyzed sql results + * @param int|string $numRows number of rows */ private function getMessageForNoRowsReturned( - $messageToShow, + ?string $messageToShow, array $analyzedSqlResults, $numRows ): Message { @@ -1016,13 +1025,14 @@ class Sql * @param string $db current database * @param string|null $table current table * @param string|null $messageToShow message to show - * @param int $numRows number of rows + * @param int|string $numRows number of rows * @param DisplayResults $displayResultsObject DisplayResult instance * @param array|null $extraData extra data * @param array|null $profilingResults profiling results * @param ResultInterface|false|null $result executed query results * @param string $sqlQuery sql query * @param string|null $completeQuery complete sql query + * @psalm-param int|numeric-string $numRows * * @return string html */ @@ -1046,7 +1056,7 @@ class Sql if (isset($extraData['error'])) { $message = Message::rawError($extraData['error']); } else { - $message = $this->getMessageForNoRowsReturned($messageToShow ?? null, $analyzedSqlResults, $numRows); + $message = $this->getMessageForNoRowsReturned($messageToShow, $analyzedSqlResults, $numRows); } $queryMessage = Generator::getMessage($message, $GLOBALS['sql_query'], 'success'); @@ -1183,12 +1193,14 @@ class Sql * @param array $displayParts the parts to display * @param bool $editable whether the result table is * editable or not - * @param int $unlimNumRows unlimited number of rows - * @param int $numRows number of rows + * @param int|string $unlimNumRows unlimited number of rows + * @param int|string $numRows number of rows * @param array|null $showTable table definitions * @param ResultInterface|false|null $result result of the executed query * @param array $analyzedSqlResults analyzed sql results * @param bool $isLimitedDisplay Show only limited operations or not + * @psalm-param int|numeric-string $unlimNumRows + * @psalm-param int|numeric-string $numRows */ private function getHtmlForSqlQueryResultsTable( $displayResultsObject, @@ -1386,13 +1398,15 @@ class Sql * @param string|null $table current table * @param array|null $sqlData sql data * @param DisplayResults $displayResultsObject Instance of DisplayResults - * @param int $unlimNumRows unlimited number of rows - * @param int $numRows number of rows + * @param int|string $unlimNumRows unlimited number of rows + * @param int|string $numRows number of rows * @param string|null $dispQuery display query * @param Message|string|null $dispMessage display message * @param array|null $profilingResults profiling results * @param string $sqlQuery sql query * @param string|null $completeQuery complete sql query + * @psalm-param int|numeric-string $unlimNumRows + * @psalm-param int|numeric-string $numRows * * @return string html */ diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 60d42e448c..620d87d418 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -7785,11 +7785,6 @@ parameters: count: 1 path: libraries/classes/Setup/Index.php - - - message: "#^Cannot call method save\\(\\) on PhpMyAdmin\\\\Bookmark\\|false\\.$#" - count: 1 - path: libraries/classes/Sql.php - - message: "#^Method PhpMyAdmin\\\\Sql\\:\\:countQueryResults\\(\\) has parameter \\$analyzedSqlResults with no value type specified in iterable type array\\.$#" count: 1 @@ -7971,13 +7966,23 @@ parameters: path: libraries/classes/Sql.php - - message: "#^Parameter \\#1 \\$messageToShow of method PhpMyAdmin\\\\Sql\\:\\:getMessageForNoRowsReturned\\(\\) expects string, string\\|null given\\.$#" + message: "#^Parameter \\#1 \\$numberOfLine of method PhpMyAdmin\\\\Sql\\:\\:getStartPosToDisplayRow\\(\\) expects int, mixed given\\.$#" + count: 2 + path: libraries/classes/Sql.php + + - + message: "#^Parameter \\#1 \\$rows of static method PhpMyAdmin\\\\Message\\:\\:getMessageForAffectedRows\\(\\) expects int, int\\|string given\\.$#" + count: 2 + path: libraries/classes/Sql.php + + - + message: "#^Parameter \\#1 \\$rows of static method PhpMyAdmin\\\\Message\\:\\:getMessageForDeletedRows\\(\\) expects int, int\\|string given\\.$#" count: 1 path: libraries/classes/Sql.php - - message: "#^Parameter \\#1 \\$numberOfLine of method PhpMyAdmin\\\\Sql\\:\\:getStartPosToDisplayRow\\(\\) expects int, mixed given\\.$#" - count: 2 + message: "#^Parameter \\#1 \\$rows of static method PhpMyAdmin\\\\Message\\:\\:getMessageForInsertedRows\\(\\) expects int, int\\|string given\\.$#" + count: 1 path: libraries/classes/Sql.php - @@ -7990,66 +7995,21 @@ parameters: count: 1 path: libraries/classes/Sql.php - - - message: "#^Parameter \\#2 \\$db of method PhpMyAdmin\\\\Sql\\:\\:storeTheQueryAsBookmark\\(\\) expects string, string\\|null given\\.$#" - count: 1 - path: libraries/classes/Sql.php - - message: "#^Parameter \\#2 \\$foreign_field of method PhpMyAdmin\\\\ConfigStorage\\\\Relation\\:\\:foreignDropdown\\(\\) expects string, mixed given\\.$#" count: 1 path: libraries/classes/Sql.php - - - message: "#^Parameter \\#2 \\$table of method PhpMyAdmin\\\\Sql\\:\\:deleteTransformationInfo\\(\\) expects string, string\\|null given\\.$#" - count: 1 - path: libraries/classes/Sql.php - - message: "#^Parameter \\#2 \\$table of method PhpMyAdmin\\\\Sql\\:\\:handleSortOrder\\(\\) expects string, string\\|null given\\.$#" count: 1 path: libraries/classes/Sql.php - - - message: "#^Parameter \\#3 \\$db of method PhpMyAdmin\\\\Sql\\:\\:countQueryResults\\(\\) expects string, string\\|null given\\.$#" - count: 1 - path: libraries/classes/Sql.php - - - - message: "#^Parameter \\#3 \\$isModifyLink of static method PhpMyAdmin\\\\Html\\\\Generator\\:\\:mysqlDie\\(\\) expects bool, string given\\.$#" - count: 1 - path: libraries/classes/Sql.php - - - - message: "#^Parameter \\#3 \\$sqlData of method PhpMyAdmin\\\\Sql\\:\\:getHtmlForPreviousUpdateQuery\\(\\) expects array, array\\|null given\\.$#" - count: 1 - path: libraries/classes/Sql.php - - message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, mixed given\\.$#" count: 1 path: libraries/classes/Sql.php - - - message: "#^Parameter \\#4 \\$table of method PhpMyAdmin\\\\Sql\\:\\:countQueryResults\\(\\) expects string, string\\|null given\\.$#" - count: 1 - path: libraries/classes/Sql.php - - - - message: "#^Parameter \\#7 \\$sqlQueryForBookmark of method PhpMyAdmin\\\\Sql\\:\\:executeTheQuery\\(\\) expects string, string\\|null given\\.$#" - count: 1 - path: libraries/classes/Sql.php - - - - message: "#^Parameter \\#8 \\$extraData of method PhpMyAdmin\\\\Sql\\:\\:executeTheQuery\\(\\) expects array, array\\|null given\\.$#" - count: 1 - path: libraries/classes/Sql.php - - - - message: "#^Variable \\$db on left side of \\?\\? always exists and is not nullable\\.$#" - count: 2 - path: libraries/classes/Sql.php - - message: "#^Method PhpMyAdmin\\\\SqlQueryForm\\:\\:init\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 2ad8110f4b..c10e73000c 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -12703,17 +12703,13 @@ - - '' - $unlimNumRows - + $_POST[$requestIndex] $_POST['bkm_label'] $_POST['dropped_column'] ?? null - $_POST['purge'] ?? null $_POST['table_create_time'] ?? null $analyzedSqlResults $analyzedSqlResults['is_affected'] @@ -12745,26 +12741,18 @@ $analyzedSqlResults['statement']->where $columns[$indexColumnName]['Extra'] $db - $extraData $extraData['error'] $fieldInfoResult[0]['Type'] $foreignData['foreign_field'] - $numRows - $numRows $numberOfLine / $maxRows $oneResult['Duration'] $oneResult['Status'] - $profilingResults - $profilingResults - $result - $result $sortCol $statement $table $tokenList $unlimNumRows $unlimNumRows - $unlimNumRows Message::sanitize($warning) @@ -12861,38 +12849,23 @@ $unlimNumRows - - save - - - $_POST['purge'] ?? null - $db - $db - $extraData ?? null - $messageToShow ?? null + + $numRows + $numRows + $numRows + $numRows + + + $numRows + + $result $row[0] - $sqlData ?? null - $sqlQueryForBookmark ?? null - $table - $table $table (bool) $GLOBALS['cfg']['ShowSQL'] - - $db - $db - $db - $table - - - '' - '' - '' - '' -