From 4f92a073ae18bdc0bff16ae2e64169544f147df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sat, 6 Jan 2024 22:09:39 -0300 Subject: [PATCH 1/3] Remove ListAbstract::getDefault() method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This method only returns the value of Current::$database. Signed-off-by: Maurício Meneghini Fauth --- src/ListAbstract.php | 8 -------- src/ListDatabase.php | 18 +----------------- tests/classes/ListDatabaseTest.php | 27 --------------------------- 3 files changed, 1 insertion(+), 52 deletions(-) diff --git a/src/ListAbstract.php b/src/ListAbstract.php index b955e12057..49b7a2dd67 100644 --- a/src/ListAbstract.php +++ b/src/ListAbstract.php @@ -27,14 +27,6 @@ abstract class ListAbstract extends ArrayObject return true; } - /** - * Returns default item - */ - public function getDefault(): string - { - return ''; - } - /** * builds up the list */ diff --git a/src/ListDatabase.php b/src/ListDatabase.php index f8f58cfa64..b7eda762f2 100644 --- a/src/ListDatabase.php +++ b/src/ListDatabase.php @@ -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; @@ -164,18 +162,4 @@ class ListDatabase extends ListAbstract return true; } - - /** - * returns default item - * - * @return string default item - */ - public function getDefault(): string - { - if (Current::$database !== '') { - return Current::$database; - } - - return parent::getDefault(); - } } diff --git a/tests/classes/ListDatabaseTest.php b/tests/classes/ListDatabaseTest.php index a22b0ad86a..ebfff05aaf 100644 --- a/tests/classes/ListDatabaseTest.php +++ b/tests/classes/ListDatabaseTest.php @@ -32,15 +32,6 @@ class ListDatabaseTest extends AbstractTestCase $this->object = new ListDatabase(); } - /** - * Test for ListDatabase::getDefault - */ - public function testEmpty(): void - { - $arr = new ListDatabase(); - $this->assertEquals('', $arr->getDefault()); - } - /** * Test for ListDatabase::exists */ @@ -83,22 +74,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', - ); - } } From 8a9690749d07ed932a000be1e9c0d9fd0a377eac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sat, 6 Jan 2024 22:18:41 -0300 Subject: [PATCH 2/3] Remove ListAbstract class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This abstract class is not necessary. Signed-off-by: Maurício Meneghini Fauth --- src/ListAbstract.php | 34 ---------------------------------- src/ListDatabase.php | 28 +++++++++++++++++++++------- 2 files changed, 21 insertions(+), 41 deletions(-) delete mode 100644 src/ListAbstract.php diff --git a/src/ListAbstract.php b/src/ListAbstract.php deleted file mode 100644 index 49b7a2dd67..0000000000 --- a/src/ListAbstract.php +++ /dev/null @@ -1,34 +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; - } - - /** - * builds up the list - */ - abstract public function build(): void; -} diff --git a/src/ListDatabase.php b/src/ListDatabase.php index b7eda762f2..38c97ce52e 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,15 +19,11 @@ 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() { @@ -162,4 +160,20 @@ class ListDatabase extends ListAbstract return true; } + + /** + * 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; + } } From 0a15e1ca0b478c0bfe987e5f6526f88957acc748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sun, 7 Jan 2024 00:00:38 -0300 Subject: [PATCH 3/3] Extract dependencies from ListDatabase class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- psalm-baseline.xml | 12 ++++------- src/DatabaseInterface.php | 2 +- src/ListDatabase.php | 33 +++++++++++++++--------------- tests/classes/ListDatabaseTest.php | 20 ++++++++++++------ 4 files changed, 36 insertions(+), 31 deletions(-) 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/ListDatabase.php b/src/ListDatabase.php index 38c97ce52e..4217807384 100644 --- a/src/ListDatabase.php +++ b/src/ListDatabase.php @@ -25,12 +25,14 @@ use function usort; */ 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(); } @@ -55,13 +57,12 @@ class ListDatabase extends ArrayObject */ 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; } @@ -80,8 +81,7 @@ class ListDatabase extends ArrayObject { $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 . "'"; @@ -101,10 +101,10 @@ class ListDatabase extends ArrayObject } 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 @@ -133,18 +133,19 @@ class ListDatabase extends ArrayObject */ 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)) { diff --git a/tests/classes/ListDatabaseTest.php b/tests/classes/ListDatabaseTest.php index ebfff05aaf..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,11 +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(); + $this->object = new ListDatabase($dbi, $config, new CheckUserPrivileges($dbi)); } /** @@ -37,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(