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);
/** @param array{foo: int} $single */
function doFoo(array $single): void
{
ksort($single);
}
Why is it reported? #
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.
How to fix it #
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.
How to ignore this error #
You can use the identifier sortArray.noop to ignore this error using a comment:
// @phpstan-ignore sortArray.noop
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.noop
Rules that report this error #
- PHPStan\Rules\Functions\SortWithoutEffectRule [1]