Skip to content

Commit 07db8a5

Browse files
Merge pull request #19551 from kamil-tekiela/Small-refactoring
Small refactoring
2 parents 797e137 + 79fa401 commit 07db8a5

10 files changed

Lines changed: 59 additions & 151 deletions

File tree

phpstan-baseline.neon

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6748,20 +6748,8 @@ parameters:
67486748
path: src/Encoding.php
67496749

67506750
-
6751-
message: '#^Cannot access offset 0 on mixed\.$#'
6752-
identifier: offsetAccess.nonOffsetAccessible
6753-
count: 2
6754-
path: src/Encoding.php
6755-
6756-
-
6757-
message: '#^Cannot access offset 1 on mixed\.$#'
6758-
identifier: offsetAccess.nonOffsetAccessible
6759-
count: 2
6760-
path: src/Encoding.php
6761-
6762-
-
6763-
message: '#^Cannot access offset 2 on mixed\.$#'
6764-
identifier: offsetAccess.nonOffsetAccessible
6751+
message: '#^Call to function function_exists\(\) with ''iconv''\|''mb_convert_encoding'' will always evaluate to true\.$#'
6752+
identifier: function.alreadyNarrowedType
67656753
count: 1
67666754
path: src/Encoding.php
67676755

@@ -6789,30 +6777,6 @@ parameters:
67896777
count: 1
67906778
path: src/Encoding.php
67916779

6792-
-
6793-
message: '#^Parameter \#1 \$extension of static method PhpMyAdmin\\Core\:\:warnMissingExtension\(\) expects string, mixed given\.$#'
6794-
identifier: argument.type
6795-
count: 1
6796-
path: src/Encoding.php
6797-
6798-
-
6799-
message: '#^Parameter \#1 \$function of function function_exists expects string, mixed given\.$#'
6800-
identifier: argument.type
6801-
count: 2
6802-
path: src/Encoding.php
6803-
6804-
-
6805-
message: '#^Right side of && is always true\.$#'
6806-
identifier: booleanAnd.rightAlwaysTrue
6807-
count: 1
6808-
path: src/Encoding.php
6809-
6810-
-
6811-
message: '#^Static property PhpMyAdmin\\Encoding\:\:\$engine \(int\|null\) does not accept mixed\.$#'
6812-
identifier: assign.propertyType
6813-
count: 2
6814-
path: src/Encoding.php
6815-
68166780
-
68176781
message: '#^Binary operation "\*" between mixed and 100 results in an error\.$#'
68186782
identifier: binaryOp.invalid
@@ -11223,28 +11187,16 @@ parameters:
1122311187
count: 1
1122411188
path: src/Plugins/Export/ExportSql.php
1122511189

11226-
-
11227-
message: '#^Method PhpMyAdmin\\Plugins\\Export\\ExportSql\:\:replaceWithAliases\(\) never assigns null to &\$flag so it can be removed from the by\-ref type\.$#'
11228-
identifier: parameterByRef.unusedType
11229-
count: 1
11230-
path: src/Plugins/Export/ExportSql.php
11231-
1123211190
-
1123311191
message: '#^Method PhpMyAdmin\\Plugins\\Export\\ExportSql\:\:setMaxQuerySize\(\) should return int\<0, max\> but returns int\.$#'
1123411192
identifier: return.type
1123511193
count: 2
1123611194
path: src/Plugins/Export/ExportSql.php
1123711195

11238-
-
11239-
message: '#^Only booleans are allowed in &&, bool\|null given on the left side\.$#'
11240-
identifier: booleanAnd.leftNotBoolean
11241-
count: 1
11242-
path: src/Plugins/Export/ExportSql.php
11243-
1124411196
-
1124511197
message: '#^Only booleans are allowed in an if condition, bool\|null given\.$#'
1124611198
identifier: if.condNotBoolean
11247-
count: 3
11199+
count: 1
1124811200
path: src/Plugins/Export/ExportSql.php
1124911201

1125011202
-
@@ -20208,12 +20160,6 @@ parameters:
2020820160
count: 1
2020920161
path: tests/unit/SqlTest.php
2021020162

