Skip to content

Commit bd52a99

Browse files
Refactor ListAbstract (#18216)
* Move getList to ListDatabase This is clearly specific to only Database names Signed-off-by: Kamil Tekiela <tekiela246@gmail.com> * ListAbstract is a list of strings Signed-off-by: Kamil Tekiela <tekiela246@gmail.com> * getEmpty() seems to have no defined use Signed-off-by: Kamil Tekiela <tekiela246@gmail.com> * Update baselines Signed-off-by: Kamil Tekiela <tekiela246@gmail.com> * $targetDb param cannot be null Signed-off-by: Kamil Tekiela <tekiela246@gmail.com> --------- Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
1 parent 60a6f50 commit bd52a99

6 files changed

Lines changed: 44 additions & 96 deletions

File tree

‎libraries/classes/ListAbstract.php‎

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,34 @@
55
namespace PhpMyAdmin;
66

77
use ArrayObject;
8-
use PhpMyAdmin\Query\Utilities;
98

109
use function in_array;
1110

1211
/** @extends ArrayObject<int, string> */
1312
abstract class ListAbstract extends ArrayObject
1413
{
15-
/** @var mixed empty item */
16-
protected mixed $itemEmpty = '';
17-
1814
/**
19-
* defines what is an empty item (0, '', false or null)
20-
*
21-
* @return mixed an empty item
22-
*/
23-
public function getEmpty(): mixed
24-
{
25-
return $this->itemEmpty;
26-
}
27-
28-
/**
29-
* checks if the given db names exists in the current list, if there is
15+
* Checks if the given strings exists in the current list, if there is
3016
* missing at least one item it returns false otherwise true
31-
*
32-
* @param mixed[] ...$params params
3317
*/
34-
public function exists(...$params): bool
18+
public function exists(string ...$params): bool
3519
{
36-
$this_elements = $this->getArrayCopy();
37-
foreach ($params as $result) {
38-
if (! in_array($result, $this_elements)) {
20+
$elements = $this->getArrayCopy();
21+
foreach ($params as $param) {
22+
if (! in_array($param, $elements, true)) {
3923
return false;
4024
}
4125
}
4226

4327
return true;
4428
}
4529

46-
/** @return array<int, array<string, bool|string>> */
47-
public function getList(): array
48-
{
49-
$selected = $this->getDefault();
50-
51-
$list = [];
52-
foreach ($this as $eachItem) {
53-
if (Utilities::isSystemSchema($eachItem)) {
54-
continue;
55-
}
56-
57-
$list[] = [
58-
'name' => $eachItem,
59-
'is_selected' => $selected === $eachItem,
60-
];
61-
}
62-
63-
return $list;
64-
}
65-
6630
/**
67-
* returns default item
68-
*
69-
* @return string default item
31+
* Returns default item
7032
*/
7133
public function getDefault(): string
7234
{
73-
return $this->getEmpty();
35+
return '';
7436
}
7537

7638
/**

‎libraries/classes/ListDatabase.php‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace PhpMyAdmin;
66

7+
use PhpMyAdmin\Query\Utilities;
8+
79
use function array_merge;
810
use function is_array;
911
use function is_string;
@@ -34,6 +36,26 @@ public function __construct()
3436
$this->build();
3537
}
3638

39+
/** @return array<int, array<string, bool|string>> */
40+
public function getList(): array
41+
{
42+
$selected = $this->getDefault();
43+
44+
$list = [];
45+
foreach ($this as $eachItem) {
46+
if (Utilities::isSystemSchema($eachItem)) {
47+
continue;
48+
}
49+
50+
$list[] = [
51+
'name' => $eachItem,
52+
'is_selected' => $selected === $eachItem,
53+
];
54+
}
55+
56+
return $list;
57+
}
58+
3759
/**
3860
* checks if the configuration wants to hide some databases
3961
*/
@@ -157,6 +179,6 @@ public function getDefault(): string
157179
return $GLOBALS['db'];
158180
}
159181

160-
return $this->getEmpty();
182+
return parent::getDefault();
161183
}
162184
}

‎libraries/classes/Table.php‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -908,18 +908,18 @@ public static function duplicateInfo(
908908
/**
909909
* Copies or renames table
910910
*
911-
* @param string $sourceDb source database
912-
* @param string $sourceTable source table
913-
* @param string|null $targetDb target database
914-
* @param string $targetTable target table
915-
* @param string $what what to be moved or copied (data, dataonly)
916-
* @param bool $move whether to move
917-
* @param string $mode mode
911+
* @param string $sourceDb source database
912+
* @param string $sourceTable source table
913+
* @param string $targetDb target database
914+
* @param string $targetTable target table
915+
* @param string $what what to be moved or copied (data, dataonly)
916+
* @param bool $move whether to move
917+
* @param string $mode mode
918918
*/
919919
public static function moveCopy(
920-
$sourceDb,
920+
string $sourceDb,
921921
$sourceTable,
922-
string|null $targetDb,
922+
string $targetDb,
923923
$targetTable,
924924
$what,
925925
$move,
@@ -959,7 +959,7 @@ public static function moveCopy(
959959
$GLOBALS['message'] = Message::rawError(
960960
sprintf(
961961
__('Target database `%s` was not found!'),
962-
htmlspecialchars((string) $targetDb),
962+
htmlspecialchars($targetDb),
963963
),
964964
);
965965
}
@@ -977,7 +977,7 @@ public static function moveCopy(
977977

978978
// If the target database is not specified, the operation is taking
979979
// place in the same database.
980-
if (! isset($targetDb) || $targetDb === '') {
980+
if ($targetDb === '') {
981981
$targetDb = $sourceDb;
982982
}
983983

‎phpstan-baseline.neon‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,7 @@ parameters:
13611361
path: libraries/classes/Controllers/Database/Structure/CopyTableController.php
13621362

13631363
-
1364-
message: "#^Parameter \\#3 \\$targetDb of static method PhpMyAdmin\\\\Table\\:\\:moveCopy\\(\\) expects string\\|null, mixed given\\.$#"
1364+
message: "#^Parameter \\#3 \\$targetDb of static method PhpMyAdmin\\\\Table\\:\\:moveCopy\\(\\) expects string, mixed given\\.$#"
13651365
count: 1
13661366
path: libraries/classes/Controllers/Database/Structure/CopyTableController.php
13671367

@@ -5435,16 +5435,6 @@ parameters:
54355435
count: 1
54365436
path: libraries/classes/Linter.php
54375437

5438-
-
5439-
message: "#^Method PhpMyAdmin\\\\ListAbstract\\:\\:getDefault\\(\\) should return string but returns mixed\\.$#"
5440-
count: 1
5441-
path: libraries/classes/ListAbstract.php
5442-
5443-
-
5444-
message: "#^Method PhpMyAdmin\\\\ListDatabase\\:\\:getDefault\\(\\) should return string but returns mixed\\.$#"
5445-
count: 1
5446-
path: libraries/classes/ListDatabase.php
5447-
54485438
-
54495439
message: "#^Method PhpMyAdmin\\\\ListDatabase\\:\\:retrieve\\(\\) return type has no value type specified in iterable type array\\.$#"
54505440
count: 1

‎psalm-baseline.xml‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8547,30 +8547,16 @@
85478547
<code>USE_UTF_STRINGS</code>
85488548
</UndefinedConstant>
85498549
</file>
8550-
<file src="libraries/classes/ListAbstract.php">
8551-
<MixedInferredReturnType>
8552-
<code>string</code>
8553-
</MixedInferredReturnType>
8554-
<MixedReturnStatement>
8555-
<code><![CDATA[$this->getEmpty()]]></code>
8556-
</MixedReturnStatement>
8557-
</file>
85588550
<file src="libraries/classes/ListDatabase.php">
85598551
<InvalidArrayOffset>
85608552
<code><![CDATA[$GLOBALS['cfg']['Server']['hide_db']]]></code>
85618553
</InvalidArrayOffset>
85628554
<MixedArgumentTypeCoercion>
85638555
<code><![CDATA[usort($database_list, 'strnatcasecmp')]]></code>
85648556
</MixedArgumentTypeCoercion>
8565-
<MixedInferredReturnType>
8566-
<code>string</code>
8567-
</MixedInferredReturnType>
85688557
<MixedOperand>
85698558
<code><![CDATA[$GLOBALS['cfg']['Server']['hide_db']]]></code>
85708559
</MixedOperand>
8571-
<MixedReturnStatement>
8572-
<code><![CDATA[$this->getEmpty()]]></code>
8573-
</MixedReturnStatement>
85748560
</file>
85758561
<file src="libraries/classes/Logging.php">
85768562
<MixedAssignment>
@@ -13495,13 +13481,6 @@
1349513481
</PossiblyNullArgument>
1349613482
</file>
1349713483
<file src="libraries/classes/Table.php">
13498-
<InvalidArgument>
13499-
<code>$newDb</code>
13500-
<code>$sourceDb</code>
13501-
<code>$sourceDb</code>
13502-
<code>$targetDb</code>
13503-
<code>$targetDb</code>
13504-
</InvalidArgument>
1350513484
<InvalidArrayOffset>
1350613485
<code><![CDATA[$GLOBALS['add_constraints']]]></code>
1350713486
<code><![CDATA[$GLOBALS['errorUrl']]]></code>
@@ -15955,11 +15934,6 @@
1595515934
<code>array</code>
1595615935
</MixedInferredReturnType>
1595715936
</file>
15958-
<file src="test/classes/ListDatabaseTest.php">
15959-
<InvalidArgument>
15960-
<code><![CDATA['single_db']]></code>
15961-
</InvalidArgument>
15962-
</file>
1596315937
<file src="test/classes/MessageTest.php">
1596415938
<MixedInferredReturnType>
1596515939
<code>array</code>

‎test/classes/ListDatabaseTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ protected function setUp(): void
2929
}
3030

3131
/**
32-
* Test for ListDatabase::getEmpty
32+
* Test for ListDatabase::getDefault
3333
*/
3434
public function testEmpty(): void
3535
{
3636
$arr = new ListDatabase();
37-
$this->assertEquals('', $arr->getEmpty());
37+
$this->assertEquals('', $arr->getDefault());
3838
}
3939

4040
/**

0 commit comments

Comments
 (0)