Skip to content

Commit 8f0fa82

Browse files
Merge pull request #20147 from kamil-tekiela/Clean-up-setUserValue
Clean up setUserValue
2 parents e2bc20e + 2dc32a9 commit 8f0fa82

10 files changed

Lines changed: 49 additions & 67 deletions

phpstan-baseline.neon

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -921,30 +921,12 @@ parameters:
921921
count: 1
922922
path: src/Config/UserPreferencesHandler.php
923923

924-
-
925-
message: '#^Cannot access offset ''Server'' on mixed\.$#'
926-
identifier: offsetAccess.nonOffsetAccessible
927-
count: 1
928-
path: src/Config/UserPreferencesHandler.php
929-
930-
-
931-
message: '#^Cannot access offset ''ThemeDefault'' on mixed\.$#'
932-
identifier: offsetAccess.nonOffsetAccessible
933-
count: 2
934-
path: src/Config/UserPreferencesHandler.php
935-
936924
-
937925
message: '#^Cannot access offset ''config_mtime'' on mixed\.$#'
938926
identifier: offsetAccess.nonOffsetAccessible
939927
count: 2
940928
path: src/Config/UserPreferencesHandler.php
941929

942-
-
943-
message: '#^Cannot access offset ''lang'' on mixed\.$#'
944-
identifier: offsetAccess.nonOffsetAccessible
945-
count: 3
946-
path: src/Config/UserPreferencesHandler.php
947-
948930
-
949931
message: '#^Cannot access offset ''userprefs'' on mixed\.$#'
950932
identifier: offsetAccess.nonOffsetAccessible
@@ -975,24 +957,12 @@ parameters:
975957
count: 1
976958
path: src/Config/UserPreferencesHandler.php
977959

978-
-
979-
message: '#^Only booleans are allowed in &&, string\|null given on the right side\.$#'
980-
identifier: booleanAnd.rightNotBoolean
981-
count: 1
982-
path: src/Config/UserPreferencesHandler.php
983-
984960
-
985961
message: '#^Parameter \#1 \$code of method PhpMyAdmin\\I18n\\LanguageManager\:\:getLanguage\(\) expects string, mixed given\.$#'
986962
identifier: argument.type
987963
count: 1
988964
path: src/Config/UserPreferencesHandler.php
989965

990-
-
991-
message: '#^Parameter \#2 \.\.\.\$replacements of function array_replace_recursive expects array, mixed given\.$#'
992-
identifier: argument.type
993-
count: 1
994-
path: src/Config/UserPreferencesHandler.php
995-
996966
-
997967
message: '#^Parameter \#3 \$default of method PhpMyAdmin\\Config\:\:setCookie\(\) expects string\|null, mixed given\.$#'
998968
identifier: argument.type

psalm-baseline.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,15 +408,13 @@
408408
</file>
409409
<file src="src/Config/UserPreferencesHandler.php">
410410
<MixedArgument>
411-
<code><![CDATA[$configData]]></code>
412411
<code><![CDATA[$configData['lang']]]></code>
413412
<code><![CDATA[$defaultValue]]></code>
414413
</MixedArgument>
415414
<MixedArrayAccess>
416415
<code><![CDATA[$_SESSION['cache'][$cacheKey]['userprefs']]]></code>
417416
<code><![CDATA[$_SESSION['cache'][$cacheKey]['userprefs']['LoginCookieValidity']]]></code>
418417
<code><![CDATA[$_SESSION['cache'][$cacheKey]['userprefs_type']]]></code>
419-
<code><![CDATA[$configData['lang']]]></code>
420418
</MixedArrayAccess>
421419
<MixedArrayAssignment>
422420
<code><![CDATA[$_SESSION['cache'][$cacheKey]]]></code>
@@ -427,7 +425,6 @@
427425
<code><![CDATA[$_SESSION['cache'][$cacheKey]['userprefs_type']]]></code>
428426
</MixedArrayAssignment>
429427
<MixedAssignment>
430-
<code><![CDATA[$configData]]></code>
431428
<code><![CDATA[$defaultValue]]></code>
432429
<code><![CDATA[$defaultValue]]></code>
433430
<code><![CDATA[$this->storageType]]></code>
@@ -439,9 +436,6 @@
439436
<PossiblyInvalidArgument>
440437
<code><![CDATA[$defaultValue]]></code>
441438
</PossiblyInvalidArgument>
442-
<RiskyTruthyFalsyComparison>
443-
<code><![CDATA[$cookieName]]></code>
444-
</RiskyTruthyFalsyComparison>
445439
</file>
446440
<file src="src/Config/Validator.php">
447441
<DeprecatedMethod>

