Skip to content

Commit

Permalink
Merge pull request phpmyadmin#19421 from kamil-tekiela/is_setup
Browse files Browse the repository at this point in the history
Add dedicated methods for is_setup
  • Loading branch information
MauricioFauth authored Dec 4, 2024
2 parents 42310b1 + 5b8168d commit 6eb12b6
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 86 deletions.
44 changes: 1 addition & 43 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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\<int\|string\>, mixed given\.$#'
identifier: argument.type
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -16452,12 +16416,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
Expand Down Expand Up @@ -19020,7 +18978,7 @@ parameters:
-
message: '#^Only booleans are allowed in a negated boolean, mixed given\.$#'
identifier: booleanNot.exprNotBoolean
count: 2
count: 1
path: src/Url.php

-
Expand Down
23 changes: 0 additions & 23 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@
<code><![CDATA[$this->settings]]></code>
<code><![CDATA[$this->settings]]></code>
<code><![CDATA[$this->settings]]></code>
<code><![CDATA[$this->settings]]></code>
</InvalidPropertyAssignmentValue>
<MixedArgument>
<code><![CDATA[$collationConnection]]></code>
Expand Down Expand Up @@ -422,7 +421,6 @@
<code><![CDATA[$errorList]]></code>
<code><![CDATA[$errorList]]></code>
<code><![CDATA[$ip]]></code>
<code><![CDATA[$isSetupScript]]></code>
<code><![CDATA[$key]]></code>
<code><![CDATA[$opts['errors']]]></code>
<code><![CDATA[$proxies['-' . $i]]]></code>
Expand Down Expand Up @@ -476,22 +474,14 @@
</PossiblyNullOperand>
<RiskyTruthyFalsyComparison>
<code><![CDATA[! $workPath]]></code>
<code><![CDATA[$config->get('is_setup')]]></code>
<code><![CDATA[$config->get('is_setup')]]></code>
<code><![CDATA[$isSetupScript]]></code>
<code><![CDATA[$isSetupScript]]></code>
<code><![CDATA[$isSetupScript]]></code>
</RiskyTruthyFalsyComparison>
</file>
<file src="src/Config/FormDisplayTemplate.php">
<MixedAssignment>
<code><![CDATA[$isSetupScript]]></code>
<code><![CDATA[$vName]]></code>
<code><![CDATA[$validator]]></code>
</MixedAssignment>
<RiskyTruthyFalsyComparison>
<code><![CDATA[! $isSetupScript]]></code>
<code><![CDATA[$this->config->get('is_setup')]]></code>
<code><![CDATA[empty($opts['errors'])]]></code>
</RiskyTruthyFalsyComparison>
</file>
Expand Down Expand Up @@ -647,7 +637,6 @@
<code><![CDATA[$vid]]></code>
</ReferenceReusedFromConfusingScope>
<RiskyTruthyFalsyComparison>
<code><![CDATA[$config->get('is_setup')]]></code>
<code><![CDATA[empty($errorList)]]></code>
<code><![CDATA[empty($values['Servers/1/controlpass'])]]></code>
<code><![CDATA[empty($values['Servers/1/controluser'])]]></code>
Expand Down Expand Up @@ -4030,7 +4019,6 @@
<code><![CDATA[$array]]></code>
</PossiblyUnusedParam>
<RiskyTruthyFalsyComparison>
<code><![CDATA[Config::getInstance()->get('is_setup')]]></code>
<code><![CDATA[empty($_SERVER['REMOTE_ADDR'])]]></code>
</RiskyTruthyFalsyComparison>
<UnsupportedReferenceUsage>
Expand Down Expand Up @@ -9589,16 +9577,6 @@
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
<MixedInferredReturnType>
<code><![CDATA[bool]]></code>
</MixedInferredReturnType>
<MixedReturnStatement>
<code><![CDATA[Config::getInstance()->get('is_setup')]]></code>
<code><![CDATA[Config::getInstance()->get('is_setup')]]></code>
</MixedReturnStatement>
<NullableReturnStatement>
<code><![CDATA[Config::getInstance()->get('is_setup')]]></code>
</NullableReturnStatement>
<RiskyTruthyFalsyComparison>
<code><![CDATA[empty($found[3])]]></code>
<code><![CDATA[empty($found[3])]]></code>
Expand Down Expand Up @@ -11009,7 +10987,6 @@
<code><![CDATA[$values['token']]]></code>
</MixedAssignment>
<RiskyTruthyFalsyComparison>
<code><![CDATA[! $config->get('is_setup')]]></code>
<code><![CDATA[! Config::getInstance()->get('URLQueryEncryption')]]></code>
<code><![CDATA[empty($GLOBALS['lang'])]]></code>
<code><![CDATA[empty($GLOBALS['lang'])]]></code>
Expand Down
14 changes: 12 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ class Config
/** @psalm-var ServerSettingsType */
public array $selectedServer;

