Visitar URL original
Merge pull request #18855 from kamil-tekiela/strnatcasecmp · phpmyadmin/phpmyadmin@961b339 · GitHub
Skip to content

Commit 961b339

Browse files
Merge pull request #18855 from kamil-tekiela/strnatcasecmp
Use first-class callables in sorting functions
2 parents f3be6ef + 5ec43f7 commit 961b339

9 files changed

Lines changed: 33 additions & 30 deletions

File tree

‎phpstan-baseline.neon‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ parameters:
14061406
path: src/ConfigStorage/Relation.php
14071407

14081408
-
1409-
message: "#^Parameter \\#2 \\$callback of function usort expects callable\\(string\\|null, string\\|null\\)\\: int, 'strnatcasecmp' given\\.$#"
1409+
message: "#^Parameter \\#2 \\$callback of function usort expects callable\\(string\\|null, string\\|null\\)\\: int, Closure\\(string, string\\)\\: int\\<\\-1, 1\\> given\\.$#"
14101410
count: 1
14111411
path: src/ConfigStorage/Relation.php
14121412

@@ -5136,12 +5136,12 @@ parameters:
51365136
path: src/Controllers/Table/RelationController.php
51375137

51385138
-
5139-
message: "#^Parameter \\#2 \\$callback of function usort expects callable\\(mixed, mixed\\)\\: int, 'strnatcasecmp' given\\.$#"
5139+
message: "#^Parameter \\#2 \\$callback of function usort expects callable\\(mixed, mixed\\)\\: int, Closure\\(string, string\\)\\: int\\<\\-1, 1\\> given\\.$#"
51405140
count: 1
51415141
path: src/Controllers/Table/RelationController.php
51425142

51435143
-
5144-
message: "#^Parameter \\#2 \\$callback of function usort expects callable\\(string\\|null, string\\|null\\)\\: int, 'strnatcasecmp' given\\.$#"
5144+
message: "#^Parameter \\#2 \\$callback of function usort expects callable\\(string\\|null, string\\|null\\)\\: int, Closure\\(string, string\\)\\: int\\<\\-1, 1\\> given\\.$#"
51455145
count: 1
51465146
path: src/Controllers/Table/RelationController.php
51475147

@@ -10041,7 +10041,7 @@ parameters:
1004110041
path: src/ListDatabase.php
1004210042

1004310043
-
10044-
message: "#^Parameter \\#2 \\$callback of function usort expects callable\\(mixed, mixed\\)\\: int, 'strnatcasecmp' given\\.$#"
10044+
message: "#^Parameter \\#2 \\$callback of function usort expects callable\\(mixed, mixed\\)\\: int, Closure\\(string, string\\)\\: int\\<\\-1, 1\\> given\\.$#"
1004510045
count: 1
1004610046
path: src/ListDatabase.php
1004710047