src/Config.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,12 @@ public function set(string $setting, mixed $value): void
307307
private function setValueRecursive(array &$array, array $parts, mixed $value): bool
308308
{
309309
$part = array_shift($parts);
310+
if (! isset($array[$part])) {
311+
throw new ConfigException('Configuration option "' . $part . '" does not exist.');
312+
}
313+
310314
if ($parts === []) {
311-
if (isset($array[$part]) && $array[$part] === $value) {
315+
if ($array[$part] === $value) {
312316
return false;
313317
}
314318

@@ -318,7 +322,7 @@ private function setValueRecursive(array &$array, array $parts, mixed $value): b
318322
}
319323

320324
if (! is_array($array[$part])) {
321-
throw new ConfigException('Failed to set configuration value.');
325+
throw new ConfigException('Configuration option "' . $part . '" is not an array.');
322326
}
323327

324328
return $this->setValueRecursive($array[$part], $parts, $value);

src/Config/UserPreferencesHandler.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function loadUserPreferences(bool $isMinimumCommon = false): void
5858
return;
5959
}
6060

61+
/** @var mixed[] $configData */
6162
$configData = $_SESSION['cache'][$cacheKey]['userprefs'];
6263
// type is 'db' or 'session'
6364
$this->storageType = $_SESSION['cache'][$cacheKey]['userprefs_type'];
@@ -88,11 +89,11 @@ public function loadUserPreferences(bool $isMinimumCommon = false): void
8889
&& $configData['ThemeDefault'] !== $this->themeManager->theme->getId()
8990
) {
9091
$this->setUserValue(
91-
null,
9292
'ThemeDefault',
9393
$this->themeManager->theme->getId(),
9494
'original',
9595
);
96+
$this->updateConfigValue('ThemeDefault', $this->themeManager->theme->getId());
9697
}
9798
} elseif (
9899
$this->config->config->ThemeDefault !== $this->themeManager->theme->getId()
@@ -111,7 +112,7 @@ public function loadUserPreferences(bool $isMinimumCommon = false): void
111112
|| isset($configData['lang'])
112113
&& Current::$lang !== $configData['lang']
113114
) {
114-
$this->setUserValue(null, 'lang', Current::$lang, 'en');
115+
$this->setUserValue('lang', Current::$lang, 'en');
115116
}
116117
} elseif (isset($configData['lang'])) {
117118
// read language from settings
@@ -151,17 +152,12 @@ public function getLoginCookieValidityFromCache(int $server): void
151152
* If user preferences are not yet initialized, option is applied to
152153
* global config and added to a update queue, which is processed
153154
* by {@link loadUserPreferences()}
154-
*
155-
* @param string|null $cookieName can be null
156-
* @param string $cfgPath configuration path
157-
* @param mixed $newCfgValue new value
158-
* @param string|null $defaultValue default value
159155
*/
160156
public function setUserValue(
161-
string|null $cookieName,
162157
string $cfgPath,
163158
mixed $newCfgValue,
164159
string|null $defaultValue = null,
160+
string $cookieName = '',
165161
): true|Message {
166162
$result = true;
167163
// use permanent user preferences if possible
@@ -173,7 +169,7 @@ public function setUserValue(
173169
$result = $this->userPreferences->persistOption($cfgPath, $newCfgValue, $defaultValue);
174170
}
175171

176-
if ($this->storageType !== 'db' && $cookieName) {
172+
if ($this->storageType !== 'db' && $cookieName !== '') {
177173
// fall back to cookies
178174
if ($defaultValue === null) {
179175
$defaultValue = Core::arrayRead($cfgPath, $this->config->settings);
@@ -182,11 +178,14 @@ public function setUserValue(
182178
$this->config->setCookie($cookieName, (string) $newCfgValue, $defaultValue);
183179
}
184180

185-
$this->config->set($cfgPath, $newCfgValue);
186-
187181
return $result;
188182
}
189183

184+
public function updateConfigValue(string $cfgPath, mixed $newCfgValue): void
185+
{
186+
$this->config->set($cfgPath, $newCfgValue);
187+
}
188+
190189
/**
191190
* Reads value stored by {@link setUserValue()}
192191
*

src/Controllers/CollationConnectionController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ public function __construct(
2323
public function __invoke(ServerRequest $request): Response
2424
{
2525
$this->userPreferencesHandler->setUserValue(
26-
null,
2726
'DefaultConnectionCollation',
2827
$request->getParsedBodyParam('collation_connection'),
2928
'utf8mb4_unicode_ci',
3029
);
30+
$this->userPreferencesHandler->updateConfigValue(
31+
'DefaultConnectionCollation',
32+
$request->getParsedBodyParam('collation_connection'),
33+
);
3134

3235
$this->response->redirect('index.php?route=/' . Url::getCommonRaw([], '&'));
3336

src/Controllers/Console/UpdateConfigController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __invoke(ServerRequest $request): Response
3939
return $this->response->response();
4040
}
4141

42-
$result = $this->userPreferencesHandler->setUserValue(null, 'Console/' . $key, $value);
42+
$result = $this->userPreferencesHandler->setUserValue('Console/' . $key, $value);
4343
if ($result !== true) {
4444
$this->response->setStatusCode(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR);
4545
$this->response->setRequestStatus(false);
@@ -48,6 +48,8 @@ public function __invoke(ServerRequest $request): Response
4848
return $this->response->response();
4949
}
5050

51+
$this->userPreferencesHandler->updateConfigValue('Console/' . $key, $value);
52+
5153
$this->response->addJSON('message', __('Console settings has been updated successfully.'));
5254

5355
return $this->response->response();

src/Controllers/Navigation/UpdateNavWidthConfigController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public function __invoke(ServerRequest $request): Response
3434
return $this->response->response();
3535
}
3636

37-
$result = $this->userPreferencesHandler->setUserValue(null, 'NavigationWidth', (int) $value);
37+
$result = $this->userPreferencesHandler->setUserValue('NavigationWidth', (int) $value);
38+
$this->userPreferencesHandler->updateConfigValue('NavigationWidth', (int) $value);
3839
if ($result === true) {
3940
return $this->response->response();
4041
}

src/Export/Export.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,27 +168,21 @@ public function rememberFilename(
168168
): void {
169169
if ($exportType === ExportType::Server) {
170170
$userPreferencesHandler->setUserValue(
171-
'pma_server_filename_template',
172171
'Export/file_template_server',
173172
$filenameTemplate,
173+
cookieName: 'pma_server_filename_template',
174174
);
175175
} elseif ($exportType === ExportType::Database) {
176176
$userPreferencesHandler->setUserValue(
177-
'pma_db_filename_template',
178177
'Export/file_template_database',
179178
$filenameTemplate,
179+
cookieName: 'pma_db_filename_template',
180180
);
181-
} elseif ($exportType === ExportType::Raw) {
181+
} elseif ($exportType === ExportType::Table) {
182182
$userPreferencesHandler->setUserValue(
183-
'pma_raw_filename_template',
184-
'Export/file_template_raw',
185-
$filenameTemplate,
186-
);
187-
} else {
188-
$userPreferencesHandler->setUserValue(
189-
'pma_table_filename_template',
190183
'Export/file_template_table',
191184
$filenameTemplate,
185+
cookieName: 'pma_table_filename_template',
192186
);
193187
}
194188
}

tests/unit/Config/UserPreferencesHandlerTest.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,24 @@ public function testSetUserValue(): void
2929
new LanguageManager($config),
3030
new ThemeManager(),
3131
);
32-
$userPreferencesHandler->setUserValue(null, 'Lang', 'cs', 'en');
33-
$userPreferencesHandler->setUserValue('TEST_COOKIE_USER_VAL', 'Servers/1/hide_db', 'cfg_val_1');
32+
$userPreferencesHandler->setUserValue('Lang', 'cs', 'en');
33+
$userPreferencesHandler->setUserValue('Servers/1/hide_db', 'cfg_val_1', cookieName: 'TEST_COOKIE_USER_VAL');
3434
self::assertSame('cfg_val_1', $userPreferencesHandler->getUserValue('TEST_COOKIE_USER_VAL', 'fail'));
35-
$userPreferencesHandler->setUserValue(null, 'NavigationWidth', 300);
36-
self::assertSame(300, $config->settings['NavigationWidth']);
35+
}
36+
37+
public function testUpdateConfigValue(): void
38+
{
39+
$config = new Config();
40+
$dbi = $this->createDatabaseInterface();
41+
$userPreferencesHandler = new UserPreferencesHandler(
42+
$config,
43+
$dbi,
44+
new UserPreferences($dbi, new Relation($dbi, $config), new Template($config), $config, new Clock()),
45+
new LanguageManager($config),
46+
new ThemeManager(),
47+
);
48+
$userPreferencesHandler->updateConfigValue('NavigationWidth', 300);
49+
self::assertSame(300, $config->config->NavigationWidth);
3750
}
3851

3952
public function testGetUserValue(): void

tests/unit/Controllers/CollationConnectionControllerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public function testInvoke(): void
2828

2929
$userPreferencesHandler = self::createMock(UserPreferencesHandler::class);
3030
$userPreferencesHandler->expects(self::once())->method('setUserValue')
31-
->with(null, 'DefaultConnectionCollation', 'utf8mb4_general_ci', 'utf8mb4_unicode_ci');
31+
->with('DefaultConnectionCollation', 'utf8mb4_general_ci', 'utf8mb4_unicode_ci');
32+
$userPreferencesHandler->expects(self::once())->method('updateConfigValue')
33+
->with('DefaultConnectionCollation', 'utf8mb4_general_ci');
3234

3335
(new CollationConnectionController($response, $userPreferencesHandler))($request);
3436
}

0 commit comments

Comments
 (0)