Extract ResponseRenderer dependency from UserPreferences class

Signed-off-by: Maurício Meneghini Fauth <mauricio@mfauth.net>
This commit is contained in:
Maurício Meneghini Fauth 2026-02-14 15:11:19 -03:00
parent 2e1c947d42
commit 25e4dbadea
No known key found for this signature in database
GPG Key ID: 6A16FD38AFC89CC8
11 changed files with 56 additions and 51 deletions

View File

@ -2206,7 +2206,7 @@ parameters:
path: src/Controllers/Preferences/ManageController.php
-
message: '#^Parameter \#1 \$fileName of method PhpMyAdmin\\Config\\UserPreferences\:\:redirect\(\) expects string, string\|false given\.$#'
message: '#^Parameter \#1 \$fileName of method PhpMyAdmin\\Config\\UserPreferences\:\:getUrlToRedirect\(\) expects string, string\|false given\.$#'
identifier: argument.type
count: 1
path: src/Controllers/Preferences/ManageController.php
@ -14202,7 +14202,7 @@ parameters:
Use dependency injection instead\.$#
'''
identifier: staticMethod.deprecated
count: 8
count: 7
path: tests/unit/Config/UserPreferencesTest.php
-
@ -14211,7 +14211,7 @@ parameters:
Use dependency injection instead\.$#
'''
identifier: staticMethod.deprecated
count: 8
count: 7
path: tests/unit/Config/UserPreferencesTest.php
-

View File

@ -8996,8 +8996,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>

View File