‎psalm-baseline.xml‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@
687687
<code>Config::getInstance()</code>
688688
</DeprecatedMethod>
689689
<InvalidArgument>
690-
<code><![CDATA[usort($tables, 'strnatcasecmp')]]></code>
690+
<code>usort($tables, strnatcasecmp(...))</code>
691691
</InvalidArgument>
692692
<InvalidReturnStatement>
693693
<code>$tableNameReplacements</code>
@@ -722,7 +722,7 @@
722722
<code><![CDATA[$oneKey['index_list']]]></code>
723723
</MixedArgument>
724724
<MixedArgumentTypeCoercion>
725-
<code><![CDATA[uksort($foreign, 'strnatcasecmp')]]></code>
725+
<code>uksort($foreign, strnatcasecmp(...))</code>
726726
</MixedArgumentTypeCoercion>
727727
<MixedArrayAccess>
728728
<code><![CDATA[$column['COLUMN_NAME']]]></code>
@@ -3685,7 +3685,7 @@
36853685
<code>Config::getInstance()</code>
36863686
</DeprecatedMethod>
36873687
<InvalidArgument>
3688-
<code><![CDATA[usort($tables, 'strnatcasecmp')]]></code>
3688+
<code>usort($tables, strnatcasecmp(...))</code>
36893689
</InvalidArgument>
36903690
<MixedArgument>
36913691
<code>$foreignDb</code>
@@ -3697,7 +3697,7 @@
36973697
: []]]></code>
36983698
</MixedArgument>
36993699
<MixedArgumentTypeCoercion>
3700-
<code><![CDATA[usort($columnList, 'strnatcasecmp')]]></code>
3700+
<code>usort($columnList, strnatcasecmp(...))</code>
37013701
</MixedArgumentTypeCoercion>
37023702
<MixedArrayAccess>
37033703
<code><![CDATA[$oneKey['ref_db_name']]]></code>
@@ -5197,8 +5197,8 @@
51975197
<code><![CDATA[$this->versionString]]></code>
51985198
</MixedArgument>
51995199
<MixedArgumentTypeCoercion>
5200-
<code><![CDATA[uksort($eachTables, 'strnatcasecmp')]]></code>
5201-
<code><![CDATA[uksort($oneDatabaseTables, 'strnatcasecmp')]]></code>
5200+
<code>uksort($eachTables, strnatcasecmp(...))</code>
5201+
<code>uksort($oneDatabaseTables, strnatcasecmp(...))</code>
52025202
</MixedArgumentTypeCoercion>
52035203
<MixedArrayAccess>
52045204
<code>$resultTarget[$row[$keyIndex]]</code>
@@ -7103,7 +7103,7 @@
71037103
<code>$db</code>
71047104
</MixedArgument>
71057105
<MixedArgumentTypeCoercion>
7106-
<code><![CDATA[usort($databaseList, 'strnatcasecmp')]]></code>
7106+
<code>usort($databaseList, strnatcasecmp(...))</code>
71077107
</MixedArgumentTypeCoercion>
71087108
<MixedAssignment>
71097109
<code>$db</code>
@@ -7259,9 +7259,6 @@
72597259
<PossiblyNullPropertyFetch>
72607260
<code><![CDATA[$container->parent->pos2]]></code>
72617261
</PossiblyNullPropertyFetch>
7262-
<PossiblyUnusedReturnValue>
7263-
<code>int</code>
7264-
</PossiblyUnusedReturnValue>
72657262
<RedundantCast>
72667263
<code>(string) $key</code>
72677264
<code>(string) $key</code>
@@ -12306,7 +12303,7 @@
1230612303
</MixedArgument>
1230712304
<MixedArgumentTypeCoercion>
1230812305
<code>$columnNames</code>
12309-
<code><![CDATA[uksort($tables, 'strnatcasecmp')]]></code>
12306+
<code>uksort($tables, strnatcasecmp(...))</code>
1231012307
</MixedArgumentTypeCoercion>
1231112308
<MixedArrayAccess>
1231212309
<code><![CDATA[$_SESSION['tmpval']['table_limit_offset']]]></code>

‎src/ConfigStorage/Relation.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
use function str_contains;
4747
use function str_replace;
4848
use function strlen;
49+
use function strnatcasecmp;
4950
use function trim;
5051
use function uksort;
5152
use function usort;
@@ -749,7 +750,7 @@ public function buildForeignDropdown(array $foreign, string $data, string $mode)
749750
if ($mode === 'id-content' || $mode === 'id-only') {
750751
// sort for id-content
751752
if ($config->settings['NaturalOrder']) {
752-
uksort($foreign, 'strnatcasecmp');
753+
uksort($foreign, strnatcasecmp(...));
753754
} else {
754755
ksort($foreign);
755756
}
@@ -1655,7 +1656,7 @@ public function getTables(string $foreignDb, string $tblStorageEngine): array
16551656
}
16561657

16571658
if (Config::getInstance()->settings['NaturalOrder']) {
1658-
usort($tables, 'strnatcasecmp');
1659+
usort($tables, strnatcasecmp(...));
16591660
}
16601661

16611662
return $tables;

‎src/Controllers/Table/RelationController.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use function array_keys;
2828
use function mb_strtoupper;
2929
use function md5;
30+
use function strnatcasecmp;
3031
use function strtoupper;
3132
use function uksort;
3233
use function usort;
@@ -176,7 +177,7 @@ public function __invoke(ServerRequest $request): void
176177

177178
$config = Config::getInstance();
178179
if ($config->settings['NaturalOrder']) {
179-
uksort($columnArray, 'strnatcasecmp');
180+
uksort($columnArray, strnatcasecmp(...));
180181
}
181182

182183
$foreignKeyRow = '';
@@ -325,7 +326,7 @@ public function getDropdownValueForTable(): void
325326
}
326327

327328
if (Config::getInstance()->settings['NaturalOrder']) {
328-
usort($columnList, 'strnatcasecmp');
329+
usort($columnList, strnatcasecmp(...));
329330
}
330331

331332
$this->response->addJSON('columns', $columnList);
@@ -368,7 +369,7 @@ public function getDropdownValueForDatabase(string $storageEngine): void
368369
}
369370

370371
if (Config::getInstance()->settings['NaturalOrder']) {
371-
usort($tables, 'strnatcasecmp');
372+
usort($tables, strnatcasecmp(...));
372373
}
373374

374375
$this->response->addJSON('tables', $tables);

‎src/DatabaseInterface.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
use function str_contains;
5151
use function str_starts_with;
5252
use function stripos;
53+
use function strnatcasecmp;
5354
use function strtolower;
5455
use function strtoupper;
5556
use function strtr;
@@ -329,7 +330,7 @@ public function getTables(string $database, int $connectionType = Connection::TY
329330
$connectionType,
330331
);
331332
if (Config::getInstance()->settings['NaturalOrder']) {
332-
usort($tables, 'strnatcasecmp');
333+
usort($tables, strnatcasecmp(...));
333334
}
334335

