Merge pull request #19494 from kamil-tekiela/Refactor-ThemeManager

Refactor ThemeManager
This commit is contained in:
Maurício Meneghini Fauth 2025-01-04 05:24:42 -03:00 committed by GitHub
commit 1ddddeb667
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 36 additions and 108 deletions

View File

@ -627,12 +627,6 @@ parameters:
count: 2
path: src/Config.php
-
message: '#^Only booleans are allowed in \|\|, string\|false given on the left side\.$#'
identifier: booleanOr.leftNotBoolean
count: 1
path: src/Config.php
-
message: '#^Parameter \#1 \$code of method PhpMyAdmin\\I18n\\LanguageManager\:\:getLanguage\(\) expects string, mixed given\.$#'
identifier: argument.type
@ -664,13 +658,13 @@ parameters:
path: src/Config.php
-
message: '#^Parameter \#1 \$theme of method PhpMyAdmin\\Theme\\ThemeManager\:\:checkTheme\(\) expects string\|null, mixed given\.$#'
message: '#^Parameter \#1 \$theme of method PhpMyAdmin\\Theme\\ThemeManager\:\:setActiveTheme\(\) expects string, mixed given\.$#'
identifier: argument.type
count: 1
path: src/Config.php
-
message: '#^Parameter \#1 \$theme of method PhpMyAdmin\\Theme\\ThemeManager\:\:setActiveTheme\(\) expects string\|null, mixed given\.$#'
message: '#^Parameter \#1 \$theme of method PhpMyAdmin\\Theme\\ThemeManager\:\:themeExists\(\) expects string, mixed given\.$#'
identifier: argument.type
count: 1
path: src/Config.php
@ -3292,13 +3286,13 @@ parameters:
path: src/Controllers/Preferences/ManageController.php
-
message: '#^Parameter \#1 \$theme of method PhpMyAdmin\\Theme\\ThemeManager\:\:checkTheme\(\) expects string\|null, mixed given\.$#'
message: '#^Parameter \#1 \$theme of method PhpMyAdmin\\Theme\\ThemeManager\:\:setActiveTheme\(\) expects string, mixed given\.$#'
identifier: argument.type
count: 1
path: src/Controllers/Preferences/ManageController.php
-
message: '#^Parameter \#1 \$theme of method PhpMyAdmin\\Theme\\ThemeManager\:\:setActiveTheme\(\) expects string\|null, mixed given\.$#'
message: '#^Parameter \#1 \$theme of method PhpMyAdmin\\Theme\\ThemeManager\:\:themeExists\(\) expects string, mixed given\.$#'
identifier: argument.type
count: 1
path: src/Controllers/Preferences/ManageController.php
@ -16690,33 +16684,9 @@ parameters:
path: src/Theme/ThemeManager.php
-
message: '#^Left side of && is always true\.$#'
identifier: booleanAnd.leftAlwaysTrue
count: 1
path: src/Theme/ThemeManager.php
-
message: '#^Method PhpMyAdmin\\Theme\\ThemeManager\:\:getColorModeCookie\(\) should return string\|false but returns mixed\.$#'
identifier: return.type
count: 1
path: src/Theme/ThemeManager.php
-
message: '#^Method PhpMyAdmin\\Theme\\ThemeManager\:\:getThemeCookie\(\) should return string\|false but returns mixed\.$#'
identifier: return.type
count: 1
path: src/Theme/ThemeManager.php
-
message: '#^Only booleans are allowed in &&, string\|false given on the left side\.$#'
identifier: booleanAnd.leftNotBoolean
count: 1
path: src/Theme/ThemeManager.php
-
message: '#^Property PhpMyAdmin\\Theme\\ThemeManager\:\:\$activeTheme \(string\) does not accept string\|null\.$#'
identifier: assign.propertyType
count: 1
message: '#^Cannot cast mixed to string\.$#'
identifier: cast.string
count: 2
path: src/Theme/ThemeManager.php
-

View File