private bool $isSetup = false;

public function __construct()
{
$this->config = new Settings([]);
Expand All @@ -127,15 +129,23 @@ public static function getInstance(): self
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
*
* @throws ConfigException
*/
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);

Expand Down
12 changes: 6 additions & 6 deletions src/Config/FormDisplay.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use PhpMyAdmin\Config;
use PhpMyAdmin\Config\Forms\User\UserFormList;
use PhpMyAdmin\Html\MySQLDocumentation;
use PhpMyAdmin\Sanitize;
use PhpMyAdmin\Util;

use function __;
Expand Down Expand Up @@ -104,8 +103,9 @@ class FormDisplay

public function __construct(private ConfigFile $configFile)
{
$this->formDisplayTemplate = new FormDisplayTemplate(Config::getInstance());
$this->isSetupScript = Sanitize::isSetup();
$config = Config::getInstance();
$this->formDisplayTemplate = new FormDisplayTemplate($config);
$this->isSetupScript = $config->isSetup();
// initialize validators
Validator::getValidators($this->configFile);
}
Expand Down Expand Up @@ -546,7 +546,7 @@ public function save(array $forms, bool $allowPartialSave = true): bool
$result = true;
$values = [];
$toSave = [];
$isSetupScript = Config::getInstance()->get('is_setup');
$isSetupScript = Config::getInstance()->isSetup();
if ($isSetupScript) {
$this->loadUserprefsInfo();
}
Expand Down Expand Up @@ -746,7 +746,7 @@ private function loadUserprefsInfo(): void
$this->userprefsKeys = array_flip(UserFormList::getFields());
// read real config for user preferences display
$config = Config::getInstance();
$userPrefsDisallow = $config->get('is_setup')
$userPrefsDisallow = $config->isSetup()

Check warning on line 749 in src/Config/FormDisplay.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "Ternary": --- Original +++ New @@ @@ $this->userprefsKeys = array_flip(UserFormList::getFields()); // read real config for user preferences display $config = Config::getInstance(); - $userPrefsDisallow = $config->isSetup() ? $this->configFile->get('UserprefsDisallow', []) : $config->settings['UserprefsDisallow']; + $userPrefsDisallow = $config->isSetup() ? $config->settings['UserprefsDisallow'] : $this->configFile->get('UserprefsDisallow', []); $this->userprefsDisallow = array_flip($userPrefsDisallow ?? []); } /**
? $this->configFile->get('UserprefsDisallow', [])
: $config->settings['UserprefsDisallow'];
$this->userprefsDisallow = array_flip($userPrefsDisallow ?? []);
Expand Down Expand Up @@ -808,7 +808,7 @@ private function setComments(string $systemPath, array &$opts): void
}