335336
return $tables;
@@ -455,7 +456,7 @@ public function getTablesFull(
455456
if ($sortBy === 'Name' && $config->settings['NaturalOrder']) {
456457
// here, the array's first key is by schema name
457458
foreach ($tables as $oneDatabaseName => $oneDatabaseTables) {
458-
uksort($oneDatabaseTables, 'strnatcasecmp');
459+
uksort($oneDatabaseTables, strnatcasecmp(...));
459460

460461
if ($sortOrder === 'DESC') {
461462
$oneDatabaseTables = array_reverse($oneDatabaseTables);
@@ -555,7 +556,7 @@ static function (array $a, array $b): int {
555556
// Sort naturally if the config allows it and we're sorting
556557
// the Name column.
557558
if ($sortBy === 'Name' && $config->settings['NaturalOrder']) {
558-
uksort($eachTables, 'strnatcasecmp');
559+
uksort($eachTables, strnatcasecmp(...));
559560

560561
if ($sortOrder === 'DESC') {
561562
$eachTables = array_reverse($eachTables);

‎src/ListDatabase.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function preg_match;
1313
use function sort;
1414
use function strlen;
15+
use function strnatcasecmp;
1516
use function strtr;
1617
use function usort;
1718

@@ -108,7 +109,7 @@ protected function retrieve(string|null $likeDbName = null): array
108109
}
109110

110111
if ($config->settings['NaturalOrder']) {
111-
usort($databaseList, 'strnatcasecmp');
112+
usort($databaseList, strnatcasecmp(...));
112113
} else {
113114
// need to sort anyway, otherwise information_schema
114115
// goes at the top

‎src/Navigation/NavigationTree.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ public function renderState(): string
848848

849849
$this->groupTree();
850850
$children = $this->tree->children;
851-
usort($children, [self::class, 'sortNode']);
851+
usort($children, self::sortNode(...));
852852
$this->setVisibility();
853853

854854
$nodes = $this->renderNodes($children);
@@ -877,7 +877,7 @@ public function renderPath(): string|false
877877
$listContent = $this->fastFilterHtml($node);
878878
$listContent .= $this->getPageSelector($node);
879879
$children = $node->children;
880-
usort($children, [self::class, 'sortNode']);
880+
usort($children, self::sortNode(...));
881881

882882
$listContent .= $this->renderNodes($children, false);
883883

@@ -1098,7 +1098,7 @@ private function renderNode(Node $node, string $class = ''): string
10981098
}
10991099

11001100
$children = $node->children;
1101-
usort($children, [self::class, 'sortNode']);
1101+
usort($children, self::sortNode(...));
11021102
$buffer = '';
11031103
$extraClass = '';
11041104
for ($i = 0, $nbChildren = count($children); $i < $nbChildren; $i++) {
@@ -1184,7 +1184,7 @@ public function renderDbSelect(): string
11841184
}
11851185

11861186
$children = $this->tree->children;
1187-
usort($children, [self::class, 'sortNode']);
1187+
usort($children, self::sortNode(...));
11881188
$this->setVisibility();
11891189

11901190
$nodes = $this->renderNodes($children);

‎src/Server/Privileges.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
use function sprintf;
5757
use function str_contains;
5858
use function str_replace;
59+
use function strnatcasecmp;
5960
use function strtr;
6061
use function trim;
6162
use function uksort;
@@ -1837,7 +1838,7 @@ public function getHtmlForInitials(): string
18371838
// just letters. For letters A-Z, we add the non-used letters
18381839
// as greyed out.
18391840
$initialsMap = array_fill_keys($initials, true) + array_fill_keys(range('A', 'Z'), false);
1840-
uksort($initialsMap, 'strnatcasecmp');
1841+
uksort($initialsMap, strnatcasecmp(...));
18411842

18421843
return $this->template->render('server/privileges/initials_row', [
18431844
'array_initials' => $initialsMap,

‎src/Util.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
use function str_starts_with;
7171
use function strftime;
7272
use function strlen;
73+
use function strnatcasecmp;
7374
use function strrev;
7475
use function strtolower;
7576
use function strtr;
@@ -1856,7 +1857,7 @@ public static function getTablesWhenOpen(string $db, ResultInterface $dbInfoResu
18561857
}
18571858

18581859
if ($config->settings['NaturalOrder']) {
1859-
uksort($tables, 'strnatcasecmp');
1860+
uksort($tables, strnatcasecmp(...));
18601861
}
18611862
}
18621863

0 commit comments

Comments
 (0)