From 15ac2dec9b7c6ea5e3ffe12be73fb13b08526f5d Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Mon, 8 Dec 2025 13:47:06 +0000 Subject: [PATCH] Better type hints Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 100 ++++++++++------- psalm-baseline.xml | 102 +++++++++++------- .../Database/StructureController.php | 33 +++--- src/Util.php | 5 +- 4 files changed, 142 insertions(+), 98 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index da9fad756f..2892705669 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1966,41 +1966,47 @@ parameters: path: src/Controllers/Database/Structure/RealRowCountController.php - - message: '#^Binary operation "\*" between mixed and int results in an error\.$#' + message: '#^Binary operation "\*" between bool\|int\|string\|null and int results in an error\.$#' identifier: binaryOp.invalid count: 1 path: src/Controllers/Database/StructureController.php - - message: '#^Binary operation "\+" between \(float\|int\) and mixed results in an error\.$#' + message: '#^Binary operation "\*" between mixed and 2 results in an error\.$#' identifier: binaryOp.invalid count: 1 path: src/Controllers/Database/StructureController.php - - message: '#^Binary operation "\+" between mixed and mixed results in an error\.$#' + message: '#^Binary operation "\+" between \(float\|int\) and bool\|int\|string\|null results in an error\.$#' identifier: binaryOp.invalid - count: 3 + count: 1 path: src/Controllers/Database/StructureController.php - - message: '#^Binary operation "\+\=" between \(float\|int\) and mixed results in an error\.$#' + message: '#^Binary operation "\+" between bool\|int\|string\|null and bool\|int\|string\|null results in an error\.$#' + identifier: binaryOp.invalid + count: 1 + path: src/Controllers/Database/StructureController.php + + - + message: '#^Binary operation "\+" between int\|string\|null and int\|string\|null results in an error\.$#' + identifier: binaryOp.invalid + count: 2 + path: src/Controllers/Database/StructureController.php + + - + message: '#^Binary operation "\+\=" between int and int\<1, max\>\|string results in an error\.$#' identifier: assignOp.invalid count: 1 path: src/Controllers/Database/StructureController.php - - message: '#^Binary operation "\+\=" between int and mixed results in an error\.$#' + message: '#^Binary operation "\+\=" between mixed and int\|string\|null results in an error\.$#' identifier: assignOp.invalid count: 1 path: src/Controllers/Database/StructureController.php - - - message: '#^Binary operation "\." between non\-falsy\-string and mixed results in an error\.$#' - identifier: binaryOp.invalid - count: 1 - path: src/Controllers/Database/StructureController.php - - message: '#^Call to function array_search\(\) requires parameter \#3 to be set\.$#' identifier: function.strict @@ -2014,11 +2020,35 @@ parameters: path: src/Controllers/Database/StructureController.php - - message: '#^Method PhpMyAdmin\\Controllers\\Database\\StructureController\:\:getStuffForEngineTypeTable\(\) should return array\{array\, string, string, string, string, int, bool, int\} but returns array\{non\-empty\-array, string, string, string, string, float\|int, bool, int\}\.$#' + message: '#^Method PhpMyAdmin\\Controllers\\Database\\StructureController\:\:getStuffForEngineTypeTable\(\) should return array\{array\, string, string, string, string, int, bool, int\} but returns array\{non\-empty\-array\, string, string, string, string, float\|int, bool, int\}\.$#' identifier: return.type count: 1 path: src/Controllers/Database/StructureController.php + - + message: '#^Method PhpMyAdmin\\Controllers\\Database\\StructureController\:\:getValuesForAriaTable\(\) should return array\{array\, string, string, string, string, float\|int, int\} but returns array\{array\, string, string, string, string, mixed, int\}\.$#' + identifier: return.type + count: 1 + path: src/Controllers/Database/StructureController.php + + - + message: '#^Method PhpMyAdmin\\Controllers\\Database\\StructureController\:\:getValuesForCsvTable\(\) should return array\{array\, string, string, int\} but returns array\{non\-empty\-array\, string, string, int\}\.$#' + identifier: return.type + count: 1 + path: src/Controllers/Database/StructureController.php + + - + message: '#^Method PhpMyAdmin\\Controllers\\Database\\StructureController\:\:getValuesForInnodbTable\(\) should return array\{array\, string, string, int\} but returns array\{non\-empty\-array\, string, string, int\}\.$#' + identifier: return.type + count: 1 + path: src/Controllers/Database/StructureController.php + + - + message: '#^Only booleans are allowed in a negated boolean, int\|string\|null given\.$#' + identifier: booleanNot.exprNotBoolean + count: 1 + path: src/Controllers/Database/StructureController.php + - message: '#^Only booleans are allowed in an if condition, mixed given\.$#' identifier: if.condNotBoolean @@ -2032,25 +2062,31 @@ parameters: path: src/Controllers/Database/StructureController.php - - message: '#^Parameter \#1 \$identifier of static method PhpMyAdmin\\Util\:\:backquote\(\) expects string\|Stringable\|null, mixed given\.$#' + message: '#^Parameter \#1 \$identifier of static method PhpMyAdmin\\Util\:\:backquote\(\) expects string\|Stringable\|null, bool\|int\|string\|null given\.$#' identifier: argument.type - count: 3 + count: 1 path: src/Controllers/Database/StructureController.php - - message: '#^Parameter \#1 \$str of method PhpMyAdmin\\Dbal\\DatabaseInterface\:\:quoteString\(\) expects string, mixed given\.$#' + message: '#^Parameter \#1 \$identifier of static method PhpMyAdmin\\Util\:\:backquote\(\) expects string\|Stringable\|null, int\|string\|null given\.$#' identifier: argument.type count: 2 path: src/Controllers/Database/StructureController.php - - message: '#^Parameter \#1 \$string of function htmlspecialchars expects string, mixed given\.$#' + message: '#^Parameter \#1 \$str of method PhpMyAdmin\\Dbal\\DatabaseInterface\:\:quoteString\(\) expects string, bool\|int\|string\|null given\.$#' + identifier: argument.type + count: 2 + path: src/Controllers/Database/StructureController.php + + - + message: '#^Parameter \#1 \$string of function htmlspecialchars expects string, int\|string\|null given\.$#' identifier: argument.type count: 4 path: src/Controllers/Database/StructureController.php - - message: '#^Parameter \#1 \$string of function md5 expects string, mixed given\.$#' + message: '#^Parameter \#1 \$string of function md5 expects string, int\|string\|null given\.$#' identifier: argument.type count: 1 path: src/Controllers/Database/StructureController.php @@ -2062,7 +2098,7 @@ parameters: path: src/Controllers/Database/StructureController.php - - message: '#^Parameter \#1 \$table of method PhpMyAdmin\\Controllers\\Database\\StructureController\:\:getTrackingIcon\(\) expects string, mixed given\.$#' + message: '#^Parameter \#1 \$table of method PhpMyAdmin\\Controllers\\Database\\StructureController\:\:getTrackingIcon\(\) expects string, int\|string\|null given\.$#' identifier: argument.type count: 1 path: src/Controllers/Database/StructureController.php @@ -2073,12 +2109,6 @@ parameters: count: 2 path: src/Controllers/Database/StructureController.php - - - message: '#^Parameter \#1 \$value of static method PhpMyAdmin\\Util\:\:formatByteDown\(\) expects float\|int\|string\|null, mixed given\.$#' - identifier: argument.type - count: 1 - path: src/Controllers/Database/StructureController.php - - message: '#^Parameter \#2 \$haystack of function array_search expects array, mixed given\.$#' identifier: argument.type @@ -2086,26 +2116,26 @@ parameters: path: src/Controllers/Database/StructureController.php - - message: '#^Parameter \#2 \$table of method PhpMyAdmin\\Controllers\\Database\\StructureController\:\:getReplicationStatus\(\) expects string, mixed given\.$#' + message: '#^Parameter \#2 \$table of method PhpMyAdmin\\Controllers\\Database\\StructureController\:\:getReplicationStatus\(\) expects string, int\|string\|null given\.$#' identifier: argument.type count: 1 path: src/Controllers/Database/StructureController.php - - message: '#^Parameter \#2 \$tableName of method PhpMyAdmin\\Dbal\\DatabaseInterface\:\:getTable\(\) expects string, mixed given\.$#' + message: '#^Parameter \#2 \$tableName of method PhpMyAdmin\\Dbal\\DatabaseInterface\:\:getTable\(\) expects string, int\|string\|null given\.$#' identifier: argument.type - count: 5 + count: 3 path: src/Controllers/Database/StructureController.php - - message: '#^Parameter \#3 \$name of static method PhpMyAdmin\\Charsets\:\:findCollationByName\(\) expects string, mixed given\.$#' + message: '#^Parameter \#2 \$tableName of method PhpMyAdmin\\Dbal\\DatabaseInterface\:\:getTable\(\) expects string, int\|string\|true\|null given\.$#' identifier: argument.type - count: 1 + count: 2 path: src/Controllers/Database/StructureController.php - - message: '#^Possibly invalid array key type mixed\.$#' - identifier: offsetAccess.invalidOffset + message: '#^Parameter \#3 \$name of static method PhpMyAdmin\\Charsets\:\:findCollationByName\(\) expects string, int\|string given\.$#' + identifier: argument.type count: 1 path: src/Controllers/Database/StructureController.php @@ -14172,12 +14202,6 @@ parameters: count: 1 path: src/Util.php - - - message: '#^Method PhpMyAdmin\\Util\:\:getDbInfo\(\) return type has no value type specified in iterable type array\.$#' - identifier: missingType.iterableValue - count: 1 - path: src/Util.php - - message: '#^Method PhpMyAdmin\\Util\:\:getTableListPosition\(\) should return int but returns mixed\.$#' identifier: return.type diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 5b252e2a61..4096f32b9a 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1135,6 +1135,18 @@ + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - @@ -1166,58 +1204,44 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + + + - - - - - - - - - - + + + + + + + + + + + + + + + + + + diff --git a/src/Controllers/Database/StructureController.php b/src/Controllers/Database/StructureController.php index ffdcce6c74..39c532b55c 100644 --- a/src/Controllers/Database/StructureController.php +++ b/src/Controllers/Database/StructureController.php @@ -72,7 +72,7 @@ final class StructureController implements InvocableController /** @var int Number of tables */ private int $totalNumTables = 0; - /** @var mixed[] Tables in the database */ + /** @var (string|int|null)[][] Tables in the database */ private array $tables = []; /** @var bool whether stats show or not */ @@ -231,7 +231,6 @@ final class StructureController implements InvocableController $structureTableRows = []; $trackedTables = $this->trackingChecker->getTrackedTables(Current::$database); $recentFavoriteTables = RecentFavoriteTables::getInstance(TableType::Favorite); - /** @var mixed[] $currentTable */ foreach ($this->tables as $currentTable) { // Get valid statistics whatever is the table type @@ -255,8 +254,7 @@ final class StructureController implements InvocableController $sumSize, ] = $this->getStuffForEngineTypeTable($currentTable, $sumSize, $overheadSize); - $curTable = $this->dbi - ->getTable(Current::$database, $currentTable['TABLE_NAME']); + $curTable = $this->dbi->getTable(Current::$database, $currentTable['TABLE_NAME']); if (! $curTable->isMerge()) { $sumEntries += $currentTable['TABLE_ROWS']; } @@ -535,8 +533,8 @@ final class StructureController implements InvocableController /** * Returns whether the row count is approximated * - * @param mixed[] $currentTable array containing details about the table - * @param bool $tableIsView whether the table is a view + * @param (string|int|null)[] $currentTable array containing details about the table + * @param bool $tableIsView whether the table is a view * * @return array{bool, string} */ @@ -641,11 +639,11 @@ final class StructureController implements InvocableController * * @internal param bool $table_is_view whether table is view or not * - * @param mixed[] $currentTable current table + * @param (string|int|null)[] $currentTable current table * @param int $sumSize total table size * @param int $overheadSize overhead size * - * @psalm-return list{mixed[], string, string, string, string, int, bool, int} + * @return list{(string|int|null)[], string, string, string, string, int, bool, int} */ private function getStuffForEngineTypeTable( array $currentTable, @@ -760,7 +758,7 @@ final class StructureController implements InvocableController /** * Get values for ARIA/MARIA tables * - * @param mixed[] $currentTable current table + * @param (string|int|null)[] $currentTable current table * @param int $sumSize sum size * @param int $overheadSize overhead size * @param string $formattedSize formatted size @@ -768,7 +766,7 @@ final class StructureController implements InvocableController * @param string $formattedOverhead overhead formatted * @param string $overheadUnit overhead unit * - * @return array{mixed[], string, string, string, string, int|float, int} + * @return array{(string|int|null)[], string, string, string, string, int|float, int} */ private function getValuesForAriaTable( array $currentTable, @@ -787,8 +785,7 @@ final class StructureController implements InvocableController if ($this->isShowStats) { /** @var int $tblsize */ - $tblsize = $currentTable['Data_length'] - + $currentTable['Index_length']; + $tblsize = $currentTable['Data_length'] + $currentTable['Index_length']; $sumSize += $tblsize; [$formattedSize, $unit] = Util::formatByteDown($tblsize, 3, $tblsize > 0 ? 1 : 0); if (isset($currentTable['Data_free']) && $currentTable['Data_free'] > 0) { @@ -803,10 +800,10 @@ final class StructureController implements InvocableController /** * Get values for InnoDB table * - * @param mixed[] $currentTable current table + * @param (string|int|null)[] $currentTable current table * @param int $sumSize sum size * - * @return array{mixed[], string, string, int} + * @return array{(string|int|null)[], string, string, int} */ private function getValuesForInnodbTable( array $currentTable, @@ -843,9 +840,9 @@ final class StructureController implements InvocableController * * https://bugs.mysql.com/bug.php?id=53929 * - * @param mixed[] $currentTable + * @param (string|int|null)[] $currentTable * - * @return array{mixed[], string, string, int} + * @return array{(string|int|null)[], string, string, int} */ private function getValuesForCsvTable(array $currentTable, int $sumSize): array { @@ -902,10 +899,10 @@ final class StructureController implements InvocableController /** * Get values for Mroonga table * - * @param mixed[] $currentTable current table + * @param (string|int|null)[] $currentTable current table * @param int $sumSize sum size * - * @return array{mixed[], string, string, int} + * @return array{(string|int|null)[], string, string, int} */ private function getValuesForMroongaTable( array $currentTable, diff --git a/src/Util.php b/src/Util.php index 2d3f66e9dd..0936b0e904 100644 --- a/src/Util.php +++ b/src/Util.php @@ -1486,8 +1486,7 @@ class Util /** * Gets the list of tables in the current db and information about these tables if possible. * - * @return array - * @psalm-return array{array, int} + * @return array{(string|int|null)[][], int} */ public static function getDbInfo(ServerRequest $request, string $db, bool $isResultLimited = true): array { @@ -1604,7 +1603,7 @@ class Util * Gets the list of tables in the current db, taking into account * that they might be "in use" * - * @return mixed[] list of tables + * @return (string|int|null)[][] list of tables */ private static function getTablesWhenOpen(string $db): array {