Error Identifier: arrayValues.empty
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);
$result = array_values([]);
Why is it reported? #
The call to array_values() is made on an array that is always empty. Since array_values() re-indexes an array’s values with consecutive integer keys starting from 0, calling it on an empty array has no effect and always returns an empty array. This is dead code that serves no purpose.
How to fix it #
Remove the unnecessary array_values() call:
<?php declare(strict_types = 1);
-$result = array_values([]);
+$result = [];
Or if the variable was expected to contain elements, fix the upstream logic so the array is not always empty:
<?php declare(strict_types = 1);
/** @var array<string, int> $data */
$data = getData();
$result = array_values($data);
How to ignore this error #
You can use the identifier arrayValues.empty to ignore this error using a comment:
// @phpstan-ignore arrayValues.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: arrayValues.empty
Rules that report this error #
- PHPStan\Rules\Functions\ArrayValuesRule [1]