@ -243,12 +243,10 @@
<code><![CDATA[$cookieName]]></code>
<code><![CDATA[$postSize]]></code>
<code><![CDATA[$prefsType]]></code>
<code><![CDATA[$themeManager->getThemeCookie()]]></code>
<code><![CDATA[empty($collationConnection)]]></code>
<code><![CDATA[empty($dir)]]></code>
<code><![CDATA[empty($path)]]></code>
<code><![CDATA[empty($path)]]></code>
<code><![CDATA[empty($this->getCookie($cookieName))]]></code>
<code><![CDATA[empty($url)]]></code>
<code><![CDATA[empty($url)]]></code>
<code><![CDATA[ini_get('zlib.output_compression')]]></code>
@ -6610,8 +6608,6 @@
<code><![CDATA[empty($config->settings['CaptchaResponseParam'])]]></code>
<code><![CDATA[empty($config->settings['CaptchaResponseParam'])]]></code>
<code><![CDATA[empty($config->settings['Lang'])]]></code>
<code><![CDATA[empty($serverCookie)]]></code>
<code><![CDATA[empty($serverCookie)]]></code>
<code><![CDATA[ini_get('allow_url_fopen')]]></code>
</RiskyTruthyFalsyComparison>
</file>
@ -9797,32 +9793,6 @@
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
<MixedInferredReturnType>
<code><![CDATA[string|false]]></code>
<code><![CDATA[string|false]]></code>
</MixedInferredReturnType>
<MixedReturnStatement>
<code><![CDATA[$config->getCookie($name)]]></code>
<code><![CDATA[$config->getCookie($name)]]></code>
<code><![CDATA[$config->getCookie($name)]]></code>
<code><![CDATA[$config->getCookie($name)]]></code>
</MixedReturnStatement>
<NullableReturnStatement>
<code><![CDATA[$config->getCookie($name)]]></code>
<code><![CDATA[$config->getCookie($name)]]></code>
</NullableReturnStatement>
<PossiblyNullArrayOffset>
<code><![CDATA[$this->themes]]></code>
</PossiblyNullArrayOffset>
<PossiblyNullPropertyAssignmentValue>
<code><![CDATA[$theme]]></code>
</PossiblyNullPropertyAssignmentValue>
<RedundantCondition>
<code><![CDATA[$configThemeExists]]></code>
</RedundantCondition>
<RiskyTruthyFalsyComparison>
<code><![CDATA[$cookieTheme]]></code>
</RiskyTruthyFalsyComparison>
</file>
<file src="src/Tracking/Tracker.php">
<DeprecatedMethod>
@ -10051,8 +10021,6 @@
</MixedAssignment>
<RiskyTruthyFalsyComparison>
<code><![CDATA[! Config::getInstance()->get('URLQueryEncryption')]]></code>
<code><![CDATA[empty($config->getCookie('pma_lang'))]]></code>
<code><![CDATA[empty($config->getCookie('pma_lang'))]]></code>
</RiskyTruthyFalsyComparison>
</file>
<file src="src/UserPassword.php">

View File

