diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 727c15566d..e403cd2d89 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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 - diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 6f0f66278a..790d1cee89 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -243,12 +243,10 @@ - getThemeCookie()]]> - getCookie($cookieName))]]> @@ -6610,8 +6608,6 @@ settings['CaptchaResponseParam'])]]> settings['CaptchaResponseParam'])]]> settings['Lang'])]]> - - @@ -9797,32 +9793,6 @@ - - - - - - getCookie($name)]]> - getCookie($name)]]> - getCookie($name)]]> - getCookie($name)]]> - - - getCookie($name)]]> - getCookie($name)]]> - - - themes]]> - - - - - - - - - - @@ -10051,8 +10021,6 @@ get('URLQueryEncryption')]]> - getCookie('pma_lang'))]]> - getCookie('pma_lang'))]]> diff --git a/src/Config.php b/src/Config.php index eb5c445432..5592c24851 100644 --- a/src/Config.php +++ b/src/Config.php @@ -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 { diff --git a/src/Controllers/Preferences/ManageController.php b/src/Controllers/Preferences/ManageController.php index bc25b7e640..91ecfc7ca3 100644 --- a/src/Controllers/Preferences/ManageController.php +++ b/src/Controllers/Preferences/ManageController.php @@ -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(); diff --git a/src/Theme/ThemeManager.php b/src/Theme/ThemeManager.php index 900212f020..6af9b5e0b1 100644 --- a/src/Theme/ThemeManager.php +++ b/src/Theme/ThemeManager.php @@ -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 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{ diff --git a/tests/unit/Theme/ThemeManagerTest.php b/tests/unit/Theme/ThemeManagerTest.php index 8f79d9c7f7..f332f90683 100644 --- a/tests/unit/Theme/ThemeManagerTest.php +++ b/tests/unit/Theme/ThemeManagerTest.php @@ -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()); } }