Skip to content

Commit 14084fd

Browse files
Merge pull request #18372 from MauricioFauth/config-check-servers-removal
Remove `Config::checkServers()` method
2 parents 97f1d2f + 5ffe284 commit 14084fd

8 files changed

Lines changed: 45 additions & 241 deletions

File tree

‎libraries/classes/Common.php‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,6 @@ private static function setSQLQueryGlobalFromRequest(ContainerInterface $contain
633633

634634
private static function setCurrentServerGlobal(ContainerInterface $container, Config $config): void
635635
{
636-
$config->checkServers();
637636
$server = $config->selectServer();
638637
$GLOBALS['server'] = $server;
639638
$GLOBALS['urlParams']['server'] = $server;

‎libraries/classes/Config.php‎

Lines changed: 5 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
use Throwable;
1212

1313
use function __;
14-
use function array_filter;
1514
use function array_key_last;
16-
use function array_merge;
1715
use function array_replace_recursive;
1816
use function array_slice;
1917
use function count;
@@ -34,7 +32,6 @@
3432
use function is_array;
3533
use function is_bool;
3634
use function is_dir;
37-
use function is_int;
3835
use function is_numeric;
3936
use function is_readable;
4037
use function is_string;
@@ -53,15 +50,13 @@
5350
use function rtrim;
5451
use function setcookie;
5552
use function sprintf;
56-
use function str_contains;
5753
use function stripos;
5854
use function strtolower;
5955
use function substr;
6056
use function sys_get_temp_dir;
6157
use function time;
6258
use function trim;
6359

64-
use const ARRAY_FILTER_USE_KEY;
6560
use const DIRECTORY_SEPARATOR;
6661
use const PHP_OS;
6762
use const PHP_URL_PATH;
@@ -103,7 +98,6 @@ public function __construct()
10398
$this->config = new Settings([]);
10499
$this->defaultServer = $this->config->Servers[1]->asArray();
105100
$config = $this->config->asArray();
106-
unset($config['Servers']);
107101
$this->default = $config;
108102
$this->settings = $config;
109103
$this->baseSettings = $config;
@@ -346,6 +340,7 @@ public function load(string|null $source = null): bool
346340
return false;
347341
}
348342

343+
/** @var mixed $cfg */
349344
$cfg = [];
350345

351346
/**
@@ -379,25 +374,11 @@ public function load(string|null $source = null): bool
379374
$this->sourceMtime = (int) filemtime($this->getSource());
380375
}
381376

382-
/**
383-
* Ignore keys with / as we do not use these
384-
*
385-
* These can be confusing for user configuration layer as it
386-
* flatten array using / and thus don't see difference between
387-
* $cfg['Export/method'] and $cfg['Export']['method'], while rest
388-
* of the code uses the setting only in latter form.
389-
*
390-
* This could be removed once we consistently handle both values
391-
* in the functional code as well.
392-
*/
393-
$cfg = array_filter(
394-
$cfg,
395-
static fn (string $key): bool => ! str_contains($key, '/'),
396-
ARRAY_FILTER_USE_KEY,
397-
);
377+
if (is_array($cfg)) {
378+
$this->config = new Settings($cfg);
379+
}
398380

399-
$this->settings = array_replace_recursive($this->settings, $cfg);
400-
$this->config = new Settings($cfg);
381+
$this->settings = array_replace_recursive($this->settings, $this->config->asArray());
401382

402383
return true;
403384
}
@@ -1138,52 +1119,6 @@ public function selectServer(): int
11381119
return (int) $server;
11391120
}
11401121

1141-
/**
1142-
* Checks whether Servers configuration is valid and possibly apply fixups.
1143-
*/
1144-
public function checkServers(): void
1145-
{
1146-
// Do we have some server?
1147-
if (! isset($this->settings['Servers']) || count($this->settings['Servers']) === 0) {
1148-
// No server => create one with defaults
1149-
$this->settings['Servers'] = [1 => $this->defaultServer];
1150-
$this->config = new Settings($this->settings);
1151-
1152-
return;
1153-
}
1154-
1155-
// We have server(s) => apply default configuration
1156-
$newServers = [];
1157-
1158-
foreach ($this->settings['Servers'] as $serverIndex => $server) {
1159-
// Detect wrong configuration
1160-
if (! is_int($serverIndex) || $serverIndex < 1 || ! is_array($server)) {
1161-
continue;
1162-
}
1163-
1164-
$server = array_merge($this->defaultServer, $server);
1165-
1166-
// Final solution to bug #582890
1167-
// If we are using a socket connection
1168-
// and there is nothing in the verbose server name
1169-
// or the host field, then generate a name for the server
1170-
// in the form of "Server 2", localized of course!
1171-
if (empty($server['host']) && empty($server['verbose'])) {
1172-
$server['verbose'] = sprintf(__('Server %d'), $serverIndex);
1173-
}
1174-
1175-
$newServers[$serverIndex] = $server;
1176-
}
1177-
1178-
if ($newServers === []) {
1179-
// Ensures it has at least one valid server config.
1180-
$newServers = [1 => $this->defaultServer];
1181-
}
1182-
1183-
$this->settings['Servers'] = $newServers;
1184-
$this->config = new Settings($this->settings);
1185-
}
1186-
11871122
/**
11881123
* Return connection parameters for the database server
11891124
*

‎libraries/classes/Config/Settings.php‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpMyAdmin\Config\Settings\SqlQueryBox;
1414
use PhpMyAdmin\Config\Settings\Transformations;
1515

16+
use function __;
1617
use function array_map;
1718
use function count;
1819
use function defined;
@@ -21,6 +22,7 @@
2122
use function is_int;
2223
use function is_string;
2324
use function min;
25+
use function sprintf;
2426
use function strlen;
2527

2628
use const DIRECTORY_SEPARATOR;
@@ -2929,19 +2931,28 @@ private function setServers(array $settings): array
29292931
}
29302932

29312933
$servers = [];
2932-
/**
2933-
* @var int|string $key
2934-
* @var mixed $server
2935-
*/
29362934
foreach ($settings['Servers'] as $key => $server) {
29372935
if (! is_int($key) || $key < 1 || ! is_array($server)) {
29382936
continue;
29392937
}
29402938

29412939
$servers[$key] = new Server($server);
2940+
if ($servers[$key]->host !== '' || $servers[$key]->verbose !== '') {
2941+
continue;
2942+
}
2943+
2944+
/**
2945+
* Ensures that the database server has a name.
2946+
*
2947+
* @link https://github.com/phpmyadmin/phpmyadmin/issues/6878
2948+
*
2949+
* @psalm-suppress ImpureFunctionCall
2950+
*/
2951+
$server['verbose'] = sprintf(__('Server %d'), $key);
2952+
$servers[$key] = new Server($server);
29422953
}
29432954

