From 41aad5bc376db157f312b0d3412965f0eacfbf52 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Wed, 26 Mar 2025 02:23:29 +0100 Subject: [PATCH] Doc-comment: Use type aliases for arrays Type aliases could be defined either globally (https://phpstan.org/writing-php-code/phpdoc-types#global-type-aliases) or just for a class. I prefer having them at the place where they are created. --- adminer/drivers/mysql.inc.php | 38 +++++++++++-------- adminer/drivers/sqlite.inc.php | 2 +- adminer/include/adminer.inc.php | 57 +++++++++++++++-------------- adminer/include/driver.inc.php | 12 +++--- adminer/include/editing.inc.php | 26 ++++++------- adminer/include/functions.inc.php | 18 ++++----- adminer/include/html.inc.php | 6 +-- editor/include/editing.inc.php | 4 +- plugins/drivers/elastic.php | 2 +- plugins/login-servers.php | 2 +- plugins/table-indexes-structure.php | 2 +- plugins/table-structure.php | 2 +- 12 files changed, 89 insertions(+), 82 deletions(-) diff --git a/adminer/drivers/mysql.inc.php b/adminer/drivers/mysql.inc.php index 58c544d8..7f72044f 100644 --- a/adminer/drivers/mysql.inc.php +++ b/adminer/drivers/mysql.inc.php @@ -443,7 +443,7 @@ if (!defined('Adminer\DRIVER')) { } /** Connect to the database - * @param array [$server, $username, $password] + * @param array{string, string, string} [$server, $username, $password] * @return mixed Db or string for error */ function connect($credentials) { @@ -549,7 +549,8 @@ if (!defined('Adminer\DRIVER')) { /** Get table status * @param string * @param bool return only "Name", "Engine" and "Comment" fields - * @return array{Name:string, Engine:string, Comment:string, Oid:int, Rows:int, Collation:string, Auto_increment:int, Data_length:int, Index_length:int, Data_free:int}[] + * @return TableStatus[] + * @phpstan-type TableStatus array{Name:string, Engine:string, Comment:string, Oid:int, Rows:int, Collation:string, Auto_increment:int, Data_length:int, Index_length:int, Data_free:int} */ function table_status($name = "", $fast = false) { $return = array(); @@ -577,7 +578,7 @@ if (!defined('Adminer\DRIVER')) { } /** Find out whether the identifier is view - * @param array + * @param TableStatus * @return bool */ function is_view($table_status) { @@ -585,7 +586,7 @@ if (!defined('Adminer\DRIVER')) { } /** Check if table supports foreign keys - * @param array result of table_status + * @param TableStatus result of table_status1() * @return bool */ function fk_support($table_status) { @@ -595,7 +596,8 @@ if (!defined('Adminer\DRIVER')) { /** Get information about fields * @param string - * @return array{field:string, full_type:string, type:string, length:int, unsigned:string, default:string, null:bool, auto_increment:bool, on_update:string, collation:string, privileges:int[], comment:string, primary:bool, generated:string}[] + * @return Field[] + * @phpstan-type Field array{field:string, full_type:string, type:string, length:int, unsigned:string, default:string, null:bool, auto_increment:bool, on_update:string, collation:string, privileges:int[], comment:string, primary:bool, generated:string} */ function fields($table) { global $connection; @@ -651,7 +653,8 @@ if (!defined('Adminer\DRIVER')) { /** Get table indexes * @param string * @param string Db to use - * @return array{type:string, columns:list, lengths:list, descs:list}[] + * @return Index[] + * @phpstan-type Index array{type:string, columns:list, lengths:list, descs:list} */ function indexes($table, $connection2 = null) { $return = array(); @@ -667,7 +670,8 @@ if (!defined('Adminer\DRIVER')) { /** Get foreign keys in table * @param string - * @return array{db:string, ns:string, table:string, source:list, target:list, on_delete:string, on_update:string}[] + * @return ForeignKey[] + * @phpstan-type ForeignKey array{db:string, ns:string, table:string, source:list, target:list, on_delete:string, on_update:string} */ function foreign_keys($table) { global $driver; @@ -807,7 +811,7 @@ if (!defined('Adminer\DRIVER')) { /** Run commands to create or alter table * @param string "" to create * @param string new name - * @param array of [$orig, $process_field, $after] + * @param list, string}> of [$orig, $process_field, $after] * @param list * @param string * @param string @@ -852,7 +856,7 @@ if (!defined('Adminer\DRIVER')) { /** Run commands to alter indexes * @param string escaped table name - * @param array[] of ["index type", "name", ["column definition", ...]] or ["index type", "name", "DROP"] + * @param array{string, string, 'DROP'|list} of ["index type", "name", ["column definition", ...]] or ["index type", "name", "DROP"] * @return bool */ function alter_indexes($table, $alter) { @@ -958,7 +962,8 @@ if (!defined('Adminer\DRIVER')) { /** Get information about trigger * @param string trigger name - * @return array{Trigger:string, Timing:string, Event:string, Of:string, Type:string, Statement:string} + * @return Trigger + * @phpstan-type Trigger array{Trigger:string, Timing:string, Event:string, Of:string, Type:string, Statement:string} */ function trigger($name) { if ($name == "") { @@ -970,7 +975,7 @@ if (!defined('Adminer\DRIVER')) { /** Get defined triggers * @param string - * @return array{string, string}[]4 + * @return array{string, string}[] */ function triggers($table) { $return = array(); @@ -994,7 +999,8 @@ if (!defined('Adminer\DRIVER')) { /** Get information about stored routine * @param string * @param string "FUNCTION" or "PROCEDURE" - * @return array{fields:list, comment:string, returns:array, definition:string, language:string} + * @return Routine + * @phpstan-type Routine array{fields:list, comment:string, returns:array, definition:string, language:string} */ function routine($name, $type) { global $driver; @@ -1046,7 +1052,7 @@ if (!defined('Adminer\DRIVER')) { /** Get routine signature * @param string - * @param array result of routine() + * @param Routine result of routine() * @return string */ function routine_id($name, $row) { @@ -1071,7 +1077,7 @@ if (!defined('Adminer\DRIVER')) { } /** Get approximate number of rows - * @param array + * @param TableStatus * @param list * @return int or null if approximate number can't be retrieved */ @@ -1178,7 +1184,7 @@ if (!defined('Adminer\DRIVER')) { } /** Convert field in select and edit - * @param array one element from fields() + * @param Field one element from fields() * @return string */ function convert_field($field) { @@ -1194,7 +1200,7 @@ if (!defined('Adminer\DRIVER')) { } /** Convert value in edit after applying functions back - * @param array one element from fields() + * @param Field one element from fields() * @param string SQL expression * @return string */ diff --git a/adminer/drivers/sqlite.inc.php b/adminer/drivers/sqlite.inc.php index 756644ac..98df1641 100644 --- a/adminer/drivers/sqlite.inc.php +++ b/adminer/drivers/sqlite.inc.php @@ -471,7 +471,7 @@ if (isset($_GET["sqlite"])) { * @param string[] [$original => idf_escape($new_column)], empty to preserve * @param string [format_foreign_key()], empty to preserve * @param int set auto_increment to this value, 0 to preserve - * @param array[] [[$type, $name, $columns]], empty to preserve + * @param list|'DROP'}> [[$type, $name, $columns]], empty to preserve * @param string CHECK constraint to drop * @param string CHECK constraint to add * @return bool diff --git a/adminer/include/adminer.inc.php b/adminer/include/adminer.inc.php index b3745f62..d79a5002 100644 --- a/adminer/include/adminer.inc.php +++ b/adminer/include/adminer.inc.php @@ -159,7 +159,7 @@ class Adminer { } /** Table caption used in navigation and headings - * @param array result of SHOW TABLE STATUS + * @param TableStatus result of table_status1() * @return string HTML code, "" to ignore table */ function tableName($tableStatus) { @@ -167,7 +167,7 @@ class Adminer { } /** Field caption used in select and edit - * @param array single field returned from fields() + * @param Field single field returned from fields() * @param int order of column in select * @return string HTML code, "" to ignore field */ @@ -178,7 +178,7 @@ class Adminer { } /** Print links after select heading - * @param array result of SHOW TABLE STATUS + * @param TableStatus result of table_status1() * @param string new item options, NULL for no new item * @return null */ @@ -211,7 +211,7 @@ class Adminer { /** Get foreign keys for table * @param string - * @return array[] same format as foreign_keys() + * @return ForeignKey[] same format as foreign_keys() */ function foreignKeys($table) { return foreign_keys($table); @@ -220,14 +220,15 @@ class Adminer { /** Find backward keys for table * @param string * @param string - * @return array{keys:string[][], name:string}[] + * @return BackwardKey[] + * @phpstan-type BackwardKey array{name:string, keys:string[][]} */ function backwardKeys($table, $tableName) { return array(); } /** Print backward keys for row - * @param array[] result of $this->backwardKeys() + * @param BackwardKey[] result of $this->backwardKeys() * @param string[] * @return null */ @@ -278,7 +279,7 @@ class Adminer { /** Get descriptions of selected data * @param list all data to print - * @param array[] + * @param ForeignKey[] * @return list */ function rowDescriptions($rows, $foreignKeys) { @@ -287,7 +288,7 @@ class Adminer { /** Get a link to use in select table * @param string raw value of the field - * @param array single field returned from fields() + * @param Field single field returned from fields() * @return string or null to create the default link */ function selectLink($val, $field) { @@ -296,7 +297,7 @@ class Adminer { /** Value printed in select table * @param string HTML-escaped value to print * @param string link to foreign key - * @param array single field returned from fields() + * @param Field single field returned from fields() * @param string original value before applying editVal() and escaping * @return string */ @@ -314,7 +315,7 @@ class Adminer { /** Value conversion used in select and edit * @param string - * @param array single field returned from fields() + * @param Field single field returned from fields() * @return string */ function editVal($val, $field) { @@ -322,8 +323,8 @@ class Adminer { } /** Print table structure in tabular format - * @param array[] data about individual fields - * @param array + * @param Field[] data about individual fields + * @param TableStatus * @return null */ function tableStructurePrint($fields, $tableStatus = null) { @@ -354,7 +355,7 @@ class Adminer { } /** Print list of indexes on table in tabular format - * @param array[] data about all indexes on a table + * @param Index[] data about all indexes on a table * @return null */ function tableIndexesPrint($indexes) { @@ -403,7 +404,7 @@ class Adminer { /** Print search box in select * @param list result of selectSearchProcess() * @param string[] selectable columns - * @param array[] + * @param Index[] * @return null */ function selectSearchPrint($where, $columns, $indexes) { @@ -439,7 +440,7 @@ class Adminer { /** Print order box in select * @param list result of selectOrderProcess() * @param string[] selectable columns - * @param array[] + * @param Index[] * @return null */ function selectOrderPrint($order, $columns, $indexes) { @@ -481,7 +482,7 @@ class Adminer { } /** Print action box in select - * @param array[] + * @param Index[] * @return null */ function selectActionPrint($indexes) { @@ -531,7 +532,7 @@ class Adminer { /** Process columns box in select * @param string[] selectable columns - * @param array[] + * @param Index[] * @return list> [[select_expressions], [group_expressions]] */ function selectColumnsProcess($columns, $indexes) { @@ -550,8 +551,8 @@ class Adminer { } /** Process search box in select - * @param array[] - * @param array[] + * @param Field[] + * @param Index[] * @return list expressions to join by AND */ function selectSearchProcess($fields, $indexes) { @@ -604,8 +605,8 @@ class Adminer { } /** Process order box in select - * @param array[] - * @param array[] + * @param Field[] + * @param Index[] * @return list expressions to join by comma */ function selectOrderProcess($fields, $indexes) { @@ -636,7 +637,7 @@ class Adminer { /** Process extras in select form * @param string[] AND conditions - * @param array[] + * @param ForeignKey[] * @return bool true if processed, false to process other parts of form */ function selectEmailProcess($where, $foreignKeys) { @@ -698,7 +699,7 @@ class Adminer { } /** Functions displayed in edit form - * @param array single field from fields() + * @param Field single field from fields() * @return list */ function editFunctions($field) { @@ -725,7 +726,7 @@ class Adminer { /** Get options to display edit field * @param string table name - * @param array single field from fields() + * @param Field single field from fields() * @param string attributes to use inside the tag * @param string * @return string custom input field or empty string for default @@ -742,7 +743,7 @@ class Adminer { /** Get hint for edit field * @param string table name - * @param array single field from fields() + * @param Field single field from fields() * @param string * @return string */ @@ -751,7 +752,7 @@ class Adminer { } /** Process sent input - * @param array single field from fields() + * @param Field single field from fields() * @param string * @param string * @return string expression to use in a query @@ -1051,7 +1052,7 @@ class Adminer { } /** Set up syntax highlight for code and