Skip to content

Commit c185910

Browse files
authored
Merge pull request #8308 from kenjis/fix-ArrayHelper-sortValuesByNatural
refactor: TypeError in strict mode by ArrayHelper::SortValuesByNatural()
2 parents 8b6260a + 29e6f76 commit c185910

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

system/Helpers/Array/ArrayHelper.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,17 @@ public static function recursiveCount(array $array, int $counter = 0): int
306306
* Sorts array values in natural order
307307
* If the value is an array, you need to specify the $sortByIndex of the key to sort
308308
*
309-
* @param int|string|null $sortByIndex
309+
* @param list<int|list<int|string>|string> $array
310+
* @param int|string|null $sortByIndex
310311
*/
311312
public static function sortValuesByNatural(array &$array, $sortByIndex = null): bool
312313
{
313314
return usort($array, static function ($currentValue, $nextValue) use ($sortByIndex) {
314315
if ($sortByIndex !== null) {
315-
return strnatcmp($currentValue[$sortByIndex], $nextValue[$sortByIndex]);
316+
return strnatcmp((string) $currentValue[$sortByIndex], (string) $nextValue[$sortByIndex]);
316317
}
317318

318-
return strnatcmp($currentValue, $nextValue);
319+
return strnatcmp((string) $currentValue, (string) $nextValue);
319320
});
320321
}
321322
}

tests/system/Helpers/Array/ArrayHelperSortValuesByNaturalTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* This file is part of CodeIgniter 4 framework.
57
*

0 commit comments

Comments
 (0)