Error Identifier: arrayFilter.same
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 list<positive-int> $numbers */
$numbers = [1, 2, 3];
$filtered = array_filter($numbers);
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’s value type does not contain any falsy values (such as 0, '', null, false, or []), so the call has no effect – the array will always stay the same after filtering.
In the example above, the array contains only positive integers, which are always truthy, so array_filter() cannot remove any elements.
How to fix it #
If the filtering is unnecessary, remove the array_filter() call:
<?php declare(strict_types = 1);
/** @var list<positive-int> $numbers */
$numbers = [1, 2, 3];
-$filtered = array_filter($numbers);
+$filtered = $numbers;
If you intended to filter by a specific condition, provide an explicit callback:
<?php declare(strict_types = 1);
/** @var list<positive-int> $numbers */
$numbers = [1, 2, 3];
-$filtered = array_filter($numbers);
+$filtered = array_filter($numbers, static function (int $value): bool {
+ return $value > 1;
+});
How to ignore this error #
You can use the identifier arrayFilter.same to ignore this error using a comment:
// @phpstan-ignore arrayFilter.same
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.same
Rules that report this error #
- PHPStan\Rules\Functions\ArrayFilterRule [1]