website/errors/sortArray.list.md
<?php declare(strict_types = 1);
/** @param list<string> $list */
function doFoo(array $list): void
{
ksort($list);
}
ksort() sorts an array by its keys. A list is an array whose keys are consecutive integers starting from 0, so its keys are already in ascending order. With the default SORT_REGULAR flag (or SORT_NUMERIC), sorting those keys leaves the array unchanged, so the call has no effect. This usually indicates a logic error or leftover code.
This check is part of PHPStan's bleeding edge and runs at rule level 5.
Remove the pointless sort call:
/** @param list<string> $list */
function doFoo(array $list): void
{
- ksort($list);
}
If you meant to sort by value instead of by key, use sort():
/** @param list<string> $list */
function doFoo(array $list): void
{
- ksort($list);
+ sort($list);
}
If the list type comes from a PHPDoc that you believe is inaccurate, you can turn off this check by setting treatPhpDocTypesAsCertain: false.