Merge pull request #18934 from kamil-tekiela/Refactor-Routines-getDetails

Refactor Routines::getDetails()
This commit is contained in:
Maurício Meneghini Fauth 2024-01-24 14:44:57 -03:00 committed by GitHub
commit f900fcee8b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 52 additions and 114 deletions

View File

@ -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
-

View File

@ -1149,8 +1149,6 @@
<code><![CDATA[$GLOBALS['errors']]]></code>
</InvalidArrayOffset>
<MixedArgument>
<code>$item</code>
<code>$routine</code>
<code><![CDATA[$routine['item_param_length'][$i]]]></code>
<code><![CDATA[$routine['item_param_name'][$i]]]></code>
</MixedArgument>
@ -1185,8 +1183,6 @@
<MixedAssignment>
<code><![CDATA[$GLOBALS['errorUrl']]]></code>
<code><![CDATA[$GLOBALS['errors']]]></code>
<code>$item</code>
<code>$routine</code>
<code><![CDATA[$routine['item_num_params']]]></code>
<code><![CDATA[$routine['item_num_params']]]></code>
<code><![CDATA[$routine['item_original_name']]]></code>
@ -4834,6 +4830,11 @@
<code>DatabaseInterface::getInstance()</code>
</DeprecatedMethod>
</file>
<file src="src/Database/Routine.php">
<PossiblyUnusedProperty>
<code>$returns</code>
</PossiblyUnusedProperty>
</file>
<file src="src/Database/Routines.php">
<DeprecatedMethod>
<code>Config::getInstance()</code>
@ -4876,12 +4877,6 @@
<code><![CDATA[$routine['item_param_type'][$i]]]></code>
<code><![CDATA[$routine['item_param_type'][$i]]]></code>
<code><![CDATA[$routine['item_param_type'][$i]]]></code>
<code><![CDATA[$routine['name']]]></code>
<code><![CDATA[$routine['name']]]></code>
<code><![CDATA[$routine['name']]]></code>
<code><![CDATA[$routine['name']]]></code>
<code><![CDATA[$routine['type']]]></code>
<code><![CDATA[$routine['type']]]></code>
<code>$value</code>
<code>$value</code>
<code>$value</code>
@ -4900,11 +4895,6 @@
<code>$priv[5]</code>
<code>$priv[6]</code>
<code>$priv[7]</code>
<code><![CDATA[$routine['DTD_IDENTIFIER']]]></code>
<code><![CDATA[$routine['Db']]]></code>
<code><![CDATA[$routine['Definer']]]></code>
<code><![CDATA[$routine['Name']]]></code>
<code><![CDATA[$routine['Type']]]></code>
<code><![CDATA[$routine['item_param_dir'][$i]]]></code>
<code><![CDATA[$routine['item_param_dir'][$i]]]></code>
<code><![CDATA[$routine['item_param_length_arr'][$i]]]></code>
@ -4966,7 +4956,6 @@
<code><![CDATA[$retval['item_name']]]></code>
<code><![CDATA[$retval['item_sqldataaccess']]]></code>
<code><![CDATA[$retval['item_type']]]></code>
<code>$routine</code>
<code>$string</code>
<code>$value</code>
<code>$value</code>
@ -10607,7 +10596,6 @@
<code>$passwordSetReal</code>
<code>$privilege</code>
<code>$realSqlQuery</code>
<code><![CDATA[$routine['name']]]></code>
<code>$row</code>
<code>$row</code>
<code><![CDATA[$row1['Type']]]></code>
@ -10643,10 +10631,6 @@
<code><![CDATA[$privilege['Host']]]></code>
<code><![CDATA[$privilege['User']]]></code>
<code><![CDATA[$privilege['User']]]></code>
<code><![CDATA[$routine['name']]]></code>
<code><![CDATA[$routine['name']]]></code>
<code><![CDATA[$routine['name']]]></code>
<code><![CDATA[$routine['type']]]></code>
<code><![CDATA[$row['Db']]]></code>
<code><![CDATA[$row['Grant_priv']]]></code>
<code><![CDATA[$row['Proc_priv']]]></code>
@ -10698,9 +10682,6 @@
<code>$queries[]</code>
<code>$queries[]</code>
<code>$queries[]</code>
<code>$routine</code>
<code>$routine</code>
<code>$routines[]</code>
<code>$row</code>
<code><![CDATA[$row['password']]]></code>
<code><![CDATA[$row['password']]]></code>
@ -10714,7 +10695,6 @@
<code>$value</code>
</MixedAssignment>
<MixedInferredReturnType>
<code>string</code>
<code>string|null</code>
</MixedInferredReturnType>
<MixedOperand>
@ -10726,7 +10706,6 @@
<code>$sqlQuery</code>
</MixedOperand>
<MixedReturnStatement>
<code><![CDATA[$routine['type']]]></code>
<code><![CDATA[$row['password']]]></code>
</MixedReturnStatement>
<MixedReturnTypeCoercion>

