Extract dependencies from Util::expandUserString()

Uses DI instead.

Signed-off-by: Maurício Meneghini Fauth <mauricio@mfauth.net>
This commit is contained in:
Maurício Meneghini Fauth 2026-01-30 15:21:07 -03:00
parent 1ef1fb8095
commit 21ae1694a6
No known key found for this signature in database
GPG Key ID: 6A16FD38AFC89CC8
10 changed files with 47 additions and 33 deletions

View File

@ -377,6 +377,7 @@ return [
ResponseFactory::class,
Config::class,
UserPreferencesHandler::class,
DatabaseInterface::class,
],
],
Export\TablesController::class => [

View File

@ -13134,7 +13134,7 @@ parameters:
Use dependency injection instead\.$#
'''
identifier: staticMethod.deprecated
count: 6
count: 5
path: src/Util.php
-
@ -13143,7 +13143,7 @@ parameters:
Use dependency injection instead\.$#
'''
identifier: staticMethod.deprecated
count: 6
count: 5
path: src/Util.php
-
@ -16941,7 +16941,7 @@ parameters:
Use dependency injection instead\.$#
'''
identifier: staticMethod.deprecated
count: 6
count: 5
path: tests/unit/UtilTest.php
-

View File

@ -8527,8 +8527,6 @@
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
@ -10869,7 +10867,6 @@
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
<InvalidPropertyAssignmentValue>
<code><![CDATA[Config::getInstance()->selectedServer]]></code>

View File

@ -11,6 +11,7 @@ use PhpMyAdmin\Controllers\Database\ExportController as DatabaseExportController
use PhpMyAdmin\Controllers\InvocableController;
use PhpMyAdmin\Core;
use PhpMyAdmin\Current;
use PhpMyAdmin\Dbal\DatabaseInterface;
use PhpMyAdmin\Encoding;
use PhpMyAdmin\Exceptions\ExportException;
use PhpMyAdmin\Export\Export;
@ -50,6 +51,7 @@ final readonly class ExportController implements InvocableController
private ResponseFactory $responseFactory,
private Config $config,
private UserPreferencesHandler $userPreferencesHandler,
private DatabaseInterface $dbi,
) {
}
@ -235,7 +237,7 @@ final readonly class ExportController implements InvocableController
$filename = $this->export->getFinalFilename(
$exportPlugin,
Sanitize::sanitizeFilename(Util::expandUserString($filenameTemplate), true),
Sanitize::sanitizeFilename(Util::expandUserString($this->dbi, $this->config, $filenameTemplate), true),
);
$mimeType = $this->export->getMimeType($exportPlugin);

View File

@ -421,9 +421,7 @@ class Header
$tempTitle = $this->config->config->TitleDefault;
}
$this->title = htmlspecialchars(
Util::expandUserString($tempTitle),
);
$this->title = htmlspecialchars(Util::expandUserString($this->dbi, $this->config, $tempTitle));
} else {
$this->title = 'phpMyAdmin';
}

View File

@ -294,12 +294,16 @@ class ExportLatex extends ExportPlugin
if ($this->caption) {
$buffer .= ' \\caption{'
. Util::expandUserString(
$this->dbi,
$this->config,
$this->dataCaption,
[static::class, 'texEscape'],
static::texEscape(...),
['table' => $tableAlias, 'database' => $dbAlias],
)
. '} \\label{'
. Util::expandUserString(
$this->dbi,
$this->config,
$this->dataLabel,
null,
['table' => $tableAlias, 'database' => $dbAlias],
@ -324,8 +328,10 @@ class ExportLatex extends ExportPlugin
$this->outputHandler->addLine(
'\\caption{'
. Util::expandUserString(
$this->dbi,
$this->config,
$this->dataContinuedCaption,
[static::class, 'texEscape'],
static::texEscape(...),
['table' => $tableAlias, 'database' => $dbAlias],
)
. '} \\\\ ',
@ -468,12 +474,16 @@ class ExportLatex extends ExportPlugin
if ($this->caption) {
$buffer .= ' \\caption{'
. Util::expandUserString(
$this->dbi,
$this->config,
$this->structureCaption,
[static::class, 'texEscape'],
static::texEscape(...),
['table' => $tableAlias, 'database' => $dbAlias],
)
. '} \\label{'
. Util::expandUserString(
$this->dbi,
$this->config,
$this->structureLabel,
null,
['table' => $tableAlias, 'database' => $dbAlias],
@ -487,8 +497,10 @@ class ExportLatex extends ExportPlugin
if ($this->caption) {
$buffer .= ' \\caption{'
. Util::expandUserString(
$this->dbi,
$this->config,
$this->structureContinuedCaption,
[static::class, 'texEscape'],
static::texEscape(...),
['table' => $tableAlias, 'database' => $dbAlias],
)
. '} \\\\ ' . "\n";

View File

@ -159,7 +159,12 @@ class SqlQueryForm
$tmpDbLink .= htmlspecialchars($db) . '</a>';
$legend = sprintf(__('Run SQL query/queries on database %s'), $tmpDbLink);
if ($query === '') {
$query = Util::expandUserString($this->config->settings['DefaultQueryDatabase'], Util::backquote(...));
$query = Util::expandUserString(
$this->dbi,
$this->config,
$this->config->settings['DefaultQueryDatabase'],
Util::backquote(...),
);
}
} else {
$db = Current::$database;
@ -174,7 +179,12 @@ class SqlQueryForm
$tmpTblLink .= htmlspecialchars($db) . '.' . htmlspecialchars($table) . '</a>';
$legend = sprintf(__('Run SQL query/queries on table %s'), $tmpTblLink);
if ($query === '') {
$query = Util::expandUserString($this->config->settings['DefaultQueryTable'], Util::backquote(...));
$query = Util::expandUserString(
$this->dbi,
$this->config,
$this->config->settings['DefaultQueryTable'],
Util::backquote(...),
);
}
}

View File

@ -1079,6 +1079,8 @@ class Util
* @psalm-param callable(string):string|null $escape
*/
public static function expandUserString(
DatabaseInterface $dbi,
Config $config,
string $string,
callable|null $escape = null,
array $updates = [],
@ -1086,7 +1088,6 @@ class Util
/* Content */
$vars = [];
$vars['http_host'] = Core::getEnv('HTTP_HOST');
$config = Config::getInstance();
$vars['server_name'] = $config->selectedServer['host'];
$vars['server_verbose'] = $config->selectedServer['verbose'];
@ -1135,7 +1136,7 @@ class Util
/* Fetch columns list if required */
if (str_contains($string, '@COLUMNS@')) {
$columnsList = DatabaseInterface::getInstance()->getColumnNames(Current::$database, Current::$table);
$columnsList = $dbi->getColumnNames(Current::$database, Current::$table);
$columnNames = [];
if ($escape !== null) {

View File

@ -210,6 +210,7 @@ final class ExportControllerTest extends AbstractTestCase
ResponseFactory::create(),
$config,
$userPreferencesHandler,
$dbi,
);
$response = $exportController($request);
$output = $this->getActualOutputForAssertion();
@ -384,6 +385,7 @@ final class ExportControllerTest extends AbstractTestCase
ResponseFactory::create(),
$config,
$userPreferencesHandler,
$dbi,
);
$response = $exportController($request);
$output = $this->getActualOutputForAssertion();
@ -547,6 +549,7 @@ final class ExportControllerTest extends AbstractTestCase
ResponseFactory::create(),
$config,
$userPreferencesHandler,
$dbi,
);
$response = $exportController($request);
@ -714,6 +717,7 @@ final class ExportControllerTest extends AbstractTestCase
ResponseFactory::create(),
$config,
$userPreferencesHandler,
$dbi,
);
$response = $exportController($request);

View File

@ -160,26 +160,15 @@ class UtilTest extends AbstractTestCase
#[DataProvider('providerExpandUserString')]
public function testExpandUserString(string $in, string $out): void
{
$this->setGlobalConfig();
$config = Config::getInstance();
$config = new Config();
$config->selectedServer['host'] = 'host&';
$config->selectedServer['verbose'] = 'verbose';
Current::$database = 'database';
Current::$table = 'table';
$dbi = $this->createDatabaseInterface();
self::assertSame(
$out,
Util::expandUserString($in),
);
self::assertSame(
htmlspecialchars($out),
Util::expandUserString(
$in,
'htmlspecialchars',
),
);
self::assertSame($out, Util::expandUserString($dbi, $config, $in));
self::assertSame(htmlspecialchars($out), Util::expandUserString($dbi, $config, $in, htmlspecialchars(...)));
}
/**