diff --git a/psalm-baseline.xml b/psalm-baseline.xml
index c5ad20a76d..66b6563bdb 100644
--- a/psalm-baseline.xml
+++ b/psalm-baseline.xml
@@ -5099,6 +5099,7 @@
Config::getInstance()
Config::getInstance()
Config::getInstance()
+ Config::getInstance()
Routing::$route
@@ -7026,13 +7027,6 @@
-
- Config::getInstance()
- Config::getInstance()
- Config::getInstance()
- DatabaseInterface::getInstance()
- DatabaseInterface::getInstance()
-
$db
@@ -7042,6 +7036,9 @@
$db
+
+ $checkUserPrivileges
+
@@ -14451,7 +14448,6 @@
Config::getInstance()
- Config::getInstance()
diff --git a/src/DatabaseInterface.php b/src/DatabaseInterface.php
index 4ddee37c72..8a08478ccf 100644
--- a/src/DatabaseInterface.php
+++ b/src/DatabaseInterface.php
@@ -2073,7 +2073,7 @@ class DatabaseInterface implements DbalInterface
public function getDatabaseList(): ListDatabase
{
if ($this->databaseList === null) {
- $this->databaseList = new ListDatabase();
+ $this->databaseList = new ListDatabase($this, Config::getInstance(), new CheckUserPrivileges($this));
}
return $this->databaseList;
diff --git a/src/ListAbstract.php b/src/ListAbstract.php
deleted file mode 100644
index b955e12057..0000000000
--- a/src/ListAbstract.php
+++ /dev/null
@@ -1,42 +0,0 @@
- */
-abstract class ListAbstract extends ArrayObject
-{
- /**
- * Checks if the given strings exists in the current list, if there is
- * missing at least one item it returns false otherwise true
- */
- public function exists(string ...$params): bool
- {
- $elements = $this->getArrayCopy();
- foreach ($params as $param) {
- if (! in_array($param, $elements, true)) {
- return false;
- }
- }
-
- return true;
- }
-
- /**
- * Returns default item
- */
- public function getDefault(): string
- {
- return '';
- }
-
- /**
- * builds up the list
- */
- abstract public function build(): void;
-}
diff --git a/src/ListDatabase.php b/src/ListDatabase.php
index f8f58cfa64..4217807384 100644
--- a/src/ListDatabase.php
+++ b/src/ListDatabase.php
@@ -4,9 +4,11 @@ declare(strict_types=1);
namespace PhpMyAdmin;
+use ArrayObject;
use PhpMyAdmin\Query\Utilities;
use function array_merge;
+use function in_array;
use function is_array;
use function is_string;
use function preg_match;
@@ -17,22 +19,20 @@ use function strtr;
use function usort;
/**
- * handles database lists
+ * Handles database lists
*
- *
- * $ListDatabase = new ListDatabase();
- *
- *
- * @todo this object should be attached to the PMA_Server object
+ * @extends ArrayObject
*/
-class ListDatabase extends ListAbstract
+class ListDatabase extends ArrayObject
{
- public function __construct()
- {
+ public function __construct(
+ private readonly DatabaseInterface $dbi,
+ private readonly Config $config,
+ private readonly CheckUserPrivileges $checkUserPrivileges,
+ ) {
parent::__construct();
- $checkUserPrivileges = new CheckUserPrivileges(DatabaseInterface::getInstance());
- $checkUserPrivileges->getPrivileges();
+ $this->checkUserPrivileges->getPrivileges();
$this->build();
}
@@ -40,15 +40,13 @@ class ListDatabase extends ListAbstract
/** @return array> */
public function getList(): array
{
- $selected = $this->getDefault();
-
$list = [];
foreach ($this as $eachItem) {
if (Utilities::isSystemSchema($eachItem)) {
continue;
}
- $list[] = ['name' => $eachItem, 'is_selected' => $selected === $eachItem];
+ $list[] = ['name' => $eachItem, 'is_selected' => $eachItem === Current::$database];
}
return $list;
@@ -59,13 +57,12 @@ class ListDatabase extends ListAbstract
*/
protected function checkHideDatabase(): void
{
- $config = Config::getInstance();
- if (empty($config->selectedServer['hide_db'])) {
+ if (empty($this->config->selectedServer['hide_db'])) {
return;
}
foreach ($this->getArrayCopy() as $key => $db) {
- if (! preg_match('/' . $config->selectedServer['hide_db'] . '/', $db)) {
+ if (! preg_match('/' . $this->config->selectedServer['hide_db'] . '/', $db)) {
continue;
}
@@ -84,8 +81,7 @@ class ListDatabase extends ListAbstract
{
$databaseList = [];
$command = '';
- $config = Config::getInstance();
- if (! $config->selectedServer['DisableIS']) {
+ if (! $this->config->selectedServer['DisableIS']) {
$command .= 'SELECT `SCHEMA_NAME` FROM `INFORMATION_SCHEMA`.`SCHEMATA`';
if ($likeDbName !== null) {
$command .= " WHERE `SCHEMA_NAME` LIKE '" . $likeDbName . "'";
@@ -105,10 +101,10 @@ class ListDatabase extends ListAbstract
}
if ($command !== '') {
- $databaseList = DatabaseInterface::getInstance()->fetchResult($command);
+ $databaseList = $this->dbi->fetchResult($command);
}
- if ($config->settings['NaturalOrder']) {
+ if ($this->config->settings['NaturalOrder']) {
usort($databaseList, strnatcasecmp(...));
} else {
// need to sort anyway, otherwise information_schema
@@ -137,18 +133,19 @@ class ListDatabase extends ListAbstract
*/
protected function checkOnlyDatabase(): bool
{
- $config = Config::getInstance();
- if (is_string($config->selectedServer['only_db']) && strlen($config->selectedServer['only_db']) > 0) {
- $config->selectedServer['only_db'] = [$config->selectedServer['only_db']];
+ if (
+ is_string($this->config->selectedServer['only_db']) && strlen($this->config->selectedServer['only_db']) > 0
+ ) {
+ $this->config->selectedServer['only_db'] = [$this->config->selectedServer['only_db']];
}
- if (! is_array($config->selectedServer['only_db'])) {
+ if (! is_array($this->config->selectedServer['only_db'])) {
return false;
}
$items = [];
- foreach ($config->selectedServer['only_db'] as $eachOnlyDb) {
+ foreach ($this->config->selectedServer['only_db'] as $eachOnlyDb) {
// check if the db name contains wildcard,
// thus containing not escaped _ or %
if (! preg_match('/(^|[^\\\\])(_|%)/', $eachOnlyDb)) {
@@ -166,16 +163,18 @@ class ListDatabase extends ListAbstract
}
/**
- * returns default item
- *
- * @return string default item
+ * Checks if the given strings exists in the current list, if there is
+ * missing at least one item it returns false otherwise true
*/
- public function getDefault(): string
+ public function exists(string ...$params): bool
{
- if (Current::$database !== '') {
- return Current::$database;
+ $elements = $this->getArrayCopy();
+ foreach ($params as $param) {
+ if (! in_array($param, $elements, true)) {
+ return false;
+ }
}
- return parent::getDefault();
+ return true;
}
}
diff --git a/tests/classes/ListDatabaseTest.php b/tests/classes/ListDatabaseTest.php
index a22b0ad86a..894f7b66b6 100644
--- a/tests/classes/ListDatabaseTest.php
+++ b/tests/classes/ListDatabaseTest.php
@@ -4,9 +4,9 @@ declare(strict_types=1);
namespace PhpMyAdmin\Tests;
+use PhpMyAdmin\CheckUserPrivileges;
use PhpMyAdmin\Config;
use PhpMyAdmin\Current;
-use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\ListDatabase;
use PHPUnit\Framework\Attributes\CoversClass;
@@ -25,20 +25,11 @@ class ListDatabaseTest extends AbstractTestCase
{
parent::setUp();
- DatabaseInterface::$instance = $this->createDatabaseInterface();
- $config = Config::getInstance();
+ $dbi = $this->createDatabaseInterface();
+ $config = new Config();
$config->selectedServer['DisableIS'] = false;
$config->selectedServer['only_db'] = ['single\\_db'];
- $this->object = new ListDatabase();
- }
-
- /**
- * Test for ListDatabase::getDefault
- */
- public function testEmpty(): void
- {
- $arr = new ListDatabase();
- $this->assertEquals('', $arr->getDefault());
+ $this->object = new ListDatabase($dbi, $config, new CheckUserPrivileges($dbi));
}
/**
@@ -46,13 +37,21 @@ class ListDatabaseTest extends AbstractTestCase
*/
public function testExists(): void
{
- $arr = new ListDatabase();
+ $dbi = $this->createDatabaseInterface();
+ $config = new Config();
+ $config->selectedServer['DisableIS'] = false;
+ $config->selectedServer['only_db'] = ['single\\_db'];
+ $arr = new ListDatabase($dbi, $config, new CheckUserPrivileges($dbi));
$this->assertTrue($arr->exists('single_db'));
}
public function testGetList(): void
{
- $arr = new ListDatabase();
+ $dbi = $this->createDatabaseInterface();
+ $config = new Config();
+ $config->selectedServer['DisableIS'] = false;
+ $config->selectedServer['only_db'] = ['single\\_db'];
+ $arr = new ListDatabase($dbi, $config, new CheckUserPrivileges($dbi));
Current::$database = 'db';
$this->assertEquals(
@@ -83,22 +82,4 @@ class ListDatabaseTest extends AbstractTestCase
'',
);
}
-
- /**
- * Test for getDefault
- */
- public function testGetDefault(): void
- {
- Current::$database = '';
- $this->assertEquals(
- $this->object->getDefault(),
- '',
- );
-
- Current::$database = 'mysql';
- $this->assertEquals(
- $this->object->getDefault(),
- 'mysql',
- );
- }
}