From 016c1b2357fbbc3dfa8478a450833692cb12e3f8 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Sun, 30 Mar 2025 21:08:06 +0200 Subject: [PATCH] PHPStan: Fix types --- adminer/drivers/mysql.inc.php | 4 ++-- adminer/edit.inc.php | 11 +++++++---- adminer/include/adminer.inc.php | 26 +++++++++++--------------- adminer/include/editing.inc.php | 2 +- adminer/select.inc.php | 10 +++++----- editor/include/adminer.inc.php | 4 ++-- phpstan.neon | 12 ++++++------ 7 files changed, 34 insertions(+), 35 deletions(-) diff --git a/adminer/drivers/mysql.inc.php b/adminer/drivers/mysql.inc.php index 260d8ea3..1235bcc2 100644 --- a/adminer/drivers/mysql.inc.php +++ b/adminer/drivers/mysql.inc.php @@ -27,8 +27,8 @@ if (!defined('Adminer\DRIVER')) { ($server . $username != "" ? $username : ini_get("mysqli.default_user")), ($server . $username . $password != "" ? $password : ini_get("mysqli.default_pw")), null, - (is_numeric($port) ? $port : ini_get("mysqli.default_port")), - (is_numeric($port) ? intval($port) : null), + (is_numeric($port) ? intval($port) : ini_get("mysqli.default_port")), + (is_numeric($port) ? $port : null), ($ssl ? ($ssl['verify'] !== false ? 2048 : 64) : 0) // 2048 - MYSQLI_CLIENT_SSL, 64 - MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT (not available before PHP 5.6.16) ); $this->options(MYSQLI_OPT_LOCAL_INFILE, false); diff --git a/adminer/edit.inc.php b/adminer/edit.inc.php index 68925ed3..5e661f3e 100644 --- a/adminer/edit.inc.php +++ b/adminer/edit.inc.php @@ -3,7 +3,10 @@ namespace Adminer; $TABLE = $_GET["edit"]; $fields = fields($TABLE); -$where = (isset($_GET["select"]) ? ($_POST["check"] && count($_POST["check"]) == 1 ? where_check($_POST["check"][0], $fields) : "") : where($_GET, $fields)); +$where = (isset($_GET["select"]) + ? ($_POST["check"] && count($_POST["check"]) == 1 ? where_check($_POST["check"][0], $fields) : "") + : where($_GET, $fields) +); $update = (isset($_GET["select"]) ? $_POST["edit"] : $where); foreach ($fields as $name => $field) { if (!isset($field["privileges"][$update ? "update" : "insert"]) || adminer()->fieldName($field) == "" || $field["generated"]) { @@ -27,7 +30,7 @@ if ($_POST && !$error && !isset($_GET["select"])) { queries_redirect( $location, lang('Item has been deleted.'), - driver()->delete($TABLE, $query_where, !$unique_array) + driver()->delete($TABLE, $query_where, $unique_array ? 0 : 1) ); } else { @@ -46,7 +49,7 @@ if ($_POST && !$error && !isset($_GET["select"])) { queries_redirect( $location, lang('Item has been updated.'), - driver()->update($TABLE, $set, $query_where, !$unique_array) + driver()->update($TABLE, $set, $query_where, $unique_array ? 0 : 1) ); if (is_ajax()) { page_headers(); @@ -94,7 +97,7 @@ if ($_POST["save"]) { if (!support("table") && !$fields) { // used by Mongo and SimpleDB if (!$where) { // insert - $result = driver()->select($TABLE, array("*"), $where, array("*")); + $result = driver()->select($TABLE, array("*"), array(), array("*")); $row = ($result ? $result->fetch_assoc() : false); if (!$row) { $row = array(driver()->primary => ""); diff --git a/adminer/include/adminer.inc.php b/adminer/include/adminer.inc.php index 7878f5ef..f64b8710 100644 --- a/adminer/include/adminer.inc.php +++ b/adminer/include/adminer.inc.php @@ -159,7 +159,7 @@ class Adminer { } /** Field caption used in select and edit - * @param Field $field + * @param Field|RoutineField $field * @param int $order order of column in select * @return string HTML code, "" to ignore field */ @@ -259,7 +259,7 @@ class Adminer { /** Get descriptions of selected data * @param list $rows all data to print - * @param ForeignKey[] $foreignKeys + * @param list[] $foreignKeys * @return list */ function rowDescriptions(array $rows, array $foreignKeys): array { @@ -428,10 +428,8 @@ class Adminer { echo "\n"; } - /** Print limit box in select - * @param string $limit result of selectLimitProcess() - */ - function selectLimitPrint(string $limit): void { + /** Print limit box in select */ + function selectLimitPrint(int $limit): void { echo "
" . lang('Limit') . "
"; //
for easy styling echo ""; echo script("qsl('input').oninput = selectFieldChange;", ""); @@ -585,11 +583,9 @@ class Adminer { return $return; } - /** Process limit box in select - * @return string expression to use in LIMIT, will be escaped - */ - function selectLimitProcess(): string { - return (isset($_GET["limit"]) ? $_GET["limit"] : "50"); + /** Process limit box in select */ + function selectLimitProcess(): int { + return (isset($_GET["limit"]) ? intval($_GET["limit"]) : 50); } /** Process length box in select @@ -601,7 +597,7 @@ class Adminer { /** Process extras in select form * @param string[] $where AND conditions - * @param ForeignKey[] $foreignKeys + * @param list[] $foreignKeys * @return bool true if processed, false to process other parts of form */ function selectEmailProcess(array $where, array $foreignKeys): bool { @@ -657,8 +653,8 @@ class Adminer { } /** Functions displayed in edit form - * @param Field $field - * @return list + * @param Field|array{null:bool} $field + * @return string[] */ function editFunctions(array $field): array { $return = ($field["null"] ? "NULL/" : ""); @@ -1015,7 +1011,7 @@ class Adminer { } echo "\n"; } - echo script("syntaxHighlighting('" . (is_object(connection()) ? preg_replace('~^(\d\.?\d).*~s', '\1', connection()->server_info) : "") . "'" + echo script("syntaxHighlighting('" . preg_replace('~^(\d\.?\d).*~s', '\1', connection()->server_info) . "'" . (connection()->flavor == 'maria' ? ", 'maria'" : (connection()->flavor == 'cockroach' ? ", 'cockroach'" : "")) . ");" ); } diff --git a/adminer/include/editing.inc.php b/adminer/include/editing.inc.php index 026e5561..36c5af15 100644 --- a/adminer/include/editing.inc.php +++ b/adminer/include/editing.inc.php @@ -9,7 +9,7 @@ namespace Adminer; * @param int|numeric-string $limit * @return string[] $orgtables */ -function print_select_result($result, $connection2 = null, array $orgtables = array(), $limit = 0): array { +function print_select_result($result, Db $connection2 = null, array $orgtables = array(), $limit = 0): array { $links = array(); // colno => orgtable - create links from these columns $indexes = array(); // orgtable => array(column => colno) - primary keys $columns = array(); // orgtable => array(column => ) - not selected columns in primary key diff --git a/adminer/select.inc.php b/adminer/select.inc.php index 0a11ddf9..682fd09f 100644 --- a/adminer/select.inc.php +++ b/adminer/select.inc.php @@ -180,7 +180,7 @@ if ($_POST && !$error) { $TABLE, $set, " WHERE " . ($where ? implode(" AND ", $where) . " AND " : "") . where_check($unique_idf, $fields), - !$is_group && !$primary, + ($is_group || $primary ? 0 : 1), " " ); if (!$result) { @@ -273,7 +273,7 @@ if (!$columns && support("table")) { $page = $_GET["page"]; if ($page == "last") { $found_rows = get_val(count_rows($TABLE, $where, $is_group, $group)); - $page = floor(max(0, intval($found_rows) - 1) / $limit); + $page = floor(max(0, intval($found_rows) - 1) / intval($limit)); } $select2 = $select; @@ -492,7 +492,7 @@ if (!$columns && support("table")) { $found_rows = ($page ? $page * $limit : 0) + count($rows); } elseif (JUSH != "sql" || !$is_group) { $found_rows = ($is_group ? false : found_rows($table_status, $where)); - if ($found_rows < max(1e4, 2 * ($page + 1) * $limit)) { + if (intval($found_rows) < max(1e4, 2 * ($page + 1) * intval($limit))) { // slow with big tables $found_rows = first(slow_query(count_rows($TABLE, $where, $is_group, $group))); } else { @@ -505,7 +505,7 @@ if (!$columns && support("table")) { if ($pagination) { echo (($found_rows === false ? count($rows) + 1 : $found_rows - $page * $limit) > $limit ? '

' . lang('Load more data') . '' - . script("qsl('a').onclick = partial(selectLoadMore, " . (+$limit) . ", '" . lang('Loading') . "…');", "") + . script("qsl('a').onclick = partial(selectLoadMore, " . intval($limit) . ", '" . lang('Loading') . "…');", "") : '' ); echo "\n"; @@ -516,7 +516,7 @@ if (!$columns && support("table")) { // display first, previous 4, next 4 and last page $max_page = ($found_rows === false ? $page + (count($rows) >= $limit ? 2 : 1) - : floor(($found_rows - 1) / $limit) + : floor(($found_rows - 1) / intval($limit)) ); echo "

"; if (JUSH != "simpledb") { diff --git a/editor/include/adminer.inc.php b/editor/include/adminer.inc.php index 0373809b..11df014e 100644 --- a/editor/include/adminer.inc.php +++ b/editor/include/adminer.inc.php @@ -310,7 +310,7 @@ ORDER BY ORDINAL_POSITION", null, "") as $row function selectLimitPrint($limit) { echo "
" . lang('Limit') . "
"; //
for easy styling - echo html_select("limit", array("", "50", "100"), $limit); + echo html_select("limit", array("", 50, 100), $limit); echo "
\n"; } @@ -414,7 +414,7 @@ ORDER BY ORDINAL_POSITION", null, "") as $row } function selectLimitProcess() { - return (isset($_GET["limit"]) ? $_GET["limit"] : "50"); + return (isset($_GET["limit"]) ? intval($_GET["limit"]) : 50); } function selectLengthProcess() { diff --git a/phpstan.neon b/phpstan.neon index c50e9f67..f07f0536 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,8 +4,9 @@ parameters: ignoreErrors: # need to fix - - "~^Function Adminer\\\\fields_from_edit\\(\\) should return~" # Mongo and SimpleDB + - "~^Function Adminer\\\\fields_from_edit\\(\\) should return|Adminer\\\\Driver::\\$primary~" # Mongo and SimpleDB - "~Adminer\\\\Result.*mysqli_result~" # mysqli_result + - "~Function Adminer\\\\queries\\(\\) never returns Adminer\\\\Result~" # mysqli_result # not real problems - identifier: include.fileNotFound # includes in include/ relative from index.php @@ -14,6 +15,9 @@ parameters: - "~an unknown class OCI-?Lob~" # this looks like PHPStan bug - "~^Variable \\$error might not be defined~" # declared in bootstrap.inc.php - "~^Constant LANG not found~" # defined in lang.inc.php + - "~ an undefined \\w+ Adminer\\\\Db::~" # defined in that versions of Db + - "~^Call to an undefined method Adminer\\\\Result::seek~" # defined in MS SQL + - "~^Call to an undefined method Adminer\\\\Driver::setUserTypes~" # defined in PostgreSQL - "~expects int, float given~" # this will work - "~expects bool~" # truthy values - "~fread expects int<1, max>, 100000~" # 1e6 @@ -25,14 +29,11 @@ parameters: - adminer/include/pdo.inc.php - adminer/drivers/* - - - message: "~ to an undefined ~" # PostgreSQL has this in its version of Db - path: adminer/drivers/pgsql.inc.php - # it probably doesn't like $ar[$key] instead of isset($ar[$key]) and thinks that $ar[$key] is always set - identifier: identical.alwaysFalse - identifier: notEqual.alwaysFalse - identifier: notIdentical.alwaysTrue + - identifier: booleanNot.alwaysTrue - identifier: booleanNot.alwaysFalse - identifier: booleanAnd.alwaysFalse - identifier: booleanAnd.leftAlwaysTrue @@ -51,7 +52,6 @@ parameters: - compile.php # compile_file() excludePaths: - adminer/adminer-plugins* - - adminer/lang/ - adminer/designs.php - adminer/elastic.php - adminer/sqlite.php