Skip to content

Commit 1cf2d30

Browse files
committed
Refactor Plugins\Export\ExportXml::exportDefinitions method
Improve type inference of the parameters. Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent a8421ef commit 1cf2d30

File tree

5 files changed

+45
-67
lines changed

5 files changed

+45
-67
lines changed

libraries/classes/DatabaseInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ public function getWarnings($link = self::CONNECT_USER): array
14721472
* @param string $which PROCEDURE | FUNCTION
14731473
* @param int $link link type
14741474
*
1475-
* @return array the procedure names or function names
1475+
* @return string[] the procedure names or function names
14761476
*/
14771477
public function getProceduresOrFunctions(
14781478
string $db,

libraries/classes/Dbal/DbalInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ public function getWarnings($link = DatabaseInterface::CONNECT_USER): array;
436436
* @param string $which PROCEDURE | FUNCTION
437437
* @param int $link link type
438438
*
439-
* @return array the procedure names or function names
439+
* @return string[] the procedure names or function names
440440
*/
441441
public function getProceduresOrFunctions(
442442
string $db,

libraries/classes/Plugins/Export/ExportXml.php

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -148,51 +148,38 @@ protected function setProperties(): ExportPluginProperties
148148
}
149149

150150
/**
151-
* Generates output for SQL defintions of routines
151+
* Generates output for SQL definitions.
152152
*
153-
* @param string $db Database name
154-
* @param string $type Item type to be used in XML output
155-
* @param string $dbitype Item type used in DBI queries
153+
* @param string $db Database name
154+
* @param string $type Item type to be used in XML output
155+
* @param string[] $names Names of items to export
156+
* @psalm-param 'event'|'function'|'procedure' $type
156157
*
157158
* @return string XML with definitions
158159
*/
159-
private function exportRoutinesDefinition($db, $type, $dbitype)
160-
{
161-
// Export routines
162-
$routines = $GLOBALS['dbi']->getProceduresOrFunctions($db, $dbitype);
163-
164-
return $this->exportDefinitions($db, $type, $dbitype, $routines);
165-
}
166-
167-
/**
168-
* Generates output for SQL defintions
169-
*
170-
* @param string $db Database name
171-
* @param string $type Item type to be used in XML output
172-
* @param string $dbitype Item type used in DBI queries
173-
* @param array $names Names of items to export
174-
*
175-
* @return string XML with definitions
176-
*/
177-
private function exportDefinitions($db, $type, $dbitype, array $names)
160+
private function exportDefinitions(string $db, string $type, array $names): string
178161
{
179162
$GLOBALS['crlf'] = $GLOBALS['crlf'] ?? null;
180163

181164
$head = '';
182165

183-
if ($names) {
184-
foreach ($names as $name) {
185-
$head .= ' <pma:' . $type . ' name="'
186-
. htmlspecialchars($name) . '">' . $GLOBALS['crlf'];
166+
foreach ($names as $name) {
167+
$head .= ' <pma:' . $type . ' name="' . htmlspecialchars($name) . '">' . $GLOBALS['crlf'];
187168

188-
// Do some formatting
189-
$sql = $GLOBALS['dbi']->getDefinition($db, $dbitype, $name);
190-
$sql = htmlspecialchars(rtrim($sql));
191-
$sql = str_replace("\n", "\n ", $sql);
192-
193-
$head .= ' ' . $sql . $GLOBALS['crlf'];
194-
$head .= ' </pma:' . $type . '>' . $GLOBALS['crlf'];
169+
if ($type === 'function') {
170+
$definition = $GLOBALS['dbi']->getDefinition($db, 'FUNCTION', $name);
171+
} elseif ($type === 'procedure') {
172+
$definition = $GLOBALS['dbi']->getDefinition($db, 'PROCEDURE', $name);
173+
} else {
174+
$definition = $GLOBALS['dbi']->getDefinition($db, 'EVENT', $name);
195175
}
176+
177+
// Do some formatting
178+
$sql = htmlspecialchars(rtrim((string) $definition));
179+
$sql = str_replace("\n", "\n ", $sql);
180+
181+
$head .= ' ' . $sql . $GLOBALS['crlf'];
182+
$head .= ' </pma:' . $type . '>' . $GLOBALS['crlf'];
196183
}
197184

198185
return $head;
@@ -331,11 +318,19 @@ public function exportHeader(): bool
331318
}
332319

333320
if (isset($GLOBALS['xml_export_functions']) && $GLOBALS['xml_export_functions']) {
334-
$head .= $this->exportRoutinesDefinition($GLOBALS['db'], 'function', 'FUNCTION');
321+
$head .= $this->exportDefinitions(
322+
$GLOBALS['db'],
323+
'function',
324+
$GLOBALS['dbi']->getProceduresOrFunctions($GLOBALS['db'], 'FUNCTION')
325+
);
335326
}
336327

337328
if (isset($GLOBALS['xml_export_procedures']) && $GLOBALS['xml_export_procedures']) {
338-
$head .= $this->exportRoutinesDefinition($GLOBALS['db'], 'procedure', 'PROCEDURE');
329+
$head .= $this->exportDefinitions(
330+
$GLOBALS['db'],
331+
'procedure',
332+
$GLOBALS['dbi']->getProceduresOrFunctions($GLOBALS['db'], 'PROCEDURE')
333+
);
339334
}
340335

341336
if (isset($GLOBALS['xml_export_events']) && $GLOBALS['xml_export_events']) {
@@ -345,7 +340,7 @@ public function exportHeader(): bool
345340
. "WHERE EVENT_SCHEMA='" . $GLOBALS['dbi']->escapeString($GLOBALS['db'])
346341
. "'"
347342
);
348-
$head .= $this->exportDefinitions($GLOBALS['db'], 'event', 'EVENT', $events);
343+
$head .= $this->exportDefinitions($GLOBALS['db'], 'event', $events);
349344
}
350345

351346
unset($result);

phpstan-baseline.neon

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,11 +2545,6 @@ parameters:
25452545
count: 1
25462546
path: libraries/classes/DatabaseInterface.php
25472547

2548-
-
2549-
message: "#^Method PhpMyAdmin\\\\DatabaseInterface\\:\\:getProceduresOrFunctions\\(\\) return type has no value type specified in iterable type array\\.$#"
2550-
count: 1
2551-
path: libraries/classes/DatabaseInterface.php
2552-
25532548
-
25542549
message: "#^Method PhpMyAdmin\\\\DatabaseInterface\\:\\:getProtoInfo\\(\\) should return bool\\|int but returns int\\|string\\.$#"
25552550
count: 1
@@ -2680,11 +2675,6 @@ parameters:
26802675
count: 1
26812676
path: libraries/classes/Dbal/DbalInterface.php
26822677

2683-
-
2684-
message: "#^Method PhpMyAdmin\\\\Dbal\\\\DbalInterface\\:\\:getProceduresOrFunctions\\(\\) return type has no value type specified in iterable type array\\.$#"
2685-
count: 1
2686-
path: libraries/classes/Dbal/DbalInterface.php
2687-
26882678
-
26892679
message: "#^Method PhpMyAdmin\\\\Dbal\\\\DbalInterface\\:\\:getTables\\(\\) return type has no value type specified in iterable type array\\.$#"
26902680
count: 1
@@ -5505,11 +5495,6 @@ parameters:
55055495
count: 1
55065496
path: libraries/classes/Plugins/Export/ExportXml.php
55075497

5508-
-
5509-
message: "#^Method PhpMyAdmin\\\\Plugins\\\\Export\\\\ExportXml\\:\\:exportDefinitions\\(\\) has parameter \\$names with no value type specified in iterable type array\\.$#"
5510-
count: 1
5511-
path: libraries/classes/Plugins/Export/ExportXml.php
5512-
55135498
-
55145499
message: "#^Method PhpMyAdmin\\\\Plugins\\\\Export\\\\ExportXml\\:\\:getTables\\(\\) return type has no value type specified in iterable type array\\.$#"
55155500
count: 1

psalm-baseline.xml

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5734,10 +5734,12 @@
57345734
<code>SessionCache::get('mysql_cur_user')</code>
57355735
<code>reset($columns)</code>
57365736
</MixedReturnStatement>
5737-
<MixedReturnTypeCoercion occurrences="4">
5737+
<MixedReturnTypeCoercion occurrences="6">
5738+
<code>$result</code>
57385739
<code>$this-&gt;fetchResult($sql, null, 'Field', $link)</code>
57395740
<code>$this-&gt;fetchResult($sql, null, null, $link)</code>
57405741
<code>string[]</code>
5742+
<code>string[]</code>
57415743
</MixedReturnTypeCoercion>
57425744
<NullableReturnStatement occurrences="2">
57435745
<code>$user</code>
@@ -8663,7 +8665,7 @@
86638665
</InvalidReturnType>
86648666
</file>
86658667
<file src="libraries/classes/Operations.php">
8666-
<MixedArgument occurrences="35">
8668+
<MixedArgument occurrences="33">
86678669
<code>$_POST['comment']</code>
86688670
<code>$_POST['db_collation'] ?? ''</code>
86698671
<code>$_POST['new_auto_increment']</code>
@@ -8689,12 +8691,10 @@
86898691
<code>$event_name</code>
86908692
<code>$foreignTable</code>
86918693
<code>$foreignTable</code>
8692-
<code>$function_name</code>
86938694
<code>$newRowFormat</code>
86948695
<code>$newRowFormat</code>
86958696
<code>$old_priv</code>
86968697
<code>$one_query</code>
8697-
<code>$procedure_name</code>
86988698
<code>$this_what ?? 'data'</code>
86998699
<code>$trigger['create']</code>
87008700
<code>$view</code>
@@ -8756,12 +8756,11 @@
87568756
<code>$trigger['create']</code>
87578757
<code>$trigger['create']</code>
87588758
</MixedArrayAccess>
8759-
<MixedAssignment occurrences="17">
8759+
<MixedAssignment occurrences="15">
87608760
<code>$GLOBALS['auto_increment']</code>
87618761
<code>$arr</code>
87628762
<code>$event_name</code>
87638763
<code>$foreignTable</code>
8764-
<code>$function_name</code>
87658764
<code>$newRowFormat</code>
87668765
<code>$old_priv</code>
87678766
<code>$old_priv</code>
@@ -8770,7 +8769,6 @@
87708769
<code>$old_priv</code>
87718770
<code>$old_priv</code>
87728771
<code>$one_query</code>
8773-
<code>$procedure_name</code>
87748772
<code>$this_what</code>
87758773
<code>$trigger</code>
87768774
<code>$view</code>
@@ -9837,16 +9835,18 @@
98379835
</PossiblyUndefinedVariable>
98389836
</file>
98399837
<file src="libraries/classes/Plugins/Export/ExportXml.php">
9840-
<MixedArgument occurrences="8">
9838+
<MixedArgument occurrences="7">
98419839
<code>$code</code>
98429840
<code>$col_as</code>
98439841
<code>$db_charset</code>
98449842
<code>$db_collation</code>
9845-
<code>$name</code>
98469843
<code>$table</code>
98479844
<code>$table</code>
98489845
<code>$trigger['name']</code>
98499846
</MixedArgument>
9847+
<MixedArgumentTypeCoercion occurrences="1">
9848+
<code>$events</code>
9849+
</MixedArgumentTypeCoercion>
98509850
<MixedArrayAccess occurrences="5">
98519851
<code>$result[$table][1]</code>
98529852
<code>$result[0]['DEFAULT_CHARACTER_SET_NAME']</code>
@@ -9860,18 +9860,16 @@
98609860
<MixedArrayTypeCoercion occurrences="1">
98619861
<code>$result[$table]</code>
98629862
</MixedArrayTypeCoercion>
9863-
<MixedAssignment occurrences="8">
9863+
<MixedAssignment occurrences="7">
98649864
<code>$GLOBALS['tables']</code>
98659865
<code>$code</code>
98669866
<code>$col_as</code>
98679867
<code>$db_charset</code>
98689868
<code>$db_collation</code>
9869-
<code>$name</code>
98709869
<code>$table</code>
98719870
<code>$trigger</code>
98729871
</MixedAssignment>
9873-
<PossiblyNullArgument occurrences="2">
9874-
<code>$sql</code>
9872+
<PossiblyNullArgument occurrences="1">
98759873
<code>$table_alias</code>
98769874
</PossiblyNullArgument>
98779875
<PossiblyNullOperand occurrences="35">

0 commit comments

Comments
 (0)