Skip to content

Commit bb6b17a

Browse files
Merge pull request #18947 from kamil-tekiela/Routines-refactoring
Routines refactoring
2 parents 2a8ecaf + 38a5b48 commit bb6b17a

3 files changed

Lines changed: 46 additions & 99 deletions

File tree

phpstan-baseline.neon

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6750,36 +6750,6 @@ parameters:
67506750
count: 1
67516751
path: src/Database/Routines.php
67526752

6753-
-
6754-
message: "#^Cannot access offset 0 on mixed\\.$#"
6755-
count: 1
6756-
path: src/Database/Routines.php
6757-
6758-
-
6759-
message: "#^Cannot access offset 1 on mixed\\.$#"
6760-
count: 1
6761-
path: src/Database/Routines.php
6762-
6763-
-
6764-
message: "#^Cannot access offset 2 on mixed\\.$#"
6765-
count: 1
6766-
path: src/Database/Routines.php
6767-
6768-
-
6769-
message: "#^Cannot access offset 5 on mixed\\.$#"
6770-
count: 1
6771-
path: src/Database/Routines.php
6772-
6773-
-
6774-
message: "#^Cannot access offset 6 on mixed\\.$#"
6775-
count: 1
6776-
path: src/Database/Routines.php
6777-
6778-
-
6779-
message: "#^Cannot access offset 7 on mixed\\.$#"
6780-
count: 1
6781-
path: src/Database/Routines.php
6782-
67836753
-
67846754
message: "#^Cannot access offset int\\<0, max\\> on array\\<int, array\\|string\\|null\\>\\|int\\.$#"
67856755
count: 1
@@ -6835,6 +6805,11 @@ parameters:
68356805
count: 22
68366806
path: src/Database/Routines.php
68376807

6808+
-
6809+
message: "#^Method PhpMyAdmin\\\\Database\\\\Routines\\:\\:backupPrivileges\\(\\) should return array\\<array\\<string\\>\\> but returns array\\.$#"
6810+
count: 1
6811+
path: src/Database/Routines.php
6812+
68386813
-
68396814
message: "#^Only booleans are allowed in &&, int\\|false given on the right side\\.$#"
68406815
count: 2
@@ -6887,7 +6862,7 @@ parameters:
68876862

68886863
-
68896864
message: "#^Parameter \\#1 \\$str of method PhpMyAdmin\\\\DatabaseInterface\\:\\:quoteString\\(\\) expects string, mixed given\\.$#"
6890-
count: 3
6865+
count: 7
68916866
path: src/Database/Routines.php
68926867

68936868
-
@@ -6897,7 +6872,7 @@ parameters:
68976872

68986873
-
68996874
message: "#^Parameter \\#1 \\$string of function htmlspecialchars expects string, mixed given\\.$#"
6900-
count: 3
6875+
count: 2
69016876
path: src/Database/Routines.php
69026877

69036878
-
@@ -6945,11 +6920,6 @@ parameters:
69456920
count: 1
69466921
path: src/Database/Routines.php
69476922

6948-
-
6949-
message: "#^Parameter \\#2 \\.\\.\\.\\$arrays of function array_merge expects array, mixed given\\.$#"
6950-
count: 1
6951-
path: src/Database/Routines.php
6952-
69536923
-
69546924
message: "#^Parameter \\#3 \\$name of static method PhpMyAdmin\\\\Database\\\\Routines\\:\\:getFunctionDefinition\\(\\) expects string, mixed given\\.$#"
69556925
count: 2

