Skip to content

Commit

Permalink
Merge pull request phpmyadmin#19494 from kamil-tekiela/Refactor-Theme…
Browse files Browse the repository at this point in the history
…Manager

Refactor ThemeManager
  • Loading branch information
MauricioFauth authored Jan 4, 2025
2 parents 37018db + 4b97cdd commit 1ddddeb
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 108 deletions.
44 changes: 7 additions & 37 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

-
Expand Down
32 changes: 0 additions & 32 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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">
Expand Down
6 changes: 3 additions & 3 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ public function loadUserPreferences(ThemeManager $themeManager, bool $isMinimumC
// 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')
Expand All @@ -477,7 +477,7 @@ public function loadUserPreferences(ThemeManager $themeManager, bool $isMinimumC
}
} 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']);
Expand Down Expand Up @@ -947,7 +947,7 @@ public function setCookie(
*
* @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
{
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/Preferences/ManageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public function __invoke(ServerRequest $request): Response
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();
Expand Down
55 changes: 23 additions & 32 deletions src/Theme/ThemeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use function __;
use function array_key_exists;
use function htmlspecialchars;
use function is_string;
use function ksort;
use function sprintf;

Expand All @@ -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';
Expand Down Expand Up @@ -62,8 +61,7 @@ public function initializeTheme(): void

$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']),
Expand All @@ -74,19 +72,18 @@ public function initializeTheme(): void

// 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);

Check warning on line 78 in src/Theme/ThemeManager.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ if ($cookieTheme !== '') { $this->setActiveTheme($cookieTheme); } else { - $this->setActiveTheme($this->themeDefault); + } $colorMode = $this->getColorModeCookie(); if ($colorMode === '') {
}

$colorMode = $this->getColorModeCookie();
if ($colorMode === '') {

Check warning on line 82 in src/Theme/ThemeManager.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "Identical": --- Original +++ New @@ @@ $this->setActiveTheme($this->themeDefault); } $colorMode = $this->getColorModeCookie(); - if ($colorMode === '') { + if ($colorMode !== '') { return; } $this->theme->setColorMode($colorMode);
return;
}

$this->setActiveTheme(self::FALLBACK_THEME);
$this->theme->setColorMode($colorMode);
}

/**
Expand All @@ -102,21 +99,19 @@ public function setThemePerServer(bool $perServer): void
/**
* 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

Check warning on line 104 in src/Theme/ThemeManager.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "PublicVisibility": --- Original +++ New @@ @@ * * @param string $theme theme name */ - public function setActiveTheme(string $theme) : void + protected function setActiveTheme(string $theme) : void { if (!$this->themeExists($theme)) { throw new MissingTheme(sprintf(__('Theme %s not found!'), htmlspecialchars($theme)));
{
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;
}

/**
Expand All @@ -141,34 +136,30 @@ private function getColorModeCookieName(): string

/**
* 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);

Check warning on line 145 in src/Theme/ThemeManager.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ $name = $this->getThemeCookieName(); $config = Config::getInstance(); if ($config->issetCookie($name)) { - return (string) $config->getCookie($name); + return $config->getCookie($name); } return ''; }
}

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)) {

Check warning on line 158 in src/Theme/ThemeManager.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "IfNegation": --- Original +++ New @@ @@ { $name = $this->getColorModeCookieName(); $config = Config::getInstance(); - if ($config->issetCookie($name)) { + if (!$config->issetCookie($name)) { return (string) $config->getCookie($name); } return '';
return $config->getCookie($name);
return (string) $config->getCookie($name);
}

return false;
return '';
}

public function setThemeCookie(): void
Expand Down Expand Up @@ -220,11 +211,11 @@ public function loadThemes(): void
/**
* 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

Check warning on line 216 in src/Theme/ThemeManager.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "PublicVisibility": --- Original +++ New @@ @@ * * @param string $theme name fo theme to check for */ - public function themeExists(string $theme) : bool + protected function themeExists(string $theme) : bool { return array_key_exists($theme, $this->themes); }
{
return array_key_exists($theme ?? '', $this->themes);
return array_key_exists($theme, $this->themes);
}

/** @return array{
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/Theme/ThemeManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ public function testGetThemesArray(): void
public function testSetThemeCookie(): void
{
$tm = new ThemeManager();
$tm->theme->id = 'theme_id';
$tm->setThemeCookie();
self::assertNotFalse(
$tm->getThemeCookie(),
);
self::assertNotSame('', $tm->getThemeCookie());
}
}

0 comments on commit 1ddddeb

Please sign in to comment.