website/errors/sortArray.singleElement.md
<?php declare(strict_types = 1);
/** @param array{foo: int} $single */
function doFoo(array $single): void
{
ksort($single);
}
In-place sort functions like ksort(), asort(), arsort(), usort(), etc. reorder the elements of the array passed to them by reference. An array with at most one element has nothing to reorder, so the call has no effect. This usually indicates a logic error — for example, sorting the wrong variable, or an array type that is narrower than intended.
This check is part of PHPStan's bleeding edge and runs at rule level 5.
Remove the pointless sort call:
/** @param array{foo: int} $single */
function doFoo(array $single): void
{
- ksort($single);
}
If the array is expected to hold more than one element, correct its type so it reflects that:
-/** @param array{foo: int} $single */
+/** @param array<string, int> $single */
function doFoo(array $single): void
{
ksort($single);
}
If the array type comes from a PHPDoc that you believe is inaccurate, you can turn off this check by setting treatPhpDocTypesAsCertain: false.