1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-07 15:16:44 +02:00

Doc-comments: Fix type errors

This commit is contained in:
Jakub Vrana
2025-03-28 11:46:17 +01:00
parent c169c55d70
commit a9143ccbdc
10 changed files with 44 additions and 40 deletions

View File

@@ -417,7 +417,7 @@ if (!defined('Adminer\DRIVER')) {
/** Get database collation
* @param string[][] $collations result of collations()
*/
function db_collation(string $db, array $collations): string {
function db_collation(string $db, array $collations): ?string {
$return = null;
$create = get_val("SHOW CREATE DATABASE " . idf_escape($db), 1);
if (preg_match('~ COLLATE ([^ ]+)~', $create, $match)) {
@@ -698,7 +698,7 @@ if (!defined('Adminer\DRIVER')) {
* @param string $auto_increment number
* @return Result|bool
*/
function alter_table(string $table, string $name, array $fields, array $foreign, string $comment, string $engine, string $collation, string $auto_increment, string $partitioning) {
function alter_table(string $table, string $name, array $fields, array $foreign, ?string $comment, string $engine, string $collation, string $auto_increment, string $partitioning) {
global $connection;
$alter = array();
foreach ($fields as $field) {
@@ -928,7 +928,7 @@ if (!defined('Adminer\DRIVER')) {
}
/** Get last auto increment ID
* @param Result $result or true
* @param Result|bool $result
*/
function last_id($result): string {
return get_val("SELECT LAST_INSERT_ID()"); // mysql_insert_id() truncates bigint

View File

@@ -48,7 +48,7 @@ if (isset($_GET["pgsql"])) {
);
}
function value(string $val, array $field): string {
function value(?string $val, array $field): ?string {
return ($field["type"] == "bytea" && $val !== null ? pg_unescape_bytea($val) : $val);
}

View File

@@ -4,7 +4,7 @@ namespace Adminer;
// any method change in this file should be transferred to editor/include/adminer.inc.php and plugins.inc.php
class Adminer {
/** @var list<string> */ public array $operators; // operators used in select, null for all operators
/** @var ?list<string> */ public ?array $operators = null; // operators used in select, null for all operators
/** @visibility protected(set) */ public string $error = ''; // HTML
/** Name in title and navigation
@@ -42,7 +42,7 @@ class Adminer {
/** Get server name displayed in breadcrumbs
* @return string HTML code or null
*/
function serverName(string $server): string {
function serverName(?string $server): string {
return h($server);
}
@@ -164,9 +164,9 @@ class Adminer {
/** Print links after select heading
* @param TableStatus $tableStatus result of table_status1()
* @param string $set new item options, NULL for no new item
* @param ?string $set new item options, NULL for no new item
*/
function selectLinks(array $tableStatus, string $set = ""): void {
function selectLinks(array $tableStatus, ?string $set = ""): void {
global $driver;
echo '<p class="links">';
$links = array("select" => lang('Select data'));
@@ -266,16 +266,16 @@ class Adminer {
* @param Field $field single field returned from fields()
* @return string|void null to create the default link
*/
function selectLink(string $val, array $field) {
function selectLink(?string $val, array $field) {
}
/** Value printed in select table
* @param string $val HTML-escaped value to print
* @param string $link link to foreign key
* @param ?string $val HTML-escaped value to print
* @param ?string $link link to foreign key
* @param Field $field single field returned from fields()
* @param string $original original value before applying editVal() and escaping
*/
function selectVal(string $val, string $link, array $field, string $original): string {
function selectVal(?string $val, ?string $link, array $field, ?string $original): string {
$return = ($val === null ? "<i>NULL</i>"
: (preg_match("~char|binary|boolean~", $field["type"]) && !preg_match("~var~", $field["type"]) ? "<code>$val</code>"
: (preg_match('~json~', $field["type"]) ? "<code class='jush-js'>$val</code>"
@@ -290,7 +290,7 @@ class Adminer {
/** Value conversion used in select and edit
* @param Field $field single field returned from fields()
*/
function editVal(string $val, array $field): string {
function editVal(?string $val, array $field): ?string {
return $val;
}
@@ -616,7 +616,7 @@ class Adminer {
* @param int $page index of page starting at zero
* @return string empty string to use default query
*/
function selectQueryBuild(array $select, array $where, array $group, array $order, int $limit, int $page): string {
function selectQueryBuild(array $select, array $where, array $group, array $order, ?int $limit, ?int $page): string {
return "";
}
@@ -653,7 +653,7 @@ class Adminer {
* @param Field[] $fields
* @param mixed $row
*/
function editRowPrint(string $table, array $fields, $row, bool $update): void {
function editRowPrint(string $table, array $fields, $row, ?bool $update): void {
}
/** Functions displayed in edit form
@@ -688,7 +688,7 @@ class Adminer {
* @param string $attrs attributes to use inside the tag
* @return string custom input field or empty string for default
*/
function editInput(string $table, array $field, string $attrs, string $value): string {
function editInput(string $table, array $field, string $attrs, ?string $value): string {
if ($field["type"] == "enum") {
return (isset($_GET["select"]) ? "<label><input type='radio'$attrs value='-1' checked><i>" . lang('original') . "</i></label> " : "")
. ($field["null"] ? "<label><input type='radio'$attrs value=''" . ($value !== null || isset($_GET["select"]) ? "" : " checked") . "><i>NULL</i></label> " : "")
@@ -702,7 +702,7 @@ class Adminer {
* @param string $table table name
* @param Field $field single field from fields()
*/
function editHint(string $table, array $field, string $value): string {
function editHint(string $table, array $field, ?string $value): string {
return "";
}
@@ -710,7 +710,7 @@ class Adminer {
* @param Field $field single field from fields()
* @return string expression to use in a query
*/
function processInput(array $field, string $value, string $function = ""): string {
function processInput(array $field, string $value, ?string $function = ""): string {
if ($function == "SQL") {
return $value; // SQL injection
}

View File

@@ -74,7 +74,7 @@ abstract class SqlDriver {
* @param bool $print whether to print the query
* @return Result|false
*/
function select(string $table, array $select, array $where, array $group, array $order = array(), $limit = 1, int $page = 0, bool $print = false) {
function select(string $table, array $select, array $where, array $group, array $order = array(), $limit = 1, ?int $page = 0, bool $print = false) {
global $adminer;
$is_group = (count($group) < count($select));
$query = $adminer->selectQueryBuild($select, $where, $group, $order, $limit, $page);
@@ -190,7 +190,7 @@ abstract class SqlDriver {
/** Convert value returned by database to actual value
* @param Field $field
*/
function value(string $val, array $field): string {
function value(?string $val, array $field): ?string {
return (method_exists($this->conn, 'value')
? $this->conn->value($val, $field)
: (is_resource($val) ? stream_get_contents($val) : $val)

View File

@@ -7,9 +7,10 @@ namespace Adminer;
* @param Result $result
* @param Db $connection2 connection to examine indexes
* @param string[] $orgtables
* @param int|numeric-string $limit
* @return string[] $orgtables
*/
function select($result, Db $connection2 = null, array $orgtables = array(), int $limit = 0): array {
function select($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
@@ -138,7 +139,7 @@ function textarea(string $name, $value, int $rows = 10, int $cols = 80): void {
/** Generate HTML <select> or <input> if $options are empty
* @param string[] $options
*/
function select_input(string $attrs, array $options, string $value = "", string $onchange = "", string $placeholder = ""): string {
function select_input(string $attrs, array $options, ?string $value = "", string $onchange = "", string $placeholder = ""): string {
$tag = ($options ? "select" : "input");
return "<$tag$attrs" . ($options
? "><option value=''>$placeholder" . optionlist($options, $value, true) . "</select>"
@@ -218,7 +219,7 @@ function get_partitions_info(string $table): array {
}
/** Filter length value including enums */
function process_length(string $length): string {
function process_length(?string $length): string {
global $driver;
$enum_length = $driver->enumLength;
return (preg_match("~^\\s*\\(?\\s*$enum_length(?:\\s*,\\s*$enum_length)*+\\s*\\)?\\s*\$~", $length) && preg_match_all("~$enum_length~", $length, $matches)

View File

@@ -232,7 +232,7 @@ function get_rows(string $query, Db $connection2 = null, string $error = "<p cla
* @param Index[] $indexes result of indexes()
* @return string[]|void null if there is no unique identifier
*/
function unique_array(array $row, array $indexes) {
function unique_array(?array $row, array $indexes) {
foreach ($indexes as $index) {
if (preg_match("~PRIMARY|UNIQUE~", $index["type"])) {
$return = array();
@@ -456,7 +456,7 @@ function query_redirect(string $query, string $location, string $message, bool $
class Queries {
/** @var string[] */ static array $queries = array();
static float $start;
static float $start = 0;
}
/** Execute and remember query
@@ -671,7 +671,7 @@ function dump_csv(array $row): void {
/** Apply SQL function
* @param string $column escaped column identifier
*/
function apply_sql_function(string $function, string $column): string {
function apply_sql_function(?string $function, string $column): string {
return ($function ? ($function == "unixepoch" ? "DATETIME($column, '$function')" : ($function == "count distinct" ? "COUNT(DISTINCT " : strtoupper("$function(")) . "$column)") : $column);
}
@@ -818,7 +818,7 @@ function is_mail(?string $email): bool {
}
/** Check whether the string is URL address */
function is_url(string $string): bool {
function is_url(?string $string): bool {
$domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // one domain component //! IDN
return preg_match("~^(https?)://($domain?\\.)+$domain(:\\d+)?(/.*)?(\\?.*)?(#.*)?\$~i", $string); //! restrict path, query and fragment characters
}

View File

@@ -39,7 +39,7 @@ function target_blank(): string {
}
/** Escape for HTML */
function h(string $string): string {
function h(?string $string): string {
return str_replace("\0", "&#0;", htmlspecialchars($string, ENT_QUOTES, 'utf-8'));
}
@@ -51,7 +51,7 @@ function nl_br(string $string): string {
/** Generate HTML checkbox
* @param string|int $value
*/
function checkbox(string $name, $value, bool $checked, string $label = "", string $onclick = "", string $class = "", string $labelled_by = ""): string {
function checkbox(string $name, $value, ?bool $checked, string $label = "", string $onclick = "", string $class = "", string $labelled_by = ""): string {
$return = "<input type='checkbox' name='$name' value='" . h($value) . "'"
. ($checked ? " checked" : "")
. ($labelled_by ? " aria-labelledby='$labelled_by'" : "")
@@ -91,7 +91,7 @@ function optionlist($options, $selected = null, bool $use_keys = false): string
/** Generate HTML <select>
* @param string[] $options
*/
function html_select(string $name, array $options, string $value = "", string $onchange = "", string $labelled_by = ""): string {
function html_select(string $name, array $options, ?string $value = "", string $onchange = "", string $labelled_by = ""): string {
return "<select name='" . h($name) . "'"
. ($labelled_by ? " aria-labelledby='$labelled_by'" : "")
. ">" . optionlist($options, $value) . "</select>"
@@ -115,8 +115,10 @@ function confirm(string $message = "", string $selector = "qsl('input')"): strin
return script("$selector.onclick = () => confirm('" . ($message ? js_escape($message) : lang('Are you sure?')) . "');", "");
}
/** Print header for hidden fieldset (close by </div></fieldset>) */
function print_fieldset(string $id, string $legend, bool $visible = false): void {
/** Print header for hidden fieldset (close by </div></fieldset>)
* @param bool $visible
*/
function print_fieldset(string $id, string $legend, $visible = false): void {
echo "<fieldset><legend>";
echo "<a href='#fieldset-$id'>$legend</a>";
echo script("qsl('a').onclick = partial(toggle, 'fieldset-$id');", "");
@@ -135,7 +137,7 @@ function js_escape(string $string): string {
}
/** Generate page number for pagination */
function pagination(int $page, int $current): string {
function pagination(int $page, ?int $current): string {
return " " . ($page == $current
? $page + 1
: '<a href="' . h(remove_from_uri("page") . ($page ? "&page=$page" . ($_GET["next"] ? "&next=" . urlencode($_GET["next"]) : "") : "")) . '">' . ($page + 1) . "</a>"
@@ -210,8 +212,9 @@ function input(array $field, $value, string $function, bool $autofocus = false):
$field["length"] = $enums;
}
echo $driver->unconvertFunction($field) . " ";
$table = $_GET["edit"] ?: $_GET["select"];
if ($field["type"] == "enum") {
echo h($functions[""]) . "<td>" . $adminer->editInput($_GET["edit"], $field, $attrs, $value);
echo h($functions[""]) . "<td>" . $adminer->editInput($table, $field, $attrs, $value);
} else {
$has_function = (in_array($function, $functions) || isset($functions[$function]));
echo (count($functions) > 1
@@ -220,7 +223,7 @@ function input(array $field, $value, string $function, bool $autofocus = false):
. script("qsl('select').onchange = functionChange;", "")
: h(reset($functions))
) . '<td>';
$input = $adminer->editInput($_GET["edit"], $field, $attrs, $value); // usage in call is without a table
$input = $adminer->editInput($table, $field, $attrs, $value); // usage in call is without a table
if ($input != "") {
echo $input;
} elseif (preg_match('~bool~', $field["type"])) {
@@ -263,7 +266,7 @@ function input(array $field, $value, string $function, bool $autofocus = false):
. "$attrs>"
;
}
echo $adminer->editHint($_GET["edit"], $field, $value);
echo $adminer->editHint($table, $field, $value);
// skip 'original'
$first = 0;
foreach ($functions as $key => $val) {
@@ -362,7 +365,7 @@ function on_help(string $command, int $side = 0): string {
* @param Field[] $fields
* @param mixed $row
*/
function edit_form(string $table, array $fields, $row, bool $update): void {
function edit_form(string $table, array $fields, $row, ?bool $update): void {
global $adminer, $error;
$table_name = $adminer->tableName(table_status1($table, true));
page_header(

View File

@@ -124,7 +124,7 @@ if (isset($_GET["elastic"])) {
);
}
function select(string $table, array $select, array $where, array $group, array $order = array(), $limit = 1, int $page = 0, bool $print = false) {
function select(string $table, array $select, array $where, array $group, array $order = array(), $limit = 1, ?int $page = 0, bool $print = false) {
$data = array();
if ($select != array("*")) {
$data["fields"] = array_values($select);

View File

@@ -322,7 +322,7 @@ if (isset($_GET["mongo"])) {
public $primary = "_id";
function select(string $table, array $select, array $where, array $group, array $order = array(), $limit = 1, int $page = 0, bool $print = false) {
function select(string $table, array $select, array $where, array $group, array $order = array(), $limit = 1, ?int $page = 0, bool $print = false) {
$select = ($select == array("*")
? array()
: array_fill_keys($select, 1)

View File

@@ -145,7 +145,7 @@ if (isset($_GET["simpledb"])) {
return $return;
}
function select(string $table, array $select, array $where, array $group, array $order = array(), $limit = 1, int $page = 0, bool $print = false) {
function select(string $table, array $select, array $where, array $group, array $order = array(), $limit = 1, ?int $page = 0, bool $print = false) {
$connection = connection();
$connection->next = $_GET["next"];
$return = parent::select($table, $select, $where, $group, $order, $limit, $page, $print);