2944-
if (count($servers) === 0) {
2955+
if ($servers === []) {
29452956
return [1 => new Server()];
29462957
}
29472958

‎phpstan-baseline.neon‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ parameters:
162162

163163
-
164164
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
165-
count: 2
165+
count: 1
166166
path: libraries/classes/Config.php
167167

168168
-
@@ -215,11 +215,6 @@ parameters:
215215
count: 2
216216
path: libraries/classes/Config.php
217217

218-
-
219-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
220-
count: 1
221-
path: libraries/classes/Config.php
222-
223218
-
224219
message: "#^Parameter \\#3 \\$default of method PhpMyAdmin\\\\Config\\:\\:setCookie\\(\\) expects string\\|null, mixed given\\.$#"
225220
count: 1
@@ -9185,26 +9180,11 @@ parameters:
91859180
count: 1
91869181
path: test/classes/Config/SettingsTest.php
91879182

9188-
-
9189-
message: "#^Cannot access offset 1 on mixed\\.$#"
9190-
count: 1
9191-
path: test/classes/ConfigTest.php
9192-
91939183
-
91949184
message: "#^Parameter \\#1 \\$haystack of function mb_strstr expects string, array\\<string\\> given\\.$#"
91959185
count: 1
91969186
path: test/classes/ConfigTest.php
91979187

9198-
-
9199-
message: "#^Parameter \\#2 \\$array of method PHPUnit\\\\Framework\\\\Assert\\:\\:assertArrayHasKey\\(\\) expects array\\|ArrayAccess, mixed given\\.$#"
9200-
count: 3
9201-
path: test/classes/ConfigTest.php
9202-
9203-
-
9204-
message: "#^Parameter \\#2 \\$array of method PHPUnit\\\\Framework\\\\Assert\\:\\:assertArrayNotHasKey\\(\\) expects array\\|ArrayAccess, mixed given\\.$#"
9205-
count: 7
9206-
path: test/classes/ConfigTest.php
9207-
92089188
-
92099189
message: "#^Cannot access offset 'favoriteTables' on mixed\\.$#"
92109190
count: 1

‎psalm-baseline.xml‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@
261261
<code><![CDATA[$gdInfo['GD Version']]]></code>
262262
<code>$path</code>
263263
<code><![CDATA[$server['verbose']]]></code>
264-
<code><![CDATA[$this->settings['Servers']]]></code>
265264
<code><![CDATA[$this->settings['ThemeDefault']]]></code>
266265
<code><![CDATA[$this->settings['ThemeDefault']]]></code>
267266
<code>$url</code>
@@ -14157,8 +14156,6 @@
1415714156
<code>mixed[]</code>
1415814157
<code>mixed[]</code>
1415914158
<code>mixed[]</code>
14160-
<code>mixed[]</code>
14161-
<code>mixed[]</code>
1416214159
</MixedInferredReturnType>
1416314160
</file>
1416414161
<file src="test/classes/Controllers/Database/Structure/FavoriteTableControllerTest.php">

‎test/classes/AbstractTestCase.php‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ protected function createConfig(): Config
167167
protected function setGlobalConfig(): void
168168
{
169169
$GLOBALS['config'] = $this->createConfig();
170-
$GLOBALS['config']->checkServers();
171170
$GLOBALS['config']->set('environment', 'development');
172171
$GLOBALS['cfg'] = $GLOBALS['config']->settings;
173172
}

‎test/classes/Config/SettingsTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,11 @@ public static function valuesForServersProvider(): iterable
11721172
yield 'null value' => [null, [1 => $server]];
11731173
yield 'valid value' => [[1 => [], 2 => []], [1 => $server, 2 => $server]];
11741174
yield 'valid value 2' => [[2 => ['host' => 'test']], [2 => new Server(['host' => 'test'])]];
1175+
yield 'valid value 3' => [
1176+
[4 => ['host' => '', 'verbose' => '']],
1177+
[4 => new Server(['host' => '', 'verbose' => 'Server 4'])],
1178+
];
1179+
11751180
yield 'invalid value' => ['invalid', [1 => $server]];
11761181
yield 'invalid value 2' => [[0 => [], 2 => 'invalid', 'invalid' => [], 4 => []], [4 => $server]];
11771182
yield 'invalid value 3' => [[0 => []], [1 => $server]];

0 commit comments

Comments
 (0)