Refactor flattenArray into getFlatArray in ConfigFile

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
This commit is contained in:
Kamil Tekiela 2021-12-07 17:47:55 +00:00
parent 5eb3404170
commit d44578ee04
3 changed files with 31 additions and 92 deletions

View File

@ -12,7 +12,6 @@ use PhpMyAdmin\Core;
use function array_diff;
use function array_flip;
use function array_keys;
use function array_walk;
use function count;
use function is_array;
use function preg_replace;
@ -83,13 +82,6 @@ class ConfigFile
*/
private $id;
/**
* Result for {@link flattenArray()}
*
* @var array|null
*/
private $flattenArrayResult;
/**
* @param array|null $baseConfig base configuration read from
* {@link PhpMyAdmin\Config::$base_config},
@ -247,48 +239,30 @@ class ConfigFile
/**
* Flattens multidimensional array, changes indices to paths
* (eg. 'key/subkey').
* Used as array_walk() callback.
*
* @param mixed $value Value
* @param mixed $key Key
* @param mixed $prefix Prefix
* @param array $array Multidimensional array
* @param string $prefix Prefix
*/
private function flattenArray($value, $key, $prefix): void
private function getFlatArray(array $array, string $prefix = ''): array
{
// no recursion for numeric arrays
if (is_array($value) && ! isset($value[0])) {
$prefix .= $key . '/';
array_walk(
$value,
function ($value, $key, $prefix): void {
$this->flattenArray($value, $key, $prefix);
},
$prefix
);
} else {
$this->flattenArrayResult[$prefix . $key] = $value;
$result = [];
foreach ($array as $key => $value) {
if (is_array($value) && ! isset($value[0])) {
$result += $this->getFlatArray($value, $prefix . $key . '/');
} else {
$result[$prefix . $key] = $value;
}
}
return $result;
}
/**
* Returns default config in a flattened array
*
* @return array
*/
public function getFlatDefaultConfig()
public function getFlatDefaultConfig(): array
{
$this->flattenArrayResult = [];
array_walk(
$this->defaultCfg,
function ($value, $key, $prefix): void {
$this->flattenArray($value, $key, $prefix);
},
''
);
$flatConfig = $this->flattenArrayResult;
$this->flattenArrayResult = null;
return $flatConfig;
return $this->getFlatArray($this->defaultCfg);
}
/**
@ -300,16 +274,7 @@ class ConfigFile
public function updateWithGlobalConfig(array $cfg): void
{
// load config array and flatten it
$this->flattenArrayResult = [];
array_walk(
$cfg,
function ($value, $key, $prefix): void {
$this->flattenArray($value, $key, $prefix);
},
''
);
$flatConfig = $this->flattenArrayResult;
$this->flattenArrayResult = null;
$flatConfig = $this->getFlatArray($cfg);
// save values map for translating a few user preferences paths,
// should be complemented by code reading from generated config
@ -533,16 +498,7 @@ class ConfigFile
*/
public function getConfigArray()
{
$this->flattenArrayResult = [];
array_walk(
$_SESSION[$this->id],
function ($value, $key, $prefix): void {
$this->flattenArray($value, $key, $prefix);
},
''
);
$c = $this->flattenArrayResult;
$this->flattenArrayResult = null;
$c = $this->getFlatArray($_SESSION[$this->id]);
$persistKeys = array_diff(
array_keys($this->persistKeys),

View File

@ -280,11 +280,6 @@ parameters:
count: 1
path: libraries/classes/Config.php
-
message: "#^Empty array passed to foreach\\.$#"
count: 1
path: libraries/classes/Config/ConfigFile.php
-
message: "#^Method PhpMyAdmin\\\\Config\\\\ConfigFile\\:\\:__construct\\(\\) has parameter \\$baseConfig with no value type specified in iterable type array\\.$#"
count: 1
@ -305,6 +300,16 @@ parameters:
count: 1
path: libraries/classes/Config/ConfigFile.php
-
message: "#^Method PhpMyAdmin\\\\Config\\\\ConfigFile\\:\\:getFlatArray\\(\\) has parameter \\$array with no value type specified in iterable type array\\.$#"
count: 1
path: libraries/classes/Config/ConfigFile.php
-
message: "#^Method PhpMyAdmin\\\\Config\\\\ConfigFile\\:\\:getFlatArray\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: libraries/classes/Config/ConfigFile.php
-
message: "#^Method PhpMyAdmin\\\\Config\\\\ConfigFile\\:\\:getFlatDefaultConfig\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
@ -375,11 +380,6 @@ parameters:
count: 1
path: libraries/classes/Config/ConfigFile.php
-
message: "#^Property PhpMyAdmin\\\\Config\\\\ConfigFile\\:\\:\\$flattenArrayResult type has no value type specified in iterable type array\\.$#"
count: 1
path: libraries/classes/Config/ConfigFile.php
-
message: "#^Property PhpMyAdmin\\\\Config\\\\ConfigFile\\:\\:\\$persistKeys type has no value type specified in iterable type array\\.$#"
count: 1

View File

@ -457,20 +457,6 @@
<InvalidIterator occurrences="1">
<code>$this-&gt;cfgDb['_overrides']</code>
</InvalidIterator>
<MissingClosureParamType occurrences="12">
<code>$key</code>
<code>$key</code>
<code>$key</code>
<code>$key</code>
<code>$prefix</code>
<code>$prefix</code>
<code>$prefix</code>
<code>$prefix</code>
<code>$value</code>
<code>$value</code>
<code>$value</code>
<code>$value</code>
</MissingClosureParamType>
<MixedArgument occurrences="12">
<code>$_SESSION[$this-&gt;id]</code>
<code>$_SESSION[$this-&gt;id]</code>
@ -497,11 +483,10 @@
<MixedArrayAssignment occurrences="1">
<code>$_SESSION[$this-&gt;id]['Servers'][$i]</code>
</MixedArrayAssignment>
<MixedArrayOffset occurrences="2">
<MixedArrayOffset occurrences="1">
<code>$c[$mapFrom]</code>
<code>$this-&gt;cfgUpdateReadMapping[$path]</code>
</MixedArrayOffset>
<MixedAssignment occurrences="16">
<MixedAssignment occurrences="17">
<code>$_SESSION[$this-&gt;id]['Servers'][$i]</code>
<code>$c</code>
<code>$c[$k]</code>
@ -513,8 +498,9 @@
<code>$mapFrom</code>
<code>$path</code>
<code>$path</code>
<code>$path</code>
<code>$port</code>
<code>$result[$prefix . $key]</code>
<code>$value</code>
<code>$value</code>
<code>$value</code>
<code>$verbose</code>
@ -524,11 +510,8 @@
<code>array</code>
<code>string</code>
</MixedInferredReturnType>
<MixedOperand occurrences="7">
<code>$key</code>
<MixedOperand occurrences="4">
<code>$port</code>
<code>$prefix</code>
<code>$prefix</code>
<code>$this-&gt;getValue($path . '/host')</code>
<code>$this-&gt;getValue($path . '/socket')</code>
<code>$this-&gt;getValue($path . '/user')</code>