Skip to content

Commit

Permalink
Replace DBG config array access with Debug class
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Mar 17, 2024
1 parent 8754b9b commit c9c286b
Show file tree
Hide file tree
Showing 17 changed files with 41 additions and 58 deletions.
15 changes: 0 additions & 15 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16725,16 +16725,6 @@ parameters:
count: 2
path: tests/unit/Tracking/TrackingTest.php

-
message: "#^Cannot access offset 'sql' on mixed\\.$#"
count: 1
path: tests/unit/TransformationsTest.php

-
message: "#^Property PhpMyAdmin\\\\Config\\:\\:\\$settings \\(array\\{PmaAbsoluteUri\\: string, AuthLog\\: string, AuthLogSuccess\\: bool, PmaNoRelation_DisableWarning\\: bool, SuhosinDisableWarning\\: bool, LoginCookieValidityDisableWarning\\: bool, ReservedWordDisableWarning\\: bool, TranslationWarningThreshold\\: int, \\.\\.\\.\\}\\) does not accept array\\{PmaAbsoluteUri\\: string, AuthLog\\: string, AuthLogSuccess\\: bool, PmaNoRelation_DisableWarning\\: bool, SuhosinDisableWarning\\: bool, LoginCookieValidityDisableWarning\\: bool, ReservedWordDisableWarning\\: bool, TranslationWarningThreshold\\: int, \\.\\.\\.\\}\\.$#"
count: 1
path: tests/unit/TransformationsTest.php

-
message: "#^Property PhpMyAdmin\\\\Config\\:\\:\\$settings \\(array\\{PmaAbsoluteUri\\: string, AuthLog\\: string, AuthLogSuccess\\: bool, PmaNoRelation_DisableWarning\\: bool, SuhosinDisableWarning\\: bool, LoginCookieValidityDisableWarning\\: bool, ReservedWordDisableWarning\\: bool, TranslationWarningThreshold\\: int, \\.\\.\\.\\}\\) does not accept array\\{ServerDefault\\: 1, ActionLinksMode\\: 'icons'\\}\\.$#"
count: 1
Expand All @@ -16755,11 +16745,6 @@ parameters:
count: 2
path: tests/unit/TwoFactorTest.php

-
message: "#^Property PhpMyAdmin\\\\Config\\:\\:\\$settings \\(array\\{PmaAbsoluteUri\\: string, AuthLog\\: string, AuthLogSuccess\\: bool, PmaNoRelation_DisableWarning\\: bool, SuhosinDisableWarning\\: bool, LoginCookieValidityDisableWarning\\: bool, ReservedWordDisableWarning\\: bool, TranslationWarningThreshold\\: int, \\.\\.\\.\\}\\) does not accept array\\{PmaAbsoluteUri\\: string, AuthLog\\: string, AuthLogSuccess\\: bool, PmaNoRelation_DisableWarning\\: bool, SuhosinDisableWarning\\: bool, LoginCookieValidityDisableWarning\\: bool, ReservedWordDisableWarning\\: bool, TranslationWarningThreshold\\: int, \\.\\.\\.\\}\\.$#"
count: 1
path: tests/unit/TwoFactorTest.php

