Menu

Error Identifier: sortArray.empty

← Back to sortArray.*

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

function doFoo(): void
{
	$a = [];
	sort($a);
}

Why is it reported? #

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.

How to fix it #

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.

How to ignore this error #

You can use the identifier sortArray.empty to ignore this error using a comment:

// @phpstan-ignore sortArray.empty
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: sortArray.empty

Rules that report this error #

  • PHPStan\Rules\Functions\SortWithoutEffectRule [1]
Theme
A
© 2026 PHPStan s.r.o.