Skip to content

Commit 2cc3bf4

Browse files
Merge pull request #17881 from kamil-tekiela/Refactor-PrimaryController.php
Redesign PrimaryController
2 parents 1c49ed0 + ba41957 commit 2cc3bf4

3 files changed

Lines changed: 19 additions & 59 deletions

File tree

libraries/classes/Controllers/Table/Structure/PrimaryController.php

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use function __;
1919
use function count;
20+
use function is_array;
2021

2122
final class PrimaryController extends AbstractController
2223
{
@@ -43,26 +44,23 @@ public function __invoke(ServerRequest $request): void
4344
$GLOBALS['urlParams'] = $GLOBALS['urlParams'] ?? null;
4445
$GLOBALS['errorUrl'] = $GLOBALS['errorUrl'] ?? null;
4546

46-
$selected = $request->getParsedBodyParam('selected', []);
47-
$selected_fld = $request->getParsedBodyParam('selected_fld', []);
47+
/** @var string[]|null $selected */
48+
$selected = $request->getParsedBodyParam('selected_fld', $request->getParsedBodyParam('selected'));
4849

49-
if (empty($selected) && empty($selected_fld)) {
50+
if (! is_array($selected) || $selected === []) {
5051
$this->response->setRequestStatus(false);
5152
$this->response->addJSON('message', __('No column selected.'));
5253

5354
return;
5455
}
5556

56-
$primary = $this->getKeyForTablePrimary();
57-
if (empty($primary) && ! empty($selected_fld)) {
58-
// no primary key, so we can safely create new
59-
$mult_btn = __('Yes');
60-
$selected = $selected_fld;
61-
}
57+
$this->dbi->selectDb($GLOBALS['db']);
58+
$hasPrimary = $this->hasPrimaryKey();
6259

63-
$mult_btn = $_POST['mult_btn'] ?? $mult_btn ?? '';
60+
/** @var string|null $deletionConfirmed */
61+
$deletionConfirmed = $request->getParsedBodyParam('mult_btn', null);
6462

65-
if (! empty($selected_fld) && ! empty($primary)) {
63+
if ($hasPrimary && $deletionConfirmed === null) {
6664
$this->checkParameters(['db', 'table']);
6765

6866
$GLOBALS['urlParams'] = ['db' => $GLOBALS['db'], 'table' => $GLOBALS['table']];
@@ -74,15 +72,15 @@ public function __invoke(ServerRequest $request): void
7472
$this->render('table/structure/primary', [
7573
'db' => $GLOBALS['db'],
7674
'table' => $GLOBALS['table'],
77-
'selected' => $selected_fld,
75+
'selected' => $selected,
7876
]);
7977

8078
return;
8179
}
8280

83-
if ($mult_btn === __('Yes')) {
81+
if ($deletionConfirmed === __('Yes') || ! $hasPrimary) {
8482
$GLOBALS['sql_query'] = 'ALTER TABLE ' . Util::backquote($GLOBALS['table']);
85-
if (! empty($primary)) {
83+
if ($hasPrimary) {
8684
$GLOBALS['sql_query'] .= ' DROP PRIMARY KEY,';
8785
}
8886

@@ -110,27 +108,16 @@ public function __invoke(ServerRequest $request): void
110108
($this->structureController)($request);
111109
}
112110

113-
/**
114-
* Gets table primary key
115-
*
116-
* @return string
117-
*/
118-
private function getKeyForTablePrimary()
111+
private function hasPrimaryKey(): bool
119112
{
120-
$this->dbi->selectDb($GLOBALS['db']);
121-
$result = $this->dbi->query(
122-
'SHOW KEYS FROM ' . Util::backquote($GLOBALS['table']) . ';'
123-
);
124-
$primary = '';
113+
$result = $this->dbi->query('SHOW KEYS FROM ' . Util::backquote($GLOBALS['table']));
114+
125115
foreach ($result as $row) {
126-
// Backups the list of primary keys
127-
if ($row['Key_name'] !== 'PRIMARY') {
128-
continue;
116+
if ($row['Key_name'] === 'PRIMARY') {
117+
return true;
129118
}
130-
131-
$primary .= $row['Column_name'] . ', ';
132119
}
133120

134-
return $primary;
121+
return false;
135122
}
136123
}

phpstan-baseline.neon

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,21 +1745,6 @@ parameters:
17451745
count: 1
17461746
path: libraries/classes/Controllers/Table/Structure/PartitioningController.php
17471747

1748-
-
1749-
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
1750-
count: 1
1751-
path: libraries/classes/Controllers/Table/Structure/PrimaryController.php
1752-
1753-
-
1754-
message: "#^Parameter \\#1 \\$identifier of static method PhpMyAdmin\\\\Util\\:\\:backquote\\(\\) expects string\\|Stringable\\|null, mixed given\\.$#"
1755-
count: 1
1756-
path: libraries/classes/Controllers/Table/Structure/PrimaryController.php
1757-
1758-
-
1759-
message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#"
1760-
count: 1
1761-
path: libraries/classes/Controllers/Table/Structure/PrimaryController.php
1762-
17631748
-
17641749
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
17651750
count: 1

psalm-baseline.xml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3998,21 +3998,9 @@
39983998
</RedundantConditionGivenDocblockType>
39993999
</file>
40004000
<file src="libraries/classes/Controllers/Table/Structure/PrimaryController.php">
4001-
<MixedArgument occurrences="2">
4002-
<code>$field</code>
4003-
<code>$selected</code>
4004-
</MixedArgument>
4005-
<MixedAssignment occurrences="6">
4001+
<MixedAssignment occurrences="1">
40064002
<code>$GLOBALS['errorUrl']</code>
4007-
<code>$field</code>
4008-
<code>$mult_btn</code>
4009-
<code>$selected</code>
4010-
<code>$selected</code>
4011-
<code>$selected_fld</code>
40124003
</MixedAssignment>
4013-
<PossiblyNullOperand occurrences="1">
4014-
<code>$row['Column_name']</code>
4015-
</PossiblyNullOperand>
40164004
</file>
40174005
<file src="libraries/classes/Controllers/Table/Structure/ReservedWordCheckController.php">
40184006
<MixedArgument occurrences="1">

0 commit comments

Comments
 (0)