Error Identifier: arrayFilter.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);
/** @var array{} $empty */
$empty = [];
array_filter($empty);
Why is it reported? #
Calling array_filter() without a callback on an empty array has no effect. The array is already empty, so there are no elements to filter. This is likely a sign that the variable is not populated as intended, or the array_filter() call is unnecessary.
How to fix it #
Remove the unnecessary array_filter() call if the array is intentionally empty:
<?php declare(strict_types = 1);
-$result = array_filter([]);
+$result = [];
Or ensure the array is populated before filtering:
<?php declare(strict_types = 1);
-/** @var array{} $items */
-$items = [];
+/** @var array<int, string|null> $items */
+$items = getItems();
array_filter($items);
How to ignore this error #
You can use the identifier arrayFilter.empty to ignore this error using a comment:
// @phpstan-ignore arrayFilter.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: arrayFilter.empty
Rules that report this error #
- PHPStan\Rules\Functions\ArrayFilterRule [1]