Merge pull request #20147 from kamil-tekiela/Clean-up-setUserValue

Clean up setUserValue
This commit is contained in:
Maurício Meneghini Fauth 2026-03-17 14:35:06 -03:00 committed by GitHub
commit 8f0fa8247d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 49 additions and 67 deletions

View File

@ -921,30 +921,12 @@ parameters:
count: 1
path: src/Config/UserPreferencesHandler.php
-
message: '#^Cannot access offset ''Server'' on mixed\.$#'
identifier: offsetAccess.nonOffsetAccessible
count: 1
path: src/Config/UserPreferencesHandler.php
-
message: '#^Cannot access offset ''ThemeDefault'' on mixed\.$#'
identifier: offsetAccess.nonOffsetAccessible
count: 2
path: src/Config/UserPreferencesHandler.php
-
message: '#^Cannot access offset ''config_mtime'' on mixed\.$#'
identifier: offsetAccess.nonOffsetAccessible
count: 2
path: src/Config/UserPreferencesHandler.php
-
message: '#^Cannot access offset ''lang'' on mixed\.$#'
identifier: offsetAccess.nonOffsetAccessible
count: 3
path: src/Config/UserPreferencesHandler.php
-
message: '#^Cannot access offset ''userprefs'' on mixed\.$#'
identifier: offsetAccess.nonOffsetAccessible
@ -975,24 +957,12 @@ parameters:
count: 1
path: src/Config/UserPreferencesHandler.php
-
message: '#^Only booleans are allowed in &&, string\|null given on the right side\.$#'
identifier: booleanAnd.rightNotBoolean
count: 1
path: src/Config/UserPreferencesHandler.php
-
message: '#^Parameter \#1 \$code of method PhpMyAdmin\\I18n\\LanguageManager\:\:getLanguage\(\) expects string, mixed given\.$#'
identifier: argument.type
count: 1
path: src/Config/UserPreferencesHandler.php
-
message: '#^Parameter \#2 \.\.\.\$replacements of function array_replace_recursive expects array, mixed given\.$#'
identifier: argument.type
count: 1
path: src/Config/UserPreferencesHandler.php
-
message: '#^Parameter \#3 \$default of method PhpMyAdmin\\Config\:\:setCookie\(\) expects string\|null, mixed given\.$#'
identifier: argument.type

View File

@ -408,7 +408,6 @@
</file>
<file src="src/Config/UserPreferencesHandler.php">
<MixedArgument>
<code><![CDATA[$configData]]></code>
<code><![CDATA[$configData['lang']]]></code>
<code><![CDATA[$defaultValue]]></code>
</MixedArgument>
@ -416,7 +415,6 @@
<code><![CDATA[$_SESSION['cache'][$cacheKey]['userprefs']]]></code>
<code><![CDATA[$_SESSION['cache'][$cacheKey]['userprefs']['LoginCookieValidity']]]></code>
<code><![CDATA[$_SESSION['cache'][$cacheKey]['userprefs_type']]]></code>
<code><![CDATA[$configData['lang']]]></code>
</MixedArrayAccess>
<MixedArrayAssignment>
<code><![CDATA[$_SESSION['cache'][$cacheKey]]]></code>
@ -427,7 +425,6 @@
<code><![CDATA[$_SESSION['cache'][$cacheKey]['userprefs_type']]]></code>
</MixedArrayAssignment>
<MixedAssignment>
<code><![CDATA[$configData]]></code>
<code><![CDATA[$defaultValue]]></code>
<code><![CDATA[$defaultValue]]></code>
<code><![CDATA[$this->storageType]]></code>
@ -439,9 +436,6 @@
<PossiblyInvalidArgument>
<code><![CDATA[$defaultValue]]></code>
</PossiblyInvalidArgument>
<RiskyTruthyFalsyComparison>
<code><![CDATA[$cookieName]]></code>
</RiskyTruthyFalsyComparison>
</file>
<file src="src/Config/Validator.php">
<DeprecatedMethod>

View File

