website/errors/sortArray.empty.md
<?php declare(strict_types = 1);
function doFoo(): void
{
$a = [];
sort($a);
}
In-place sort functions like sort(), ksort(), asort(), usort(), etc. reorder the elements of the array passed to them by reference. An empty array has no elements to reorder, so the call has no effect. This usually indicates a logic error — the array is sorted before it is populated, or the wrong variable is being sorted.
This check is part of PHPStan's bleeding edge and runs at rule level 5.
Remove the pointless sort call:
function doFoo(): void
{
$a = [];
- sort($a);
}
If the array is meant to contain elements, populate it before sorting:
function doFoo(): void
{
$a = [];
+ $a[] = 'b';
+ $a[] = 'a';
sort($a);
}
If the array type comes from a PHPDoc that you believe is inaccurate, you can turn off this check by setting treatPhpDocTypesAsCertain: false.