20211-
-
20212-
message: '#^Method PhpMyAdmin\\Tests\\Stubs\\DbiDummy\:\:connect\(\) never returns null so it can be removed from the return type\.$#'
20213-
identifier: return.unusedType
20214-
count: 1
20215-
path: tests/unit/Stubs/DbiDummy.php
20216-
2021720163
-
2021820164
message: '''
2021920165
#^Call to deprecated method getInstance\(\) of class PhpMyAdmin\\Config\:

psalm-baseline.xml

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4294,38 +4294,12 @@
42944294
<code><![CDATA[Config::getInstance()]]></code>
42954295
<code><![CDATA[Config::getInstance()]]></code>
42964296
</DeprecatedMethod>
4297-
<MixedArgument>
4298-
<code><![CDATA[self::$enginemap[$engine][0]]]></code>
4299-
<code><![CDATA[self::$enginemap[$engine][0]]]></code>
4300-
<code><![CDATA[self::$enginemap[$engine][2]]]></code>
4301-
</MixedArgument>
4302-
<MixedArrayAccess>
4303-
<code><![CDATA[self::$enginemap[$engine][0]]]></code>
4304-
<code><![CDATA[self::$enginemap[$engine][0]]]></code>
4305-
<code><![CDATA[self::$enginemap[$engine][1]]]></code>
4306-
<code><![CDATA[self::$enginemap[$engine][1]]]></code>
4307-
<code><![CDATA[self::$enginemap[$engine][2]]]></code>
4308-
</MixedArrayAccess>
4309-
<MixedArrayOffset>
4310-
<code><![CDATA[self::$enginemap[$engine]]]></code>
4311-
<code><![CDATA[self::$enginemap[$engine]]]></code>
4312-
</MixedArrayOffset>
4313-
<MixedAssignment>
4314-
<code><![CDATA[$engine]]></code>
4315-
<code><![CDATA[self::$engine]]></code>
4316-
<code><![CDATA[self::$engine]]></code>
4317-
</MixedAssignment>
43184297
<PossiblyNullArgument>
43194298
<code><![CDATA[Config::getInstance()->getUploadTempDir()]]></code>
43204299
</PossiblyNullArgument>
43214300
<PossiblyUnusedMethod>
43224301
<code><![CDATA[setKanjiEncodings]]></code>
43234302
</PossiblyUnusedMethod>
4324-
<RedundantConditionGivenDocblockType>
4325-
<code><![CDATA[is_string($config->settings['IconvExtraParams'])]]></code>
4326-
<code><![CDATA[isset($config->settings['IconvExtraParams'])
4327-
&& is_string($config->settings['IconvExtraParams'])]]></code>
4328-
</RedundantConditionGivenDocblockType>
43294303
</file>
43304304
<file src="src/Engines/Innodb.php">
43314305
<DeprecatedMethod>

src/Config/Settings.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,18 +1832,18 @@ final class Settings
18321832
/**
18331833
* You can select here which functions will be used for character set conversion.
18341834
* Possible values are:
1835-
* auto - automatically use available one (first is tested iconv, then mbstring)
1836-
* iconv - use iconv or libiconv functions
1837-
* mb - use mbstring extension
1838-
* none - disable encoding conversion
1835+
* auto - automatically use available one (first is tested iconv, then mbstring)
1836+
* iconv - use iconv or libiconv functions
1837+
* mbstring - use mbstring extension
1838+
* none - disable encoding conversion
18391839
*
18401840
* ```php
18411841
* $cfg['RecodingEngine'] = 'auto';
18421842
* ```
18431843
*
18441844
* @link https://docs.phpmyadmin.net/en/latest/config.html#cfg_RecodingEngine
18451845
*
1846-
* @psalm-var 'auto'|'iconv'|'mb'|'none'
1846+
* @psalm-var 'auto'|'iconv'|'mbstring'|'none'
18471847
*/
18481848
public string $RecodingEngine;
18491849

@@ -4656,7 +4656,7 @@ private function setFilterLanguages(array $settings): string
46564656
/**
46574657
* @param array<int|string, mixed> $settings
46584658
*
4659-
* @psalm-return 'auto'|'iconv'|'mb'|'none'
4659+
* @psalm-return 'auto'|'iconv'|'mbstring'|'none'
46604660
*/
46614661
private function setRecodingEngine(array $settings): string
46624662
{
@@ -4667,6 +4667,10 @@ private function setRecodingEngine(array $settings): string
46674667
return 'auto';
46684668
}
46694669

4670+
if ($settings['RecodingEngine'] === 'mb') {
4671+
return 'mbstring';
4672+
}
4673+
46704674
return $settings['RecodingEngine'];
46714675
}
46724676

src/Encoding.php

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use function function_exists;
1616
use function fwrite;
1717
use function iconv;
18-
use function is_string;
1918
use function mb_convert_encoding;
2019
use function mb_convert_kana;
2120
use function mb_detect_encoding;
@@ -33,25 +32,16 @@
3332
*/
3433
class Encoding
3534
{
36-
/**
37-
* None encoding conversion engine
38-
*/
39-
public const ENGINE_NONE = 0;
40-
41-
/**
42-
* iconv encoding conversion engine
43-
*/
44-
public const ENGINE_ICONV = 1;
45-
46-
/**
47-
* mbstring encoding conversion engine
48-
*/
49-
public const ENGINE_MB = 3;
35+
public const ENGINE_NONE = 'none';
36+
public const ENGINE_ICONV = 'iconv';
37+
public const ENGINE_MBSTRING = 'mbstring';
5038

5139
/**
5240
* Chosen encoding engine
41+
*
42+
* @var self::ENGINE_NONE|self::ENGINE_ICONV|self::ENGINE_MBSTRING|null
5343
*/
54-
private static int|null $engine = null;
44+
private static string|null $engine = null;
5545

5646
/**
5747
* Map of conversion engine configurations
@@ -61,21 +51,17 @@ class Encoding
6151
* - function to detect
6252
* - engine contant
6353
* - extension name to warn when missing
64-
*
65-
* @var mixed[]
6654
*/
67-
private static array $enginemap = [
68-
'iconv' => ['iconv', self::ENGINE_ICONV, 'iconv'],
69-
'mb' => ['mb_convert_encoding', self::ENGINE_MB, 'mbstring'],
70-
'none' => ['isset', self::ENGINE_NONE, ''],
55+
private const ENGINE_MAP = [
56+
'iconv' => 'iconv',
57+
'mbstring' => 'mb_convert_encoding',
58+
'none' => 'isset',
7159
];
7260

7361
/**
7462
* Order of automatic detection of engines
75-
*
76-
* @var mixed[]
7763
*/
78-
private static array $engineorder = ['iconv', 'mb'];
64+
private const ENGINE_ORDER = ['iconv', 'mbstring'];
7965

8066
/**
8167
* Kanji encodings list
@@ -87,27 +73,23 @@ class Encoding
8773
*/
8874
public static function initEngine(): void
8975
{
90-
$engine = 'auto';
91-
$config = Config::getInstance();
92-
if (isset($config->settings['RecodingEngine'])) {
93-
$engine = $config->settings['RecodingEngine'];
94-
}
76+
$engine = Config::getInstance()->config->RecodingEngine;
9577

9678
/* Use user configuration */
97-
if (isset(self::$enginemap[$engine])) {
98-
if (function_exists(self::$enginemap[$engine][0])) {
99-
self::$engine = self::$enginemap[$engine][1];
79+
if (isset(self::ENGINE_MAP[$engine])) {
80+
if (function_exists(self::ENGINE_MAP[$engine])) {
81+
self::$engine = $engine;
10082

10183
return;
10284
}
10385

104-
Core::warnMissingExtension(self::$enginemap[$engine][2]);
86+
Core::warnMissingExtension($engine);
10587
}
10688

10789
/* Autodetection */
108-
foreach (self::$engineorder as $engine) {
109-
if (function_exists(self::$enginemap[$engine][0])) {
110-
self::$engine = self::$enginemap[$engine][1];
90+
foreach (self::ENGINE_ORDER as $engine) {
91+
if (function_exists(self::ENGINE_MAP[$engine])) {
92+
self::$engine = $engine;
11193

11294
return;
11395
}
@@ -120,9 +102,9 @@ public static function initEngine(): void
120102
/**
121103
* Setter for engine. Use with caution, mostly useful for testing.
122104
*
123-
* @param int $engine Engine encoding
105+
* @param self::ENGINE_NONE|self::ENGINE_ICONV|self::ENGINE_MBSTRING $engine
124106
*/
125-
public static function setEngine(int $engine): void
107+
public static function setEngine(string $engine): void
126108
{
127109
self::$engine = $engine;
128110
}
@@ -164,17 +146,13 @@ public static function convertString(
164146

165147
$config = Config::getInstance();
166148
$iconvExtraParams = '';
167-
if (
168-
isset($config->settings['IconvExtraParams'])
169-
&& is_string($config->settings['IconvExtraParams'])
170-
&& str_starts_with($config->settings['IconvExtraParams'], '//')
171-
) {
172-
$iconvExtraParams = $config->settings['IconvExtraParams'];
149+
if (str_starts_with($config->config->IconvExtraParams, '//')) {
150+
$iconvExtraParams = $config->config->IconvExtraParams;
173151
}
174152

175153
return match (self::$engine) {
176154
self::ENGINE_ICONV => iconv($srcCharset, $destCharset . $iconvExtraParams, $what),
177-
self::ENGINE_MB => mb_convert_encoding($what, $destCharset, $srcCharset),
155+
self::ENGINE_MBSTRING => mb_convert_encoding($what, $destCharset, $srcCharset),
178156
default => $what,
179157
};
180158
}
@@ -312,7 +290,7 @@ public static function kanjiEncodingForm(): string
312290
/**
313291
* Lists available encodings.
314292
*
315-
* @return mixed[]
293+
* @return string[]
316294
*/
317295
public static function listEncodings(): array
318296
{
@@ -322,7 +300,7 @@ public static function listEncodings(): array
322300

323301
/* Most engines do not support listing */
324302
$config = Config::getInstance();
325-
if (self::$engine != self::ENGINE_MB) {
303+
if (self::$engine != self::ENGINE_MBSTRING) {
326304
return array_filter($config->settings['AvailableCharsets'], static function (string $charset): bool {
327305
// Removes any ignored character
328306
$normalizedCharset = strtoupper((string) preg_replace(['/[^A-Za-z0-9\-\/]/'], '', $charset));

src/Plugins/Export/ExportSql.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ protected function exportRoutineSQL(
571571
$definition = Routines::getProcedureDefinition($dbi, $db, $routine);
572572
}
573573

574+
$flag = false;
574575
$createQuery = $this->replaceWithAliases($delimiter, $definition, $aliases, $db, $flag);
575576
if ($createQuery !== '' && Config::getInstance()->settings['Export']['remove_definer_from_definitions']) {
576577
// Remove definer clause from routine definitions
@@ -1496,6 +1497,7 @@ public function getTableDef(
14961497
}
14971498

14981499
// Substitute aliases in `CREATE` query.
1500+
$flag = false;
14991501
$createQuery = $this->replaceWithAliases(null, $createQuery, $aliases, $db, $flag);
15001502

15011503
// One warning per view.
@@ -1916,6 +1918,7 @@ public function exportStructure(string $db, string $table, string $exportMode, a
19161918
$triggerQuery .= $trigger->getDropSql() . ';' . "\n";
19171919
}
19181920

1921+
$flag = false;
19191922
$triggerQuery .= 'DELIMITER ' . $delimiter . "\n";
19201923
$triggerQuery .= $this->replaceWithAliases(
19211924
$delimiter,
@@ -2394,7 +2397,7 @@ private function makeCreateTableMSSQLCompatible(string $createQuery): string
23942397
* @param string $sqlQuery SQL query in which aliases are to be substituted
23952398
* @param mixed[] $aliases Alias information for db/table/column
23962399
* @param string $db the database name
2397-
* @param bool|null $flag the flag denoting whether any replacement was done
2400+
* @param bool $flag the flag denoting whether any replacement was done
23982401
*
23992402
* @return string query replaced with aliases
24002403
*/
@@ -2403,7 +2406,7 @@ public function replaceWithAliases(
24032406
string $sqlQuery,
24042407
array $aliases,
24052408
string $db,
2406-
bool|null &$flag = null,
2409+
bool &$flag,
24072410
): string {
24082411
$flag = false;
24092412

tests/unit/Config/SettingsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ public static function providerForTestConstructor(): array
707707
['DefaultTabServer', 'status', '/server/status'],
708708
['DefaultTabDatabase', 'search', '/database/search'],
709709
['DefaultTabTable', 'search', '/table/search'],
710-
['RecodingEngine', 'mb', 'mb'],
710+
['RecodingEngine', 'mb', 'mbstring'],
711711
['RowActionLinks', 'both', 'both'],
712712
],
713713
],

tests/unit/Dbal/DbiDummyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected function setUp(): void
3131
$this->dbi = $this->createDatabaseInterface($this->dummyDbi);
3232
DatabaseInterface::$instance = $this->dbi;
3333
$config = Config::getInstance();
34-
$config->settings['IconvExtraParams'] = '';
34+
$config->set('IconvExtraParams', '');
3535
}
3636

3737
public function testGetClientInfo(): void

0 commit comments

Comments
 (0)