@ -307,8 +307,12 @@ class Config
private function setValueRecursive(array &$array, array $parts, mixed $value): bool
{
$part = array_shift($parts);
if (! isset($array[$part])) {
throw new ConfigException('Configuration option "' . $part . '" does not exist.');
}
if ($parts === []) {
if (isset($array[$part]) && $array[$part] === $value) {
if ($array[$part] === $value) {
return false;
}
@ -318,7 +322,7 @@ class Config
}
if (! is_array($array[$part])) {
throw new ConfigException('Failed to set configuration value.');
throw new ConfigException('Configuration option "' . $part . '" is not an array.');
}
return $this->setValueRecursive($array[$part], $parts, $value);

View File

@ -58,6 +58,7 @@ class UserPreferencesHandler
return;
}
/** @var mixed[] $configData */
$configData = $_SESSION['cache'][$cacheKey]['userprefs'];
// type is 'db' or 'session'
$this->storageType = $_SESSION['cache'][$cacheKey]['userprefs_type'];
@ -88,11 +89,11 @@ class UserPreferencesHandler
&& $configData['ThemeDefault'] !== $this->themeManager->theme->getId()
) {
$this->setUserValue(
null,
'ThemeDefault',
$this->themeManager->theme->getId(),
'original',
);
$this->updateConfigValue('ThemeDefault', $this->themeManager->theme->getId());
}
} elseif (
$this->config->config->ThemeDefault !== $this->themeManager->theme->getId()
@ -111,7 +112,7 @@ class UserPreferencesHandler
|| isset($configData['lang'])
&& Current::$lang !== $configData['lang']
) {
$this->setUserValue(null, 'lang', Current::$lang, 'en');
$this->setUserValue('lang', Current::$lang, 'en');
}
} elseif (isset($configData['lang'])) {
// read language from settings
@ -151,17 +152,12 @@ class UserPreferencesHandler
* If user preferences are not yet initialized, option is applied to
* global config and added to a update queue, which is processed
* by {@link loadUserPreferences()}
*
* @param string|null $cookieName can be null
* @param string $cfgPath configuration path
* @param mixed $newCfgValue new value
* @param string|null $defaultValue default value
*/
public function setUserValue(
string|null $cookieName,
string $cfgPath,
mixed $newCfgValue,
string|null $defaultValue = null,
string $cookieName = '',
): true|Message {
$result = true;
// use permanent user preferences if possible
@ -173,7 +169,7 @@ class UserPreferencesHandler
$result = $this->userPreferences->persistOption($cfgPath, $newCfgValue, $defaultValue);
}
if ($this->storageType !== 'db' && $cookieName) {
if ($this->storageType !== 'db' && $cookieName !== '') {
// fall back to cookies
if ($defaultValue === null) {
$defaultValue = Core::arrayRead($cfgPath, $this->config->settings);
@ -182,11 +178,14 @@ class UserPreferencesHandler
$this->config->setCookie($cookieName, (string) $newCfgValue, $defaultValue);
}
$this->config->set($cfgPath, $newCfgValue);
return $result;
}
public function updateConfigValue(string $cfgPath, mixed $newCfgValue): void
{
$this->config->set($cfgPath, $newCfgValue);
}
/**
* Reads value stored by {@link setUserValue()}
*

View File

@ -23,11 +23,14 @@ final readonly class CollationConnectionController implements InvocableControlle
public function __invoke(ServerRequest $request): Response
{
$this->userPreferencesHandler->setUserValue(
null,
'DefaultConnectionCollation',
$request->getParsedBodyParam('collation_connection'),
'utf8mb4_unicode_ci',
);
$this->userPreferencesHandler->updateConfigValue(
'DefaultConnectionCollation',
$request->getParsedBodyParam('collation_connection'),
);
$this->response->redirect('index.php?route=/' . Url::getCommonRaw([], '&'));

View File

@ -39,7 +39,7 @@ final readonly class UpdateConfigController implements InvocableController
return $this->response->response();
}
$result = $this->userPreferencesHandler->setUserValue(null, 'Console/' . $key, $value);
$result = $this->userPreferencesHandler->setUserValue('Console/' . $key, $value);
if ($result !== true) {
$this->response->setStatusCode(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR);
$this->response->setRequestStatus(false);
@ -48,6 +48,8 @@ final readonly class UpdateConfigController implements InvocableController
return $this->response->response();
}
$this->userPreferencesHandler->updateConfigValue('Console/' . $key, $value);
$this->response->addJSON('message', __('Console settings has been updated successfully.'));
return $this->response->response();

View File

@ -34,7 +34,8 @@ final readonly class UpdateNavWidthConfigController implements InvocableControll
return $this->response->response();
}
$result = $this->userPreferencesHandler->setUserValue(null, 'NavigationWidth', (int) $value);
$result = $this->userPreferencesHandler->setUserValue('NavigationWidth', (int) $value);
$this->userPreferencesHandler->updateConfigValue('NavigationWidth', (int) $value);
if ($result === true) {
return $this->response->response();
}

View File

@ -168,27 +168,21 @@ class Export
): void {
if ($exportType === ExportType::Server) {
$userPreferencesHandler->setUserValue(
'pma_server_filename_template',
'Export/file_template_server',
$filenameTemplate,
cookieName: 'pma_server_filename_template',
);
} elseif ($exportType === ExportType::Database) {
$userPreferencesHandler->setUserValue(
'pma_db_filename_template',
'Export/file_template_database',
$filenameTemplate,
cookieName: 'pma_db_filename_template',
);
} elseif ($exportType === ExportType::Raw) {
} elseif ($exportType === ExportType::Table) {
$userPreferencesHandler->setUserValue(
'pma_raw_filename_template',
'Export/file_template_raw',
$filenameTemplate,
);
} else {
$userPreferencesHandler->setUserValue(
'pma_table_filename_template',
'Export/file_template_table',
$filenameTemplate,
cookieName: 'pma_table_filename_template',
);
}
}

View File

@ -29,11 +29,24 @@ final class UserPreferencesHandlerTest extends AbstractTestCase
new LanguageManager($config),
new ThemeManager(),
);
$userPreferencesHandler->setUserValue(null, 'Lang', 'cs', 'en');
$userPreferencesHandler->setUserValue('TEST_COOKIE_USER_VAL', 'Servers/1/hide_db', 'cfg_val_1');
$userPreferencesHandler->setUserValue('Lang', 'cs', 'en');
$userPreferencesHandler->setUserValue('Servers/1/hide_db', 'cfg_val_1', cookieName: 'TEST_COOKIE_USER_VAL');
self::assertSame('cfg_val_1', $userPreferencesHandler->getUserValue('TEST_COOKIE_USER_VAL', 'fail'));
$userPreferencesHandler->setUserValue(null, 'NavigationWidth', 300);
self::assertSame(300, $config->settings['NavigationWidth']);
}
public function testUpdateConfigValue(): void
{
$config = new Config();
$dbi = $this->createDatabaseInterface();
$userPreferencesHandler = new UserPreferencesHandler(
$config,
$dbi,
new UserPreferences($dbi, new Relation($dbi, $config), new Template($config), $config, new Clock()),
new LanguageManager($config),
new ThemeManager(),
);
$userPreferencesHandler->updateConfigValue('NavigationWidth', 300);
self::assertSame(300, $config->config->NavigationWidth);
}
public function testGetUserValue(): void

View File

@ -28,7 +28,9 @@ class CollationConnectionControllerTest extends AbstractTestCase
$userPreferencesHandler = self::createMock(UserPreferencesHandler::class);
$userPreferencesHandler->expects(self::once())->method('setUserValue')
->with(null, 'DefaultConnectionCollation', 'utf8mb4_general_ci', 'utf8mb4_unicode_ci');
->with('DefaultConnectionCollation', 'utf8mb4_general_ci', 'utf8mb4_unicode_ci');
$userPreferencesHandler->expects(self::once())->method('updateConfigValue')
->with('DefaultConnectionCollation', 'utf8mb4_general_ci');
(new CollationConnectionController($response, $userPreferencesHandler))($request);
}