psalm-baseline.xml

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5025,7 +5025,6 @@
50255025
<code>$itemParamType[$i]</code>
50265026
<code>$itemParamType[$i]</code>
50275027
<code>$itemParamType[$i]</code>
5028-
<code>$newErrors</code>
50295028
<code><![CDATA[$routine['SPECIFIC_NAME']]]></code>
50305029
<code><![CDATA[$routine['SPECIFIC_NAME']]]></code>
50315030
<code><![CDATA[$routine['item_name']]]></code>
@@ -5044,7 +5043,6 @@
50445043
<code>$value</code>
50455044
<code>$value</code>
50465045
<code>$value</code>
5047-
<code>$value</code>
50485046
</MixedArgument>
50495047
<MixedArgumentTypeCoercion>
50505048
<code>$options</code>
@@ -5053,12 +5051,6 @@
50535051
</MixedArgumentTypeCoercion>
50545052
<MixedArrayAccess>
50555053
<code><![CDATA[$opt['value']]]></code>
5056-
<code>$priv[0]</code>
5057-
<code>$priv[1]</code>
5058-
<code>$priv[2]</code>
5059-
<code>$priv[5]</code>
5060-
<code>$priv[6]</code>
5061-
<code>$priv[7]</code>
50625054
<code><![CDATA[$routine['item_param_dir'][$i]]]></code>
50635055
<code><![CDATA[$routine['item_param_dir'][$i]]]></code>
50645056
<code><![CDATA[$routine['item_param_length_arr'][$i]]]></code>
@@ -5114,7 +5106,6 @@
51145106
<code>$i</code>
51155107
<code>$opt</code>
51165108
<code>$options[]</code>
5117-
<code>$priv</code>
51185109
<code><![CDATA[$retval['item_comment']]]></code>
51195110
<code><![CDATA[$retval['item_definer']]]></code>
51205111
<code><![CDATA[$retval['item_name']]]></code>
@@ -5124,32 +5115,33 @@
51245115
<code>$value</code>
51255116
<code>$value</code>
51265117
<code>$value</code>
5127-
<code>$value</code>
51285118
</MixedAssignment>
51295119
<MixedOperand>
51305120
<code>$itemParamLength[$i]</code>
51315121
<code>$itemParamType[$i]</code>
51325122
<code>$itemParamType[$i]</code>
5133-
<code>$priv[0]</code>
5134-
<code>$priv[1]</code>
5135-
<code>$priv[2]</code>
5136-
<code>$priv[5]</code>
5137-
<code>$priv[6]</code>
5138-
<code>$priv[7]</code>
51395123
<code>$string</code>
51405124
</MixedOperand>
5125+
<MixedReturnTypeCoercion>
5126+
<code><![CDATA[$this->dbi->fetchResult($privilegesBackupQuery, 0)]]></code>
5127+
<code>string[][]</code>
5128+
</MixedReturnTypeCoercion>
51415129
<PossiblyInvalidArgument>
51425130
<code><![CDATA[$_POST['funcs'][$routine['item_param_name'][$i]]]]></code>
51435131
<code><![CDATA[$_POST['item_comment']]]></code>
51445132
<code><![CDATA[$_POST['item_isdeterministic']]]></code>
51455133
<code><![CDATA[$_POST['item_name']]]></code>
51465134
<code><![CDATA[$_POST['item_name']]]></code>
5135+
<code><![CDATA[$_POST['item_name']]]></code>
51475136
<code><![CDATA[$_POST['item_original_name']]]></code>
51485137
<code><![CDATA[$_POST['item_original_name']]]></code>
51495138
<code><![CDATA[$_POST['item_original_name']]]></code>
5139+
<code><![CDATA[$_POST['item_original_name']]]></code>
5140+
<code><![CDATA[$_POST['item_original_type']]]></code>
51505141
<code><![CDATA[$_POST['item_original_type']]]></code>
51515142
<code><![CDATA[$_POST['item_returnopts_num']]]></code>
51525143
<code><![CDATA[$_POST['item_returnopts_text']]]></code>
5144+
<code><![CDATA[$_POST['item_type']]]></code>
51535145
<code>$itemDefiner</code>
51545146
<code>$itemName</code>
51555147
<code>$itemReturnType</code>
@@ -5165,10 +5157,14 @@
51655157
<PossiblyInvalidCast>
51665158
<code><![CDATA[$_POST['item_comment']]]></code>
51675159
<code><![CDATA[$_POST['item_isdeterministic']]]></code>
5160+
<code><![CDATA[$_POST['item_name']]]></code>
51685161
<code><![CDATA[$_POST['item_original_name']]]></code>
51695162
<code><![CDATA[$_POST['item_original_name']]]></code>
5163+
<code><![CDATA[$_POST['item_original_name']]]></code>
5164+
<code><![CDATA[$_POST['item_original_type']]]></code>
51705165
<code><![CDATA[$_POST['item_returnopts_num']]]></code>
51715166
<code><![CDATA[$_POST['item_returnopts_text']]]></code>
5167+
<code><![CDATA[$_POST['item_type']]]></code>
51725168
<code>$itemReturnType</code>
51735169
<code>$itemReturnType</code>
51745170
<code>$itemReturnType</code>
@@ -5179,15 +5175,12 @@
51795175
<code><![CDATA[$retval['item_param_dir']]]></code>
51805176
</PossiblyInvalidIterator>
51815177
<PossiblyInvalidOperand>
5182-
<code><![CDATA[$_POST['item_name']]]></code>
5183-
<code><![CDATA[$_POST['item_original_name']]]></code>
5184-
<code><![CDATA[$_POST['item_original_type']]]></code>
51855178
<code><![CDATA[$_POST['item_original_type']]]></code>
51865179
<code><![CDATA[$_POST['item_returnlength']]]></code>
5187-
<code><![CDATA[$_POST['item_type']]]></code>
51885180
<code>$itemDefinition</code>
51895181
</PossiblyInvalidOperand>
51905182
<PossiblyNullArgument>
5183+
<code><![CDATA[$GLOBALS['message']]]></code>
51915184
<code>$createRoutine</code>
51925185
<code><![CDATA[$stmt->return->parameters]]></code>
51935186
</PossiblyNullArgument>
@@ -5197,8 +5190,6 @@
51975190
</PossiblyNullIterator>
51985191
<PossiblyNullOperand>
51995192
<code><![CDATA[$_POST['item_original_type']]]></code>
5200-
<code>$output</code>
5201-
<code><![CDATA[$this->browseRow($row)]]></code>
52025193
</PossiblyNullOperand>
52035194
<PossiblyNullPropertyFetch>
52045195
<code><![CDATA[$stmt->return->name]]></code>