@ -461,7 +461,7 @@ class Config
// in frames
// save theme
if ($themeManager->getThemeCookie() || isset($_REQUEST['set_theme'])) {
if ($themeManager->getThemeCookie() !== '' || isset($_REQUEST['set_theme'])) {
if (
(! isset($configData['ThemeDefault'])
&& $themeManager->theme->getId() !== 'original')
@ -477,7 +477,7 @@ class Config
}
} elseif (
$this->settings['ThemeDefault'] != $themeManager->theme->getId()
&& $themeManager->checkTheme($this->settings['ThemeDefault'])
&& $themeManager->themeExists($this->settings['ThemeDefault'])
) {
// no cookie - read default from settings
$themeManager->setActiveTheme($this->settings['ThemeDefault']);
@ -947,7 +947,7 @@ class Config
*
* @param string $cookieName The name of the cookie to get
*
* @return mixed|null result of getCookie()
* @return mixed result of getCookie()
*/
public function getCookie(string $cookieName): mixed
{

View File

@ -187,7 +187,7 @@ final class ManageController implements InvocableController
if (
isset($configuration['ThemeDefault'])
&& $this->themeManager->theme->getId() != $configuration['ThemeDefault']
&& $this->themeManager->checkTheme($configuration['ThemeDefault'])
&& $this->themeManager->themeExists($configuration['ThemeDefault'])
) {
$this->themeManager->setActiveTheme($configuration['ThemeDefault']);
$this->themeManager->setThemeCookie();

View File

@ -12,7 +12,6 @@ use PhpMyAdmin\Exceptions\MissingTheme;
use function __;
use function array_key_exists;
use function htmlspecialchars;
use function is_string;
use function ksort;
use function sprintf;
@ -30,7 +29,7 @@ class ThemeManager
private string $themesPathUrl;
/** @var array<string,Theme> available themes */
public array $themes = [];
private array $themes = [];
/** @var string cookie name */
public string $cookieName = 'pma_theme';
@ -62,8 +61,7 @@ class ThemeManager
$this->loadThemes();
$configThemeExists = $this->checkTheme($config->settings['ThemeDefault']);
if (! $configThemeExists) {
if (! $this->themeExists($config->settings['ThemeDefault'])) {
throw new MissingTheme(sprintf(
__('Default theme %s not found!'),
htmlspecialchars($config->settings['ThemeDefault']),
@ -74,19 +72,18 @@ class ThemeManager
// check if user have a theme cookie
$cookieTheme = $this->getThemeCookie();
if (
$cookieTheme && $this->setActiveTheme($cookieTheme)
|| $configThemeExists && $this->setActiveTheme($this->themeDefault)
) {
$colorMode = $this->getColorModeCookie();
if (is_string($colorMode) && $colorMode !== '') {
$this->theme->setColorMode($colorMode);
}
if ($cookieTheme !== '') {
$this->setActiveTheme($cookieTheme);
} else {
$this->setActiveTheme($this->themeDefault);
}
$colorMode = $this->getColorModeCookie();
if ($colorMode === '') {
return;
}
$this->setActiveTheme(self::FALLBACK_THEME);
$this->theme->setColorMode($colorMode);
}
/**
@ -102,21 +99,19 @@ class ThemeManager
/**
* Sets active theme
*
* @param string|null $theme theme name
* @param string $theme theme name
*/
public function setActiveTheme(string|null $theme): bool
public function setActiveTheme(string $theme): void
{
if (! $this->checkTheme($theme)) {
if (! $this->themeExists($theme)) {
throw new MissingTheme(sprintf(
__('Theme %s not found!'),
htmlspecialchars((string) $theme),
htmlspecialchars($theme),
));
}
$this->activeTheme = $theme;
$this->theme = $this->themes[$theme];
return true;
}
/**
@ -141,34 +136,30 @@ class ThemeManager
/**
* returns name of theme stored in the cookie
*
* @return string|false theme name from cookie or false
*/
public function getThemeCookie(): string|false
public function getThemeCookie(): string
{
$name = $this->getThemeCookieName();
$config = Config::getInstance();
if ($config->issetCookie($name)) {
return $config->getCookie($name);
return (string) $config->getCookie($name);
}
return false;
return '';
}
/**
* returns name of theme stored in the cookie
*
* @return string|false theme name from cookie or false
*/
public function getColorModeCookie(): string|false
private function getColorModeCookie(): string
{
$name = $this->getColorModeCookieName();
$config = Config::getInstance();
if ($config->issetCookie($name)) {
return $config->getCookie($name);
return (string) $config->getCookie($name);
}
return false;
return '';
}
public function setThemeCookie(): void
@ -220,11 +211,11 @@ class ThemeManager
/**
* checks if given theme name is a known theme
*
* @param string|null $theme name fo theme to check for
* @param string $theme name fo theme to check for
*/
public function checkTheme(string|null $theme): bool
public function themeExists(string $theme): bool
{
return array_key_exists($theme ?? '', $this->themes);
return array_key_exists($theme, $this->themes);
}
/** @return array{

View File

@ -62,9 +62,8 @@ class ThemeManagerTest extends AbstractTestCase
public function testSetThemeCookie(): void
{
$tm = new ThemeManager();
$tm->theme->id = 'theme_id';
$tm->setThemeCookie();
self::assertNotFalse(
$tm->getThemeCookie(),
);
self::assertNotSame('', $tm->getThemeCookie());
}
}