@ -11,10 +11,8 @@ use PhpMyAdmin\Core;
use PhpMyAdmin\Current;
use PhpMyAdmin\Dbal\ConnectionType;
use PhpMyAdmin\Dbal\DatabaseInterface;
use PhpMyAdmin\Http\Response;
use PhpMyAdmin\Identifiers\DatabaseName;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;
@ -266,18 +264,16 @@ readonly class UserPreferences
}
/**
* Redirects after saving new user preferences
* Get URL to redirect after saving new user preferences.
*
* @param string $fileName Filename
* @param mixed[]|null $params URL parameters
* @param string|null $hash Hash value
*
* @return non-empty-string
*/
public function redirect(
string $fileName,
array|null $params = null,
string|null $hash = null,
): Response {
// redirect
public function getUrlToRedirect(string $fileName, array|null $params = null, string|null $hash = null): string
{
$urlParams = ['saved' => 1];
if (is_array($params)) {
$urlParams = array_merge($params, $urlParams);
@ -287,12 +283,7 @@ readonly class UserPreferences
$hash = '#' . urlencode($hash);
}
$responseRenderer = ResponseRenderer::getInstance();
$responseRenderer->redirect(
'./' . $fileName . Url::getCommonRaw($urlParams, ! str_contains($fileName, '?') ? '?' : '&') . $hash,
);
return $responseRenderer->response();
return './' . $fileName . Url::getCommonRaw($urlParams, ! str_contains($fileName, '?') ? '?' : '&') . $hash;
}
/**

View File

@ -62,7 +62,11 @@ final readonly class ExportController implements InvocableController
$this->userPreferencesHandler->loadUserPreferences();
$hash = ltrim($request->getParsedBodyParamAsString('tab_hash'), '#');
return $this->userPreferences->redirect('index.php?route=/preferences/export', null, $hash);
$this->response->redirect(
$this->userPreferences->getUrlToRedirect('index.php?route=/preferences/export', null, $hash),
);
return $this->response->response();
}
}

View File

@ -62,7 +62,11 @@ final readonly class FeaturesController implements InvocableController
$this->userPreferencesHandler->loadUserPreferences();
$hash = ltrim($request->getParsedBodyParamAsString('tab_hash'), '#');
return $this->userPreferences->redirect('index.php?route=/preferences/features', null, $hash);
$this->response->redirect(
$this->userPreferences->getUrlToRedirect('index.php?route=/preferences/features', null, $hash),
);
return $this->response->response();
}
}

View File

@ -62,7 +62,11 @@ final readonly class ImportController implements InvocableController
$this->userPreferencesHandler->loadUserPreferences();
$hash = ltrim($request->getParsedBodyParamAsString('tab_hash'), '#');
return $this->userPreferences->redirect('index.php?route=/preferences/import', null, $hash);
$this->response->redirect(
$this->userPreferences->getUrlToRedirect('index.php?route=/preferences/import', null, $hash),
);
return $this->response->response();
}
}

View File

@ -62,7 +62,11 @@ final readonly class MainPanelController implements InvocableController
$this->userPreferencesHandler->loadUserPreferences();
$hash = ltrim($request->getParsedBodyParamAsString('tab_hash'), '#');
return $this->userPreferences->redirect('index.php?route=/preferences/main-panel', null, $hash);
$this->response->redirect(
$this->userPreferences->getUrlToRedirect('index.php?route=/preferences/main-panel', null, $hash),
);
return $this->response->response();
}
}

View File

@ -221,7 +221,11 @@ final readonly class ManageController implements InvocableController
// reload config
$this->userPreferencesHandler->loadUserPreferences();
return $this->userPreferences->redirect($returnUrl ?? '', $redirectParams);
$this->response->redirect(
$this->userPreferences->getUrlToRedirect($returnUrl ?? '', $redirectParams),
);
return $this->response->response();
}
$error = $result;
@ -231,7 +235,11 @@ final readonly class ManageController implements InvocableController
if ($result === true) {
$this->config->removeCookie('pma_lang');
return $this->userPreferences->redirect('index.php?route=/preferences/manage');
$this->response->redirect(
$this->userPreferences->getUrlToRedirect('index.php?route=/preferences/manage'),
);
return $this->response->response();
}
return $this->response->response();

View File

@ -62,7 +62,11 @@ final readonly class NavigationController implements InvocableController
$this->userPreferencesHandler->loadUserPreferences();
$hash = ltrim($request->getParsedBodyParamAsString('tab_hash'), '#');
return $this->userPreferences->redirect('index.php?route=/preferences/navigation', null, $hash);
$this->response->redirect(
$this->userPreferences->getUrlToRedirect('index.php?route=/preferences/navigation', null, $hash),
);
return $this->response->response();
}
}

View File

@ -62,7 +62,11 @@ final readonly class SqlController implements InvocableController
$this->userPreferencesHandler->loadUserPreferences();
$hash = ltrim($request->getParsedBodyParamAsString('tab_hash'), '#');
return $this->userPreferences->redirect('index.php?route=/preferences/sql', null, $hash);
$this->response->redirect(
$this->userPreferences->getUrlToRedirect('index.php?route=/preferences/sql', null, $hash),
);
return $this->response->response();
}
}

View File

@ -14,14 +14,11 @@ use PhpMyAdmin\Current;
use PhpMyAdmin\Dbal\ConnectionType;
use PhpMyAdmin\Dbal\DatabaseInterface;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Tests\Clock\MockClock;
use PhpMyAdmin\Tests\Stubs\DummyResult;
use PhpMyAdmin\Tests\Stubs\ResponseRenderer as ResponseRendererStub;
use PhpMyAdmin\Url;
use PHPUnit\Framework\Attributes\BackupStaticProperties;
use PHPUnit\Framework\Attributes\CoversClass;
use ReflectionProperty;
@ -546,20 +543,11 @@ class UserPreferencesTest extends AbstractTestCase
);
}
#[BackupStaticProperties(true)]
public function testRedirect(): void
public function testGetUrlToRedirect(): void
{
$responseStub = new ResponseRendererStub();
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, $responseStub);
Current::$lang = '';
Current::$database = 'db';
Current::$table = 'table';
$config = Config::getInstance();
$config->set('PmaAbsoluteUri', '');
$dbi = DatabaseInterface::getInstance();
Current::$lang = 'en';
$config = new Config();
$dbi = $this->createDatabaseInterface();
$userPreferences = new UserPreferences(
$dbi,
new Relation($dbi, $config),
@ -567,14 +555,10 @@ class UserPreferencesTest extends AbstractTestCase
$config,
new Clock(),
);
$response = $userPreferences->redirect(
'file.html',
['a' => 'b'],
'h ash',
self::assertSame(
'./file.html?a=b&saved=1&server=2&lang=en#h+ash',
$userPreferences->getUrlToRedirect('file.html', ['a' => 'b'], 'h ash'),
);
self::assertSame(['/phpmyadmin/file.html?a=b&saved=1&server=2#h+ash'], $response->getHeader('Location'));
self::assertSame(302, $response->getStatusCode());
}
/**