-
message: "#^Class PhpMyAdmin\\\\Tests\\\\TypesByDatabaseVersionTest has an uninitialized property \\$object\\. Give it default value or assign it in the constructor\\.$#"
count: 1
Expand Down
27 changes: 16 additions & 11 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13231,6 +13231,10 @@
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
</DeprecatedMethod>
<InaccessibleProperty>
<code><![CDATA[$config->config->DBG->sql]]></code>
<code><![CDATA[$config->config->DBG->sql]]></code>
</InaccessibleProperty>
<PossiblyUnusedMethod>
<code><![CDATA[currentRolesData]]></code>
<code><![CDATA[currentUserData]]></code>
Expand Down Expand Up @@ -13440,6 +13444,10 @@
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
<InaccessibleProperty>
<code><![CDATA[$config->config->DBG->sql]]></code>
<code><![CDATA[$config->config->DBG->sql]]></code>
</InaccessibleProperty>
<MixedArrayAssignment>
<code><![CDATA[$_SESSION['debug']['queries']]]></code>
</MixedArrayAssignment>
Expand Down Expand Up @@ -14409,7 +14417,6 @@
<code><![CDATA[$config->settings]]></code>
<code><![CDATA[$config->settings]]></code>
<code><![CDATA[$config->settings]]></code>
<code><![CDATA[$config->settings]]></code>
</InvalidPropertyAssignmentValue>
</file>
<file src="tests/unit/Routing/RoutingTest.php">
Expand Down Expand Up @@ -14562,11 +14569,6 @@
<code><![CDATA[providerGetEngine]]></code>
</PossiblyUnusedMethod>
</file>
<file src="tests/unit/Stubs/DbiDummy.php">
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
</file>
<file src="tests/unit/Stubs/DummyResult.php">
<InvalidReturnStatement>
<code><![CDATA[array_column($this->result, 1, 0)]]></code>
Expand Down Expand Up @@ -14699,7 +14701,6 @@
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
<InvalidPropertyAssignmentValue>
<code><![CDATA[$config->settings]]></code>
<code><![CDATA[['ServerDefault' => 1, 'ActionLinksMode' => 'icons']]]></code>
</InvalidPropertyAssignmentValue>
<PossiblyUnusedMethod>
Expand Down Expand Up @@ -14737,10 +14738,14 @@
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
<InvalidPropertyAssignmentValue>
<code><![CDATA[$config->settings]]></code>
<code><![CDATA[$config->settings]]></code>
</InvalidPropertyAssignmentValue>
<InaccessibleProperty>
<code><![CDATA[$config->config->DBG->simple2fa]]></code>
<code><![CDATA[$config->config->DBG->simple2fa]]></code>
<code><![CDATA[$config->config->DBG->simple2fa]]></code>
<code><![CDATA[$config->config->DBG->simple2fa]]></code>
<code><![CDATA[$config->config->DBG->simple2fa]]></code>
<code><![CDATA[$config->config->DBG->simple2fa]]></code>
</InaccessibleProperty>
<MixedArgument>
<code><![CDATA[$object->config['settings']['secret']]]></code>
<code><![CDATA[$object->config['settings']['secret']]]></code>
Expand Down
3 changes: 2 additions & 1 deletion src/Config/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ public static function testDBConnection(
string $pass,
string $errorKey = 'Server',
): bool|array {
if (Config::getInstance()->settings['DBG']['demo']) {
$config = Config::getInstance();
if ($config->config->DBG->demo) {
// Connection test disabled on the demo server!
return true;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,12 @@ public function getDisplay(): string
$bookmarkFeature = $this->relation->getRelationParameters()->bookmarkFeature;
$sqlHistory = $this->relation->getHistory($this->config->selectedServer['user']);
$bookmarkContent = $this->getBookmarkContent();
$debug = isset($this->config->settings['DBG']) && $this->config->settings['DBG']['sql'];

return $this->template->render('console/display', [
'has_bookmark_feature' => $bookmarkFeature !== null,
'sql_history' => $sqlHistory,
'bookmark_content' => $bookmarkContent,
'debug' => $debug,
'debug' => $this->config->config->DBG->sql,
]);
}
}
2 changes: 1 addition & 1 deletion src/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function __invoke(ServerRequest $request): Response|null
'server' => Current::$server,
'sync_favorite_tables' => $syncFavoriteTables,
'has_server' => $hasServer,
'is_demo' => $config->settings['DBG']['demo'],
'is_demo' => $config->config->DBG->demo,
'has_server_selection' => $hasServerSelection ?? false,
'server_selection' => $serverSelection ?? '',
'has_change_password_link' => ($config->selectedServer['auth_type'] ?? '') !== 'config'
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/Setup/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
public function __invoke(ServerRequest $request): Response
{
$config = Config::getInstance();
if (@file_exists(CONFIG_FILE) && ! $config->settings['DBG']['demo']) {
if (@file_exists(CONFIG_FILE) && ! $config->config->DBG->demo) {
$response = $this->responseFactory->createResponse(StatusCodeInterface::STATUS_NOT_FOUND);

return $response->write($this->template->render('error/generic', [
Expand Down
5 changes: 2 additions & 3 deletions src/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ public function tryQuery(
int $options = self::QUERY_BUFFERED,
bool $cacheAffectedRows = true,
): ResultInterface|false {
$debug = isset($this->config->settings['DBG']) && $this->config->settings['DBG']['sql'];
if (! isset($this->connections[$connectionType->value])) {
return false;
}
Expand All @@ -226,15 +225,15 @@ public function tryQuery(
$GLOBALS['cached_affected_rows'] = $this->affectedRows($connectionType, false);
}

if ($debug) {
if ($this->config->config->DBG->sql) {
$errorMessage = $this->getError($connectionType);
Utilities::debugLogQueryIntoSession(
$query,
$errorMessage !== '' ? $errorMessage : null,
$result,
$this->lastQueryExecutionTime,
);
if ($this->config->settings['DBG']['sqllog']) {
if ($this->config->config->DBG->sqllog) {
openlog('phpMyAdmin', LOG_NDELAY | LOG_PID, LOG_USER);

syslog(
Expand Down
8 changes: 3 additions & 5 deletions src/Footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ private static function removeRecursion(mixed &$object, array $stack = []): mixe
public function getDebugMessage(): string
{
$retval = '\'false\'';
if (
$this->config->settings['DBG']['sql'] && empty($_REQUEST['no_debug']) && ! empty($_SESSION['debug'])
) {
if ($this->config->config->DBG->sql && empty($_REQUEST['no_debug']) && ! empty($_SESSION['debug'])) {
// Remove recursions and iterators from $_SESSION['debug']
self::removeRecursion($_SESSION['debug']);

Expand Down Expand Up @@ -241,7 +239,7 @@ public function getDisplay(): string
$errorMessages = $this->getErrorMessages();
$scripts = $this->scripts->getDisplay();

if ($this->config->settings['DBG']['demo']) {
if ($this->config->config->DBG->demo) {
$gitRevisionInfo = $this->getGitRevisionInfo();
}

Expand All @@ -254,7 +252,7 @@ public function getDisplay(): string
'self_url' => $url ?? null,
'error_messages' => $errorMessages ?? '',
'scripts' => $scripts ?? '',
'is_demo' => $this->config->settings['DBG']['demo'],
'is_demo' => $this->config->config->DBG->demo,
'git_revision_info' => $gitRevisionInfo ?? [],
'footer' => $footer ?? '',
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/Auth/AuthenticationCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function showLoginForm(): never

$response->addHTML($this->template->render('login/form', [
'login_header' => $loginHeader,
'is_demo' => $config->settings['DBG']['demo'],
'is_demo' => $config->config->DBG->demo,
'error_messages' => $errorMessages,
'available_languages' => $availableLanguages,
'is_session_expired' => $sessionExpired,
Expand Down
3 changes: 2 additions & 1 deletion src/TwoFactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public function showSubmit(): bool
public function getAvailableBackends(): array
{
$result = [];
if (Config::getInstance()->settings['DBG']['simple2fa']) {
$config = Config::getInstance();
if ($config->config->DBG->simple2fa) {
$result[] = 'simple';
}

Expand Down
5 changes: 3 additions & 2 deletions tests/unit/DatabaseInterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ public function testGetDbCollation(): void

$config = Config::getInstance();
$config->selectedServer['DisableIS'] = false;
$config->settings['DBG']['sql'] = false;

self::assertSame(
'utf8_general_ci',
Expand All @@ -300,8 +299,10 @@ public function testGetDbCollation(): void
public function testGetServerCollation(): void
{
$dbi = $this->createDatabaseInterface();
Config::getInstance()->settings['DBG']['sql'] = true;
$config = Config::getInstance();
$config->config->DBG->sql = true;
self::assertSame('utf8_general_ci', $dbi->getServerCollation());
$config->config->DBG->sql = false;
}

/**
Expand Down
1 change: 0 additions & 1 deletion tests/unit/Dbal/DbiDummyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected function setUp(): void
$this->dbi = $this->createDatabaseInterface($this->dummyDbi);
DatabaseInterface::$instance = $this->dbi;
$config = Config::getInstance();
$config->settings['DBG']['sql'] = false;
$config->settings['IconvExtraParams'] = '';
}

Expand Down
4 changes: 3 additions & 1 deletion tests/unit/FooterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ protected function tearDown(): void
#[Group('medium')]
public function testGetDebugMessage(): void
{
Config::getInstance()->settings['DBG']['sql'] = true;
$config = Config::getInstance();
$config->config->DBG->sql = true;
$_SESSION['debug']['queries'] = [
['count' => 1, 'time' => 0.2, 'query' => 'SELECT * FROM `pma_bookmark` WHERE 1'],
['count' => 1, 'time' => 2.5, 'query' => 'SELECT * FROM `db` WHERE 1'],
Expand All @@ -79,6 +80,7 @@ public function testGetDebugMessage(): void
. '{"count":1,"time":2.5,"query":"SELECT * FROM `db` WHERE 1"}]}',
$this->object->getDebugMessage(),
);
$config->config->DBG->sql = false;
}

/**
Expand Down
1 change: 0 additions & 1 deletion tests/unit/Replication/ReplicationGuiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ protected function setUp(): void
$config->settings['ShowSQL'] = true;
$config->settings['TableNavigationLinksMode'] = 'icons';
$config->settings['LimitChars'] = 100;
$config->settings['DBG']['sql'] = false;
$config->settings['ShowHint'] = true;

Current::$table = 'table';
Expand Down
4 changes: 0 additions & 4 deletions tests/unit/Stubs/DbiDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace PhpMyAdmin\Tests\Stubs;

use PhpMyAdmin\Config;
use PhpMyAdmin\Config\Settings\Server;
use PhpMyAdmin\Dbal\Connection;
use PhpMyAdmin\Dbal\DbiExtension;
Expand Down Expand Up @@ -2256,8 +2255,5 @@ private function init(): void
],
],
];

/* Some basic setup for dummy driver */
Config::getInstance()->settings['DBG']['sql'] = false;
}
}
1 change: 0 additions & 1 deletion tests/unit/TransformationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ protected function setUp(): void
$config->selectedServer['table_info'] = '';
$config->selectedServer['table_coords'] = '';
$config->selectedServer['column_info'] = 'column_info';
$config->settings['DBG']['sql'] = false;

$this->transformations = new Transformations();
}
Expand Down
13 changes: 6 additions & 7 deletions tests/unit/TwoFactorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ protected function setUp(): void
Current::$table = 'table';
$config = Config::getInstance();
$config->selectedServer['DisableIS'] = false;
$config->settings['DBG'] = ['simple2fa' => false, 'sql' => false];
$config->settings['NaturalOrder'] = true;
$this->initStorageConfigAndData();
}
Expand Down Expand Up @@ -175,11 +174,11 @@ public function testSimple(): void
{
$request = new ServerRequest(self::createStub(ServerRequestInterface::class));
$config = Config::getInstance();
$config->settings['DBG']['simple2fa'] = true;
$config->config->DBG->simple2fa = true;
$object = $this->getTwoFactorAndLoadConfig('user', ['type' => 'db', 'backend' => 'simple']);
$backend = $object->getBackend();
self::assertSame('simple', $backend::$id);
$config->settings['DBG']['simple2fa'] = false;
$config->config->DBG->simple2fa = false;

unset($_POST['2fa_confirm']);
self::assertFalse($object->check($request, true));
Expand All @@ -204,7 +203,7 @@ public function testConfigureSimple(): void
{
$request = new ServerRequest(self::createStub(ServerRequestInterface::class));
$config = Config::getInstance();
$config->settings['DBG']['simple2fa'] = true;
$config->config->DBG->simple2fa = true;
$object = $this->getTwoFactorAndLoadConfig('user', null);

$this->dummyDbi->assertAllQueriesConsumed();
Expand All @@ -229,7 +228,7 @@ public function testConfigureSimple(): void

$this->initStorageConfigAndData();// Needs a re-init

$config->settings['DBG']['simple2fa'] = false;
$config->config->DBG->simple2fa = false;
$object = $this->getTwoFactorAndLoadConfig('user', null);
self::assertFalse($object->configure($request, 'simple'));
$this->dummyDbi->assertAllQueriesConsumed();
Expand Down Expand Up @@ -484,13 +483,13 @@ public function testKeyAuthentication(): void
public function testBackends(): void
{
$config = Config::getInstance();
$config->settings['DBG']['simple2fa'] = true;
$config->config->DBG->simple2fa = true;
$object = $this->getTwoFactorAndLoadConfig('user', null);
$backends = $object->getAllBackends();
self::assertCount(
count($object->getAvailable()) + 1,
$backends,
);
$config->settings['DBG']['simple2fa'] = false;
$config->config->DBG->simple2fa = false;
}
}

0 comments on commit c9c286b

Please sign in to comment.