$config = Config::getInstance();
if ($config->get('is_setup')) {
if ($config->isSetup()) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Config/FormDisplayTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function displayInput(
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') {
Expand Down Expand Up @@ -104,7 +104,7 @@ public function displayGroupHeader(string $headerText): string
return '';
}

$colspan = $this->config->get('is_setup') ? 3 : 2;
$colspan = $this->config->isSetup() ? 3 : 2;

Check warning on line 107 in src/Config/FormDisplayTemplate.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ if ($headerText === '') { return ''; } - $colspan = $this->config->isSetup() ? 3 : 2; + $colspan = $this->config->isSetup() ? 2 : 2; return $this->template->render('config/form_display/group_header', ['group' => $this->group, 'colspan' => $colspan, 'header_text' => $headerText]); } /**

Check warning on line 107 in src/Config/FormDisplayTemplate.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ if ($headerText === '') { return ''; } - $colspan = $this->config->isSetup() ? 3 : 2; + $colspan = $this->config->isSetup() ? 4 : 2; return $this->template->render('config/form_display/group_header', ['group' => $this->group, 'colspan' => $colspan, 'header_text' => $headerText]); } /**

Check warning on line 107 in src/Config/FormDisplayTemplate.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ if ($headerText === '') { return ''; } - $colspan = $this->config->isSetup() ? 3 : 2; + $colspan = $this->config->isSetup() ? 3 : 1; return $this->template->render('config/form_display/group_header', ['group' => $this->group, 'colspan' => $colspan, 'header_text' => $headerText]); } /**

Check warning on line 107 in src/Config/FormDisplayTemplate.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ if ($headerText === '') { return ''; } - $colspan = $this->config->isSetup() ? 3 : 2; + $colspan = $this->config->isSetup() ? 3 : 3; return $this->template->render('config/form_display/group_header', ['group' => $this->group, 'colspan' => $colspan, 'header_text' => $headerText]); } /**

Check warning on line 107 in src/Config/FormDisplayTemplate.php

View workflow job for this annotation

GitHub Actions / Infection (8.2, ubuntu-latest)

Escaped Mutant for Mutator "Ternary": --- Original +++ New @@ @@ if ($headerText === '') { return ''; } - $colspan = $this->config->isSetup() ? 3 : 2; + $colspan = $this->config->isSetup() ? 2 : 3; return $this->template->render('config/form_display/group_header', ['group' => $this->group, 'colspan' => $colspan, 'header_text' => $headerText]); } /**

return $this->template->render('config/form_display/group_header', [
'group' => $this->group,
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static function getValidators(ConfigFile $cf): array

self::$validators = $cf->getDbEntry('_validators', []);
$config = Config::getInstance();
if ($config->get('is_setup')) {
if ($config->isSetup()) {
return self::$validators;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public static function linkURL(string $url): string
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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Middleware/SetupPageRedirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private function setupPageBootstrap(): void

// 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();
Expand Down
4 changes: 2 additions & 2 deletions src/Sanitize.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public static function checkLink(string $url, bool $http = false, bool $other =
/**
* Check if we are currently on a setup folder page
*/
public static function isSetup(): bool
private static function isSetup(): bool
{
return Config::getInstance()->get('is_setup');
return Config::getInstance()->isSetup();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public static function getCommonRaw(array $params = [], string $divider = '?', b
Current::$server > 0
&& Current::$server !== $config->settings['ServerDefault']
&& ! isset($params['server'])
&& ! $config->get('is_setup')
&& ! $config->isSetup()
) {
$params['server'] = Current::$server;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/Config/FormDisplayTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function testDisplayInput(): void

// second case

$this->config->set('is_setup', true);
$this->config->setSetup(true);
$opts = [];
$opts['errors'] = [];
$opts['setvalue'] = 'setVal';
Expand Down Expand Up @@ -203,14 +203,14 @@ public function testDisplayGroupHeader(): void

$this->formDisplayTemplate->group = 3;

$this->config->set('is_setup', true);
$this->config->setSetup(true);

$result = $this->formDisplayTemplate->displayGroupHeader('headerText');

self::assertStringContainsString('<tr class="group-header group-header-4">', $result);

// without PMA_SETUP
$this->config->set('is_setup', false);
$this->config->setSetup(false);

$this->formDisplayTemplate->group = 3;

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Config/FormDisplayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public function testSetComments(): void
self::assertTrue($opts['comment_warning']);

$config = Config::getInstance();
$config->set('is_setup', false);
$config->setSetup(false);

$config->settings['MaxDbList'] = 10;
$config->settings['MaxTableList'] = 10;
Expand Down

0 comments on commit 6eb12b6

Please sign in to comment.