Error Identifier: arrayFilter.alwaysEmpty
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);
$values = [null, 0];
$filtered = array_filter($values);
Why is it reported? #
When array_filter() is called without a callback, PHP removes all falsy values from the array. PHPStan has determined that the array contains only falsy values (such as 0, '', null, false, or []), so the result will always be an empty array.
In the example above, the array contains null and 0, both of which are falsy, so array_filter() will always remove all elements.
How to fix it #
If the filtering is intentional, consider replacing the call with an empty array directly:
<?php declare(strict_types = 1);
$values = [null, 0];
-$filtered = array_filter($values);
+$filtered = [];
If you intended to filter by a specific condition, provide an explicit callback:
<?php declare(strict_types = 1);
$values = [null, 0];
-$filtered = array_filter($values);
+$filtered = array_filter($values, static function ($value): bool {
+ return $value !== null;
+});
If the array is expected to contain non-falsy values, fix the type of the source data:
<?php declare(strict_types = 1);
-$values = [null, 0];
+$values = [1, 2, 3];
$filtered = array_filter($values);
How to ignore this error #
You can use the identifier arrayFilter.alwaysEmpty to ignore this error using a comment:
// @phpstan-ignore arrayFilter.alwaysEmpty
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: arrayFilter.alwaysEmpty
Rules that report this error #
- PHPStan\Rules\Functions\ArrayFilterRule [1]