From 2756f2724994fbbd41cc767ea1462d2ed7d5d91e Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 4 Dec 2024 23:16:15 +0000 Subject: [PATCH] Add dedicated methods for is_setup Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 44 +------------------ psalm-baseline.xml | 23 ---------- src/Config.php | 14 +++++- src/Config/FormDisplay.php | 6 +-- src/Config/FormDisplayTemplate.php | 4 +- src/Config/Validator.php | 2 +- src/Core.php | 2 +- src/Http/Middleware/SetupPageRedirection.php | 2 +- src/Sanitize.php | 2 +- src/Url.php | 2 +- tests/unit/Config/FormDisplayTemplateTest.php | 6 +-- tests/unit/Config/FormDisplayTest.php | 2 +- 12 files changed, 27 insertions(+), 82 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 13ebfba598..913a28e123 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1023,30 +1023,12 @@ parameters: count: 1 path: src/Config/FormDisplay.php - - - message: '#^Only booleans are allowed in &&, mixed given on the left side\.$#' - identifier: booleanAnd.leftNotBoolean - count: 1 - path: src/Config/FormDisplay.php - - message: '#^Only booleans are allowed in a negated boolean, int\|string\|false given\.$#' identifier: booleanNot.exprNotBoolean count: 1 path: src/Config/FormDisplay.php - - - message: '#^Only booleans are allowed in a ternary operator condition, mixed given\.$#' - identifier: ternary.condNotBoolean - count: 1 - path: src/Config/FormDisplay.php - - - - message: '#^Only booleans are allowed in an if condition, mixed given\.$#' - identifier: if.condNotBoolean - count: 3 - path: src/Config/FormDisplay.php - - message: '#^Parameter \#1 \$array of function array_flip expects array\, mixed given\.$#' identifier: argument.type @@ -1182,12 +1164,6 @@ parameters: - message: '#^Only booleans are allowed in a negated boolean, mixed given\.$#' identifier: booleanNot.exprNotBoolean - count: 2 - path: src/Config/FormDisplayTemplate.php - - - - message: '#^Only booleans are allowed in a ternary operator condition, mixed given\.$#' - identifier: ternary.condNotBoolean count: 1 path: src/Config/FormDisplayTemplate.php @@ -1485,12 +1461,6 @@ parameters: count: 1 path: src/Config/Validator.php - - - message: '#^Only booleans are allowed in an if condition, mixed given\.$#' - identifier: if.condNotBoolean - count: 1 - path: src/Config/Validator.php - - message: '#^PHPDoc tag @var with type string is not subtype of native type non\-empty\-string\|null\.$#' identifier: varTag.nativeType @@ -6492,12 +6462,6 @@ parameters: count: 1 path: src/Core.php - - - message: '#^Only booleans are allowed in an if condition, mixed given\.$#' - identifier: if.condNotBoolean - count: 1 - path: src/Core.php - - message: '#^Parameter \#1 \$sqlQuery of static method PhpMyAdmin\\Html\\Generator\:\:formatSql\(\) expects string, mixed given\.$#' identifier: argument.type @@ -16464,12 +16428,6 @@ parameters: count: 2 path: src/Sanitize.php - - - message: '#^Method PhpMyAdmin\\Sanitize\:\:isSetup\(\) should return bool but returns mixed\.$#' - identifier: return.type - count: 1 - path: src/Sanitize.php - - message: '#^Method PhpMyAdmin\\Sanitize\:\:sanitizeFilename\(\) should return string but returns string\|null\.$#' identifier: return.type @@ -19038,7 +18996,7 @@ parameters: - message: '#^Only booleans are allowed in a negated boolean, mixed given\.$#' identifier: booleanNot.exprNotBoolean - count: 2 + count: 1 path: src/Url.php - diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 0bc3e6961a..1394976de0 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -184,7 +184,6 @@ settings]]> settings]]> settings]]> - settings]]> @@ -422,7 +421,6 @@ - @@ -476,22 +474,14 @@ - get('is_setup')]]> - get('is_setup')]]> - - - - - - config->get('is_setup')]]> @@ -647,7 +637,6 @@ - get('is_setup')]]> @@ -4030,7 +4019,6 @@ - get('is_setup')]]> @@ -9592,16 +9580,6 @@ - - - - - get('is_setup')]]> - get('is_setup')]]> - - - get('is_setup')]]> - @@ -11015,7 +10993,6 @@ - get('is_setup')]]> get('URLQueryEncryption')]]> diff --git a/src/Config.php b/src/Config.php index ea330e2f1b..8ec77cdd5a 100644 --- a/src/Config.php +++ b/src/Config.php @@ -107,6 +107,8 @@ class Config /** @psalm-var ServerSettingsType */ public array $selectedServer; + private bool $isSetup = false; + public function __construct() { $this->config = new Settings([]); @@ -127,6 +129,16 @@ class Config return self::$instance; } + public function setSetup(bool $isSetup): void + { + $this->isSetup = $isSetup; + } + + public function isSetup(): bool + { + return $this->isSetup; + } + /** * @param string|null $source source to read config from * @@ -134,8 +146,6 @@ class Config */ public function loadAndCheck(string|null $source = null): void { - $this->settings['is_setup'] = false; - // functions need to refresh in case of config file changed goes in PhpMyAdmin\Config::load() $this->load($source); diff --git a/src/Config/FormDisplay.php b/src/Config/FormDisplay.php index 5e9f9b5a54..5c99535982 100644 --- a/src/Config/FormDisplay.php +++ b/src/Config/FormDisplay.php @@ -546,7 +546,7 @@ class FormDisplay $result = true; $values = []; $toSave = []; - $isSetupScript = Config::getInstance()->get('is_setup'); + $isSetupScript = Config::getInstance()->isSetup(); if ($isSetupScript) { $this->loadUserprefsInfo(); } @@ -746,7 +746,7 @@ class FormDisplay $this->userprefsKeys = array_flip(UserFormList::getFields()); // read real config for user preferences display $config = Config::getInstance(); - $userPrefsDisallow = $config->get('is_setup') + $userPrefsDisallow = $config->isSetup() ? $this->configFile->get('UserprefsDisallow', []) : $config->settings['UserprefsDisallow']; $this->userprefsDisallow = array_flip($userPrefsDisallow ?? []); @@ -808,7 +808,7 @@ class FormDisplay } $config = Config::getInstance(); - if ($config->get('is_setup')) { + if ($config->isSetup()) { return; } diff --git a/src/Config/FormDisplayTemplate.php b/src/Config/FormDisplayTemplate.php index 813163ce04..4f959f1226 100644 --- a/src/Config/FormDisplayTemplate.php +++ b/src/Config/FormDisplayTemplate.php @@ -61,7 +61,7 @@ class FormDisplayTemplate bool $valueIsDefault = true, array|null $opts = null, ): string { - $isSetupScript = $this->config->get('is_setup'); + $isSetupScript = $this->config->isSetup(); $optionIsDisabled = ! $isSetupScript && isset($opts['userprefs_allow']) && ! $opts['userprefs_allow']; $trClass = $this->group > 0 ? 'group-field group-field-' . $this->group : ''; if (isset($opts['setvalue']) && $opts['setvalue'] === ':group') { @@ -104,7 +104,7 @@ class FormDisplayTemplate return ''; } - $colspan = $this->config->get('is_setup') ? 3 : 2; + $colspan = $this->config->isSetup() ? 3 : 2; return $this->template->render('config/form_display/group_header', [ 'group' => $this->group, diff --git a/src/Config/Validator.php b/src/Config/Validator.php index fc4f6575c7..aaa6b8c647 100644 --- a/src/Config/Validator.php +++ b/src/Config/Validator.php @@ -70,7 +70,7 @@ class Validator self::$validators = $cf->getDbEntry('_validators', []); $config = Config::getInstance(); - if ($config->get('is_setup')) { + if ($config->isSetup()) { return self::$validators; } diff --git a/src/Core.php b/src/Core.php index 2ddbb8a86f..f115e36587 100644 --- a/src/Core.php +++ b/src/Core.php @@ -414,7 +414,7 @@ class Core parse_str($arr['query'] ?? '', $vars); $query = http_build_query(['url' => $vars['url']]); - if (Config::getInstance()->get('is_setup')) { + if (Config::getInstance()->isSetup()) { return '../index.php?route=/url&' . $query; } diff --git a/src/Http/Middleware/SetupPageRedirection.php b/src/Http/Middleware/SetupPageRedirection.php index 6981c25b0d..066b7276e5 100644 --- a/src/Http/Middleware/SetupPageRedirection.php +++ b/src/Http/Middleware/SetupPageRedirection.php @@ -54,7 +54,7 @@ final class SetupPageRedirection implements MiddlewareInterface // Save current language in a cookie, since it was not set in Common::run(). $this->config->setCookie('pma_lang', $GLOBALS['lang']); - $this->config->set('is_setup', true); + $this->config->setSetup(true); // allows for redirection even after sending some data ob_start(); diff --git a/src/Sanitize.php b/src/Sanitize.php index a58e58828d..c99e7c37c7 100644 --- a/src/Sanitize.php +++ b/src/Sanitize.php @@ -88,7 +88,7 @@ class Sanitize */ public static function isSetup(): bool { - return Config::getInstance()->get('is_setup'); + return Config::getInstance()->isSetup(); } /** diff --git a/src/Url.php b/src/Url.php index 9b78c90f7e..aca9e7d46e 100644 --- a/src/Url.php +++ b/src/Url.php @@ -215,7 +215,7 @@ class Url Current::$server > 0 && Current::$server !== $config->settings['ServerDefault'] && ! isset($params['server']) - && ! $config->get('is_setup') + && ! $config->isSetup() ) { $params['server'] = Current::$server; } diff --git a/tests/unit/Config/FormDisplayTemplateTest.php b/tests/unit/Config/FormDisplayTemplateTest.php index 45a651f6e6..6831b0a989 100644 --- a/tests/unit/Config/FormDisplayTemplateTest.php +++ b/tests/unit/Config/FormDisplayTemplateTest.php @@ -80,7 +80,7 @@ class FormDisplayTemplateTest extends AbstractTestCase // second case - $this->config->set('is_setup', true); + $this->config->setSetup(true); $opts = []; $opts['errors'] = []; $opts['setvalue'] = 'setVal'; @@ -203,14 +203,14 @@ class FormDisplayTemplateTest extends AbstractTestCase $this->formDisplayTemplate->group = 3; - $this->config->set('is_setup', true); + $this->config->setSetup(true); $result = $this->formDisplayTemplate->displayGroupHeader('headerText'); self::assertStringContainsString('', $result); // without PMA_SETUP - $this->config->set('is_setup', false); + $this->config->setSetup(false); $this->formDisplayTemplate->group = 3; diff --git a/tests/unit/Config/FormDisplayTest.php b/tests/unit/Config/FormDisplayTest.php index b78bcd373e..7382bc3ba9 100644 --- a/tests/unit/Config/FormDisplayTest.php +++ b/tests/unit/Config/FormDisplayTest.php @@ -384,7 +384,7 @@ class FormDisplayTest extends AbstractTestCase self::assertTrue($opts['comment_warning']); $config = Config::getInstance(); - $config->set('is_setup', false); + $config->setSetup(false); $config->settings['MaxDbList'] = 10; $config->settings['MaxTableList'] = 10;