From 51f8dc59263f57960ddf8492ac46afb00be47ff2 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 30 Apr 2025 22:32:57 +0100 Subject: [PATCH] Use RoutineType in getQueryFromRequest Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 10 ++++++++-- psalm-baseline.xml | 5 ++--- src/Database/Routines.php | 20 ++++++++------------ 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 71072e8803..e63998b053 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -5337,7 +5337,7 @@ parameters: - message: '#^Parameter \#1 \$string of function htmlspecialchars expects string, mixed given\.$#' identifier: argument.type - count: 2 + count: 1 path: src/Database/Routines.php - @@ -5358,6 +5358,12 @@ parameters: count: 4 path: src/Database/Routines.php + - + message: '#^Parameter \#1 \$value of static method PhpMyAdmin\\Database\\RoutineType\:\:tryFrom\(\) expects int\|string, mixed given\.$#' + identifier: argument.type + count: 1 + path: src/Database/Routines.php + - message: '#^Parameter \#2 \$array of function implode expects array\|null, list\\|string given\.$#' identifier: argument.type @@ -5413,7 +5419,7 @@ parameters: path: src/Database/Routines.php - - message: '#^Parameter \#7 \$itemType of method PhpMyAdmin\\Database\\Routines\:\:processParamsAndBuild\(\) expects string, mixed given\.$#' + message: '#^Parameter \#7 \$itemType of method PhpMyAdmin\\Database\\Routines\:\:processParamsAndBuild\(\) expects PhpMyAdmin\\Database\\RoutineType, PhpMyAdmin\\Database\\RoutineType\|null given\.$#' identifier: argument.type count: 1 path: src/Database/Routines.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index a1a023af00..4ec0453411 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -3579,14 +3579,13 @@ + - - @@ -3603,7 +3602,6 @@ - @@ -3615,6 +3613,7 @@ + type->parameters]]> diff --git a/src/Database/Routines.php b/src/Database/Routines.php index 79adc82509..efaa56827b 100644 --- a/src/Database/Routines.php +++ b/src/Database/Routines.php @@ -627,7 +627,6 @@ class Routines * @param mixed[] $itemParamLength A length or not for the parameter * @param mixed[] $itemParamOpsText An optional charset for the parameter * @param mixed[] $itemParamOpsNum An optional parameter for a $itemParamType NUMBER - * @param string $itemType The item type (PROCEDURE/FUNCTION) * @param bool $warnedAboutLength A boolean that will be switched if a the length warning is given */ private function processParamsAndBuild( @@ -637,7 +636,7 @@ class Routines array $itemParamLength, array $itemParamOpsText, array $itemParamOpsNum, - string $itemType, + RoutineType $itemType, bool &$warnedAboutLength, ): string { $params = ''; @@ -650,14 +649,14 @@ class Routines } if ( - $itemType === 'PROCEDURE' + $itemType === RoutineType::Procedure && ! empty($itemParamDir[$i]) && in_array($itemParamDir[$i], $this->directions, true) ) { $params .= $itemParamDir[$i] . ' ' . Util::backquote($itemParamName[$i]) . ' ' . $itemParamType[$i]; - } elseif ($itemType === 'FUNCTION') { + } elseif ($itemType === RoutineType::Function) { $params .= Util::backquote($itemParamName[$i]) . ' ' . $itemParamType[$i]; } elseif (! $warnedAboutDir) { @@ -777,7 +776,7 @@ class Routines */ public function getQueryFromRequest(): string { - $itemType = $_POST['item_type'] ?? ''; + $itemType = RoutineType::tryFrom($_POST['item_type'] ?? ''); $itemDefiner = $_POST['item_definer'] ?? ''; $itemName = $_POST['item_name'] ?? ''; @@ -804,13 +803,10 @@ class Routines } } - if ($itemType === 'FUNCTION' || $itemType === 'PROCEDURE') { - $query .= $itemType . ' '; + if ($itemType !== null) { + $query .= $itemType->value . ' '; } else { - $this->errors[] = sprintf( - __('Invalid routine type: "%s"'), - htmlspecialchars($itemType), - ); + $this->errors[] = __('Invalid routine type!'); } if (! empty($itemName)) { @@ -850,7 +846,7 @@ class Routines } $query .= '(' . $params . ') '; - if ($itemType === 'FUNCTION') { + if ($itemType === RoutineType::Function) { $query = $this->processFunctionSpecificParameters($query, $warnedAboutLength); }