From 547686be35636c7185af093f5749d7fdfda1e87f Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 24 Jan 2024 14:07:40 +0100 Subject: [PATCH 1/3] Add type hint to Routines::getDetails Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 62 ++----------------- psalm-baseline.xml | 26 -------- .../Database/RoutinesController.php | 2 +- src/Database/Routines.php | 7 ++- 4 files changed, 11 insertions(+), 86 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index b4b7eec579..98311c6fee 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1917,7 +1917,7 @@ parameters: - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" - count: 24 + count: 23 path: src/Controllers/Database/RoutinesController.php - @@ -1935,11 +1935,6 @@ parameters: count: 1 path: src/Controllers/Database/RoutinesController.php - - - message: "#^Parameter \\#1 \\$routine of method PhpMyAdmin\\\\Database\\\\Routines\\:\\:getRow\\(\\) expects array, mixed given\\.$#" - count: 2 - path: src/Controllers/Database/RoutinesController.php - - message: "#^Parameter \\#1 \\$string of function htmlentities expects string, mixed given\\.$#" count: 3 @@ -6995,31 +6990,6 @@ parameters: count: 9 path: src/Database/Routines.php - - - message: "#^Cannot access offset 'DTD_IDENTIFIER' on mixed\\.$#" - count: 1 - path: src/Database/Routines.php - - - - message: "#^Cannot access offset 'Db' on mixed\\.$#" - count: 1 - path: src/Database/Routines.php - - - - message: "#^Cannot access offset 'Definer' on mixed\\.$#" - count: 1 - path: src/Database/Routines.php - - - - message: "#^Cannot access offset 'Name' on mixed\\.$#" - count: 1 - path: src/Database/Routines.php - - - - message: "#^Cannot access offset 'Type' on mixed\\.$#" - count: 1 - path: src/Database/Routines.php - - message: "#^Cannot access offset 'value' on mixed\\.$#" count: 1 @@ -7132,7 +7102,7 @@ parameters: - message: "#^Parameter \\#1 \\$identifier of static method PhpMyAdmin\\\\Util\\:\\:backquote\\(\\) expects string\\|Stringable\\|null, mixed given\\.$#" - count: 10 + count: 9 path: src/Database/Routines.php - @@ -7157,7 +7127,7 @@ parameters: - message: "#^Parameter \\#1 \\$str of method PhpMyAdmin\\\\DatabaseInterface\\:\\:quoteString\\(\\) expects string, mixed given\\.$#" - count: 5 + count: 3 path: src/Database/Routines.php - @@ -7220,19 +7190,14 @@ parameters: count: 1 path: src/Database/Routines.php - - - message: "#^Parameter \\#2 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#" - count: 1 - path: src/Database/Routines.php - - message: "#^Parameter \\#3 \\$name of static method PhpMyAdmin\\\\Database\\\\Routines\\:\\:getFunctionDefinition\\(\\) expects string, mixed given\\.$#" - count: 3 + count: 2 path: src/Database/Routines.php - message: "#^Parameter \\#3 \\$name of static method PhpMyAdmin\\\\Database\\\\Routines\\:\\:getProcedureDefinition\\(\\) expects string, mixed given\\.$#" - count: 3 + count: 2 path: src/Database/Routines.php - @@ -13950,21 +13915,11 @@ parameters: count: 1 path: src/Server/Privileges.php - - - message: "#^Cannot access offset 'name' on mixed\\.$#" - count: 3 - path: src/Server/Privileges.php - - message: "#^Cannot access offset 'privs' on mixed\\.$#" count: 1 path: src/Server/Privileges.php - - - message: "#^Cannot access offset 'type' on mixed\\.$#" - count: 1 - path: src/Server/Privileges.php - - message: "#^Cannot access offset 0 on mixed\\.$#" count: 2 @@ -14010,11 +13965,6 @@ parameters: count: 1 path: src/Server/Privileges.php - - - message: "#^Method PhpMyAdmin\\\\Server\\\\Privileges\\:\\:getRoutineType\\(\\) should return string but returns mixed\\.$#" - count: 1 - path: src/Server/Privileges.php - - message: "#^Offset '@@old_passwords' does not exist on array\\|null\\.$#" count: 1 @@ -14137,7 +14087,7 @@ parameters: - message: "#^Parameter \\#1 \\$string of function mb_strtolower expects string, mixed given\\.$#" - count: 2 + count: 1 path: src/Server/Privileges.php - diff --git a/psalm-baseline.xml b/psalm-baseline.xml index eb2a12f1c1..09c6bda5b0 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1149,8 +1149,6 @@ - $item - $routine @@ -1185,8 +1183,6 @@ - $item - $routine @@ -4876,12 +4872,6 @@ - - - - - - $value $value $value @@ -4900,11 +4890,6 @@ $priv[5] $priv[6] $priv[7] - - - - - @@ -4966,7 +4951,6 @@ - $routine $string $value $value @@ -10607,7 +10591,6 @@ $passwordSetReal $privilege $realSqlQuery - $row $row @@ -10643,10 +10626,6 @@ - - - - @@ -10698,9 +10677,6 @@ $queries[] $queries[] $queries[] - $routine - $routine - $routines[] $row @@ -10714,7 +10690,6 @@ $value - string string|null @@ -10726,7 +10701,6 @@ $sqlQuery - diff --git a/src/Controllers/Database/RoutinesController.php b/src/Controllers/Database/RoutinesController.php index f67927ec16..94e9965647 100644 --- a/src/Controllers/Database/RoutinesController.php +++ b/src/Controllers/Database/RoutinesController.php @@ -145,7 +145,7 @@ class RoutinesController extends AbstractController 'new_row', $this->template->render('database/routines/row', $this->routines->getRow($routine)), ); - $this->response->addJSON('insert', ! empty($routine)); + $this->response->addJSON('insert', true); $this->response->addJSON('message', $output); $this->response->addJSON('tableType', 'routines'); diff --git a/src/Database/Routines.php b/src/Database/Routines.php index b7b1f91dc9..7f26e642dd 100644 --- a/src/Database/Routines.php +++ b/src/Database/Routines.php @@ -1107,8 +1107,8 @@ class Routines /** * Creates the contents for a row in the list of routines * - * @param mixed[] $routine An array of routine data - * @param string $rowClass Additional class + * @param array{db:string, name:string, type:string, definer:string, returns:string} $routine + * @param string $rowClass Additional class * * @return mixed[] */ @@ -1223,7 +1223,7 @@ class Routines * @param string|null $which PROCEDURE | FUNCTION or null for both * @param string $name name of the routine (to fetch a specific routine) * - * @return mixed[] information about PROCEDUREs or FUNCTIONs + * @return array{db:string, name:string, type:string, definer:string, returns:string}[] */ public static function getDetails( DatabaseInterface $dbi, @@ -1261,6 +1261,7 @@ class Routines } $ret = []; + /** @var array{Db:string, Name:string, Type:string, Definer:string, DTD_IDENTIFIER:string|null} $routine */ foreach ($routines as $routine) { $ret[] = [ 'db' => $routine['Db'], From 0b3ec77f8765049d7a947dcbb482448b3dd1ff80 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 24 Jan 2024 14:22:55 +0100 Subject: [PATCH 2/3] Implement proper value object Signed-off-by: Kamil Tekiela --- psalm-baseline.xml | 7 +++ .../templates/database/routines/index.twig | 8 ++-- .../Database/RoutinesController.php | 2 +- src/Database/Routine.php | 17 ++++++++ src/Database/Routines.php | 43 +++++++++---------- src/Server/Privileges.php | 8 ++-- 6 files changed, 54 insertions(+), 31 deletions(-) create mode 100644 src/Database/Routine.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 09c6bda5b0..25d570f245 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -4830,6 +4830,13 @@ DatabaseInterface::getInstance() + + + $db + $definer + $returns + + Config::getInstance() diff --git a/resources/templates/database/routines/index.twig b/resources/templates/database/routines/index.twig index 1af4e8fa44..927bd75917 100644 --- a/resources/templates/database/routines/index.twig +++ b/resources/templates/database/routines/index.twig @@ -5,7 +5,7 @@
- {%- if items is not empty %} + {%- if has_any_routines %}
@@ -31,7 +31,7 @@
{%- endif %} - + {{ get_icon('b_routine_add', 'Create new routine'|trans) }} @@ -41,11 +41,11 @@
{{ get_hidden_inputs(db, table) }} -
+
{{ 'There are no routines to display.'|trans|notice }}
- +
diff --git a/src/Controllers/Database/RoutinesController.php b/src/Controllers/Database/RoutinesController.php index 94e9965647..820f2b44a8 100644 --- a/src/Controllers/Database/RoutinesController.php +++ b/src/Controllers/Database/RoutinesController.php @@ -484,7 +484,7 @@ class RoutinesController extends AbstractController $this->render('database/routines/index', [ 'db' => Current::$database, 'table' => Current::$table, - 'items' => $items, + 'has_any_routines' => $items !== [], 'rows' => $rows, 'has_privilege' => Util::currentUserHasPrivilege('CREATE ROUTINE', Current::$database, Current::$table), ]); diff --git a/src/Database/Routine.php b/src/Database/Routine.php new file mode 100644 index 0000000000..d9f2d39234 --- /dev/null +++ b/src/Database/Routine.php @@ -0,0 +1,17 @@ +type, + Util::backquote($routine->name), ); // this is for our purpose to decide whether to // show the edit link or not, so we need the DEFINER for the routine $where = 'ROUTINE_SCHEMA ' . Util::getCollateForIS() . '=' . $this->dbi->quoteString(Current::$database) - . ' AND SPECIFIC_NAME=' . $this->dbi->quoteString($routine['name']) - . ' AND ROUTINE_TYPE=' . $this->dbi->quoteString($routine['type']); + . ' AND SPECIFIC_NAME=' . $this->dbi->quoteString($routine->name) + . ' AND ROUTINE_TYPE=' . $this->dbi->quoteString($routine->type); $query = 'SELECT `DEFINER` FROM INFORMATION_SCHEMA.ROUTINES WHERE ' . $where . ';'; $routineDefiner = $this->dbi->fetchValue($query); @@ -1152,10 +1151,10 @@ class Routines // we will show a dialog to get values for these parameters, // otherwise we can execute it directly. - if ($routine['type'] === 'FUNCTION') { - $definition = self::getFunctionDefinition($this->dbi, Current::$database, $routine['name']); + if ($routine->type === 'FUNCTION') { + $definition = self::getFunctionDefinition($this->dbi, Current::$database, $routine->name); } else { - $definition = self::getProcedureDefinition($this->dbi, Current::$database, $routine['name']); + $definition = self::getProcedureDefinition($this->dbi, Current::$database, $routine->name); } $executeAction = ''; @@ -1166,12 +1165,12 @@ class Routines /** @var CreateStatement $stmt */ $stmt = $parser->statements[0]; - $params = Routine::getParameters($stmt); + $params = RoutineUtils::getParameters($stmt); if ($hasExecutePrivilege) { $executeAction = 'execute_routine'; for ($i = 0; $i < $params['num']; $i++) { - if ($routine['type'] === 'PROCEDURE' && $params['dir'][$i] === 'OUT') { + if ($routine->type === 'PROCEDURE' && $params['dir'][$i] === 'OUT') { continue; } @@ -1223,7 +1222,7 @@ class Routines * @param string|null $which PROCEDURE | FUNCTION or null for both * @param string $name name of the routine (to fetch a specific routine) * - * @return array{db:string, name:string, type:string, definer:string, returns:string}[] + * @return Routine[] */ public static function getDetails( DatabaseInterface $dbi, @@ -1263,13 +1262,13 @@ class Routines $ret = []; /** @var array{Db:string, Name:string, Type:string, Definer:string, DTD_IDENTIFIER:string|null} $routine */ foreach ($routines as $routine) { - $ret[] = [ - 'db' => $routine['Db'], - 'name' => $routine['Name'], - 'type' => $routine['Type'], - 'definer' => $routine['Definer'], - 'returns' => $routine['DTD_IDENTIFIER'] ?? '', - ]; + $ret[] = new Routine( + $routine['Db'], + $routine['Name'], + $routine['Type'], + $routine['Definer'], + $routine['DTD_IDENTIFIER'] ?? '', + ); } // Sort results by name diff --git a/src/Server/Privileges.php b/src/Server/Privileges.php index 371084a4ea..38cbb16035 100644 --- a/src/Server/Privileges.php +++ b/src/Server/Privileges.php @@ -1727,11 +1727,11 @@ class Privileges $routines = []; foreach ($routineData as $routine) { - if (in_array($routine['name'], $foundRows, true)) { + if (in_array($routine->name, $foundRows, true)) { continue; } - $routines[] = $routine['name']; + $routines[] = $routine->name; } $data['routines'] = $routines; @@ -3224,8 +3224,8 @@ class Privileges $routineName = mb_strtolower($routineName); foreach ($routineData as $routine) { - if (mb_strtolower($routine['name']) === $routineName) { - return $routine['type']; + if (mb_strtolower($routine->name) === $routineName) { + return $routine->type; } } From bd79b279256f09333192243cd0b925612c44947f Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 24 Jan 2024 15:06:05 +0100 Subject: [PATCH 3/3] Remove unused properties Signed-off-by: Kamil Tekiela --- psalm-baseline.xml | 2 -- src/Database/Routine.php | 2 -- src/Database/Routines.php | 10 ++-------- 3 files changed, 2 insertions(+), 12 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 25d570f245..88f01a870e 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -4832,8 +4832,6 @@ - $db - $definer $returns diff --git a/src/Database/Routine.php b/src/Database/Routine.php index d9f2d39234..dc6370288f 100644 --- a/src/Database/Routine.php +++ b/src/Database/Routine.php @@ -7,10 +7,8 @@ namespace PhpMyAdmin\Database; final class Routine { public function __construct( - public readonly string $db, public readonly string $name, public readonly string $type, - public readonly string $definer, public readonly string $returns, ) { } diff --git a/src/Database/Routines.php b/src/Database/Routines.php index ca2b4ec410..9accfe3f46 100644 --- a/src/Database/Routines.php +++ b/src/Database/Routines.php @@ -1260,15 +1260,9 @@ class Routines } $ret = []; - /** @var array{Db:string, Name:string, Type:string, Definer:string, DTD_IDENTIFIER:string|null} $routine */ + /** @var array{Name:string, Type:string, DTD_IDENTIFIER:string|null} $routine */ foreach ($routines as $routine) { - $ret[] = new Routine( - $routine['Db'], - $routine['Name'], - $routine['Type'], - $routine['Definer'], - $routine['DTD_IDENTIFIER'] ?? '', - ); + $ret[] = new Routine($routine['Name'], $routine['Type'], $routine['DTD_IDENTIFIER'] ?? ''); } // Sort results by name