src/Database/Routines.php

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function handleRequestCreateOrEdit(string $db): string
160160
/**
161161
* Backup the privileges
162162
*
163-
* @return mixed[]
163+
* @return string[][]
164164
*/
165165
public function backupPrivileges(): array
166166
{
@@ -176,21 +176,20 @@ public function backupPrivileges(): array
176176

177177
$privilegesBackupQuery = 'SELECT * FROM ' . Util::backquote('mysql')
178178
. '.' . Util::backquote('procs_priv')
179-
. ' where Routine_name = "' . $_POST['item_original_name']
180-
. '" AND Routine_type = "' . $_POST['item_original_type']
181-
. '";';
179+
. ' WHERE Routine_name = ' . $this->dbi->quoteString($_POST['item_original_name'])
180+
. ' AND Routine_type = ' . $this->dbi->quoteString($_POST['item_original_type']);
182181

183182
return $this->dbi->fetchResult($privilegesBackupQuery, 0);
184183
}
185184

186185
/**
187186
* Create the routine
188187
*
189-
* @param string $routineQuery Query to create routine
190-
* @param string $createRoutine Query to restore routine
191-
* @param mixed[] $privilegesBackup Privileges backup
188+
* @param string $routineQuery Query to create routine
189+
* @param string $createRoutine Query to restore routine
190+
* @param string[][] $privilegesBackup Privileges backup
192191
*
193-
* @return mixed[]
192+
* @return array{string[], Message|null}
194193
*/
195194
public function create(
196195
string $routineQuery,
@@ -211,7 +210,15 @@ public function create(
211210
// Try to restore the backup query
212211
$result = $this->dbi->tryQuery($createRoutine);
213212
if (! $result) {
214-
$errors = $this->checkResult($createRoutine, $errors);
213+
// OMG, this is really bad! We dropped the query,
214+
// failed to create a new one
215+
// and now even the backup query does not execute!
216+
// This should not happen, but we better handle
217+
// this just in case.
218+
$errors[] = __('Sorry, we failed to restore the dropped routine.') . '<br>'
219+
. __('The backed up query was:')
220+
. '"' . htmlspecialchars($createRoutine) . '"<br>'
221+
. __('MySQL said: ') . $this->dbi->getError();
215222
}
216223

217224
return [$errors, null];
@@ -227,13 +234,13 @@ public function create(
227234
$adjustProcPrivilege = 'INSERT INTO '
228235
. Util::backquote('mysql') . '.'
229236
. Util::backquote('procs_priv')
230-
. ' VALUES("' . $priv[0] . '", "'
231-
. $priv[1] . '", "' . $priv[2] . '", "'
232-
. $_POST['item_name'] . '", "'
233-
. $_POST['item_type'] . '", "'
234-
. $priv[5] . '", "'
235-
. $priv[6] . '", "'
236-
. $priv[7] . '");';
237+
. ' VALUES(' . $this->dbi->quoteString($priv[0]) . ', '
238+
. $this->dbi->quoteString($priv[1]) . ', ' . $this->dbi->quoteString($priv[2]) . ', '
239+
. $this->dbi->quoteString($_POST['item_name']) . ', '
240+
. $this->dbi->quoteString($_POST['item_type']) . ', '
241+
. $this->dbi->quoteString($priv[5]) . ', '
242+
. $this->dbi->quoteString($priv[6]) . ', '
243+
. $this->dbi->quoteString($priv[7]) . ');';
237244
$this->dbi->query($adjustProcPrivilege);
238245
$resultAdjust = true;
239246
}
@@ -1010,11 +1017,11 @@ public function handleExecuteRoutine(array $routine): array
10101017
/**
10111018
* Browse row array
10121019
*
1013-
* @param mixed[] $row Columns
1020+
* @param (string|null)[] $row Columns
10141021
*/
1015-
private function browseRow(array $row): string|null
1022+
private function browseRow(array $row): string
10161023
{
1017-
$output = null;
1024+
$output = '';
10181025
foreach ($row as $value) {
10191026
if ($value === null) {
10201027
$value = '<i>NULL</i>';
@@ -1195,27 +1202,6 @@ public function getRow(Routine $routine, string $rowClass = ''): array
11951202
];
11961203
}
11971204

1198-
/**
1199-
* @param string $createStatement Query
1200-
* @param mixed[] $errors Errors
1201-
*
1202-
* @return mixed[]
1203-
*/
1204-
private function checkResult(string $createStatement, array $errors): array
1205-
{
1206-
// OMG, this is really bad! We dropped the query,
1207-
// failed to create a new one
1208-
// and now even the backup query does not execute!
1209-
// This should not happen, but we better handle
1210-
// this just in case.
1211-
$errors[] = __('Sorry, we failed to restore the dropped routine.') . '<br>'
1212-
. __('The backed up query was:')
1213-
. '"' . htmlspecialchars($createStatement) . '"<br>'
1214-
. __('MySQL said: ') . $this->dbi->getError();
1215-
1216-
return $errors;
1217-
}
1218-
12191205
/**
12201206
* returns details about the PROCEDUREs or FUNCTIONs for a specific database
12211207
* or details about a specific routine

0 commit comments

Comments
 (0)