diff --git a/libraries/classes/Sql.php b/libraries/classes/Sql.php index 86fddedab1..68ddb1c409 100644 --- a/libraries/classes/Sql.php +++ b/libraries/classes/Sql.php @@ -411,9 +411,9 @@ class Sql * @param string $db the current database * @param string $table the current table * - * @return mixed the number of rows if "retain" param is true, otherwise true + * @return int the number of rows */ - public function findRealEndOfRows($db, $table): mixed + public function findRealEndOfRows($db, $table): int { $unlimNumRows = $this->dbi->getTable($db, $table)->countRecords(true); $_SESSION['tmpval']['pos'] = $this->getStartPosToDisplayRow($unlimNumRows); @@ -679,8 +679,7 @@ class Sql * @todo In countRecords(), MaxExactCount is also verified, * so can we avoid checking it twice? */ - $unlimNumRows = $this->dbi->getTable($db, $table) - ->countRecords(true); + $unlimNumRows = $this->dbi->getTable($db, $table)->countRecords(true); } } else { $statement = $statementInfo->statement; @@ -702,10 +701,7 @@ class Sql $countQuery = 'SELECT COUNT(*) FROM (' . $statement->build() . ' ) as cnt'; - $unlimNumRows = $this->dbi->fetchValue($countQuery); - if ($unlimNumRows === false) { - $unlimNumRows = 0; - } + $unlimNumRows = (int) $this->dbi->fetchValue($countQuery); } } else {// not $is_select $unlimNumRows = 0; diff --git a/libraries/classes/Table.php b/libraries/classes/Table.php index 9068907d5d..a9c84604d2 100644 --- a/libraries/classes/Table.php +++ b/libraries/classes/Table.php @@ -37,6 +37,7 @@ use function htmlspecialchars; use function implode; use function in_array; use function is_array; +use function is_numeric; use function is_string; use function json_decode; use function json_encode; @@ -688,24 +689,21 @@ class Table implements Stringable } /** - * Counts and returns (or displays) the number of records in a table + * Counts the number of records in a table * * @param bool $forceExact whether to force an exact count - * - * @return mixed the number of records if "retain" param is true, - * otherwise true */ - public function countRecords($forceExact = false): mixed + public function countRecords($forceExact = false): int { $isView = $this->isView(); $cache = $this->dbi->getCache(); $exactRowsCached = $cache->getCachedTableContent([$this->dbName, $this->name, 'ExactRows']); - if ($exactRowsCached != null) { - return $exactRowsCached; + if ($exactRowsCached !== null) { + return (int) $exactRowsCached; } - $rowCount = false; + $rowCount = null; if (! $forceExact) { if (($cache->getCachedTableContent([$this->dbName, $this->name, 'Rows']) == null) && ! $isView) { @@ -715,12 +713,12 @@ class Table implements Stringable } } - $rowCount = $cache->getCachedTableContent([$this->dbName, $this->name, 'Rows']) ?? false; + $rowCount = $cache->getCachedTableContent([$this->dbName, $this->name, 'Rows']); } // for a VIEW, $row_count is always false at this point - if ($rowCount !== false && $rowCount >= $GLOBALS['cfg']['MaxExactCount']) { - return $rowCount; + if ($rowCount !== null && $rowCount >= $GLOBALS['cfg']['MaxExactCount']) { + return (int) $rowCount; } if (! $isView) { @@ -751,11 +749,13 @@ class Table implements Stringable } } - if ($rowCount) { - $cache->cacheTableContent([$this->dbName, $this->name, 'ExactRows'], $rowCount); + if (is_numeric($rowCount)) { + $cache->cacheTableContent([$this->dbName, $this->name, 'ExactRows'], (int) $rowCount); + + return (int) $rowCount; } - return $rowCount; + return 0; } /** diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 79e47729a1..0d7114a24b 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -931,7 +931,7 @@ parameters: path: libraries/classes/ConfigStorage/Relation.php - - message: "#^Method PhpMyAdmin\\\\ConfigStorage\\\\Relation\\:\\:getForeignData\\(\\) should return array\\{foreign_link\\: bool, the_total\\: mixed, foreign_display\\: string, disp_row\\: array\\\\|null, foreign_field\\: mixed\\} but returns array\\{foreign_link\\: bool, the_total\\: mixed, foreign_display\\: string, disp_row\\: array\\\\>\\|null, foreign_field\\: mixed\\}\\.$#" + message: "#^Method PhpMyAdmin\\\\ConfigStorage\\\\Relation\\:\\:getForeignData\\(\\) should return array\\{foreign_link\\: bool, the_total\\: mixed, foreign_display\\: string, disp_row\\: array\\\\|null, foreign_field\\: mixed\\} but returns array\\{foreign_link\\: bool, the_total\\: int\\|string\\|null, foreign_display\\: string, disp_row\\: array\\\\>\\|null, foreign_field\\: mixed\\}\\.$#" count: 1 path: libraries/classes/ConfigStorage/Relation.php @@ -8181,7 +8181,7 @@ parameters: path: libraries/classes/Sql.php - - message: "#^Method PhpMyAdmin\\\\Sql\\:\\:countQueryResults\\(\\) should return int\\|numeric\\-string but returns mixed\\.$#" + message: "#^Method PhpMyAdmin\\\\Sql\\:\\:countQueryResults\\(\\) should return int\\|numeric\\-string but returns float\\|int\\|numeric\\-string\\.$#" count: 1 path: libraries/classes/Sql.php @@ -8285,11 +8285,6 @@ parameters: count: 1 path: libraries/classes/Sql.php - - - 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 @@ -8407,7 +8402,7 @@ parameters: - message: "#^Cannot cast mixed to int\\.$#" - count: 1 + count: 3 path: libraries/classes/Table.php - diff --git a/psalm-baseline.xml b/psalm-baseline.xml index be19b7f5a5..0ac8f8a7d1 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -847,7 +847,6 @@ $key $one_key $relations - $the_total $value $value @@ -1742,9 +1741,6 @@ $createTime $createTimeAll $currentTable - - - $dbTable $favoriteTables $overheadSize @@ -3303,7 +3299,6 @@ - $bkm_all_users $sql_query @@ -4046,7 +4041,6 @@ $column_name $column_name $curr_rel_field - $multi_edit_auto_increment $multi_edit_columns $multi_edit_columns[$key] @@ -5618,7 +5612,6 @@ formAndOrCols[$lastWhere]]]> $tsize[$table] $tsize[$table] - $tsize[$table] @@ -5638,7 +5631,6 @@ $indexColumns $isWhere $masterTable - $maxsize $name $oneField $oneKey @@ -5655,7 +5647,6 @@ $table $tempSearchTables[] $tmpCriteria - $tsize[$table] $uniqueColumns $value $whereClauseColumns @@ -6660,7 +6651,6 @@ $sqlQuery $sqlQueryAdd $tableCreateTime - $theTotal $total $value orgtable]]]> @@ -13205,9 +13195,6 @@ - - $unlimNumRows - @@ -13218,8 +13205,6 @@ $sortCol expr]]> - $unlimNumRows - $unlimNumRows Message::sanitize($warning) @@ -13262,10 +13247,6 @@ $resultSetColumnNames[] $sortCol $unlimNumRows - $unlimNumRows - $unlimNumRows - $unlimNumRows - $unlimNumRows int @@ -13293,6 +13274,7 @@ $pos $row[$column] $unlimNumRows + $unlimNumRows @((int) ceil($numberOfLine / $maxRows) - 1) * $maxRows @@ -13302,9 +13284,6 @@ statement->where]]> - - $unlimNumRows - @@ -13570,7 +13549,6 @@ $constraintName $currCreateTime $eachCol - $exactRowsCached $foreignDb $foreignDb $foreignField @@ -13594,7 +13572,6 @@ $tableCollation $tableComment $tableNumRowInfo - $tableNumRowInfo $tableOptions $tableRowFormat $tableStorageEngine @@ -14427,7 +14404,6 @@ $p $p $row - $rowCount $table $table @@ -14437,7 +14413,6 @@ array int - int @@ -14448,8 +14423,6 @@ - $rowCount - $rowCount $tableGroups