Error Identifier: arrayUnpacking.nonIterable
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);
$value = 'hello';
$array = [...$value];
Why is it reported? #
The spread operator (...) is used to unpack a value inside an array literal, but the value being unpacked is not iterable. Only arrays and Traversable objects can be unpacked in this context. PHP will throw a TypeError at runtime if a non-iterable value is unpacked.
In the example above, a string is being unpacked, which is not iterable.
How to fix it #
Ensure the value being unpacked is an array or iterable:
<?php declare(strict_types = 1);
-$value = 'hello';
+$value = ['h', 'e', 'l', 'l', 'o'];
$array = [...$value];
Or wrap it in an array if you want to include it as a single element:
<?php declare(strict_types = 1);
$value = 'hello';
-$array = [...$value];
+$array = [$value];
How to ignore this error #
You can use the identifier arrayUnpacking.nonIterable to ignore this error using a comment:
// @phpstan-ignore arrayUnpacking.nonIterable
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: arrayUnpacking.nonIterable
Rules that report this error #
- PHPStan\Rules\Arrays\UnpackIterableInArrayRule [1]