View File

@ -5,7 +5,7 @@
</h2>
<div class="d-flex flex-wrap my-3">
{%- if items is not empty %}
{%- if has_any_routines %}
<div>
<div class="input-group">
<div class="input-group-text">
@ -31,7 +31,7 @@
</div>
{%- endif %}
<div{{ items is not empty ? ' class="ms-2"' }}>
<div{{ has_any_routines ? ' class="ms-2"' }}>
<a class="ajax add_anchor btn btn-primary{{ not has_privilege ? ' disabled' }}" href="{{ url('/database/routines', {'db': db, 'table': table, 'add_item': true}) }}" role="button"{{ not has_privilege ? ' tabindex="-1" aria-disabled="true"' }}>
{{ get_icon('b_routine_add', 'Create new routine'|trans) }}
</a>
@ -41,11 +41,11 @@
<form id="rteListForm" class="ajax" action="{{ url('/database/routines') }}">
{{ get_hidden_inputs(db, table) }}
<div id="nothing2display"{{ items is not empty ? ' class="hide"' }}>
<div id="nothing2display"{{ has_any_routines ? ' class="hide"' }}>
{{ 'There are no routines to display.'|trans|notice }}
</div>
<table id="routinesTable" class="table table-striped table-hover{{ items is empty ? ' hide' }} data w-auto">
<table id="routinesTable" class="table table-striped table-hover{{ not has_any_routines ? ' hide' }} data w-auto">
<thead>
<tr>
<th></th>

View File

@ -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');
@ -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),
]);

15
src/Database/Routine.php Normal file
View File

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Database;
final class Routine
{
public function __construct(
public readonly string $name,
public readonly string $type,
public readonly string $returns,
) {
}
}

View File

@ -14,7 +14,7 @@ use PhpMyAdmin\Query\Generator as QueryGenerator;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\Utils\Routine;
use PhpMyAdmin\SqlParser\Utils\Routine as RoutineUtils;
use PhpMyAdmin\UserPrivileges;
use PhpMyAdmin\Util;
@ -451,7 +451,7 @@ class Routines
$body = (string) $routine['ROUTINE_DEFINITION'];
}
$params = Routine::getParameters($stmt);
$params = RoutineUtils::getParameters($stmt);
$retval['item_num_params'] = $params['num'];
$retval['item_param_dir'] = $params['dir'];
$retval['item_param_name'] = $params['name'];
@ -1107,24 +1107,23 @@ 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 string $rowClass Additional class
*
* @return mixed[]
*/
public function getRow(array $routine, string $rowClass = ''): array
public function getRow(Routine $routine, string $rowClass = ''): array
{
$sqlDrop = sprintf(
'DROP %s IF EXISTS %s',
$routine['type'],
Util::backquote($routine['name']),
$routine->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 mixed[] information about PROCEDUREs or FUNCTIONs
* @return Routine[]
*/
public static function getDetails(
DatabaseInterface $dbi,
@ -1261,14 +1260,9 @@ class Routines
}
$ret = [];
/** @var array{Name:string, Type: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['Name'], $routine['Type'], $routine['DTD_IDENTIFIER'] ?? '');
}
// Sort results by name

View File

@ -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;
}
}