Error Identifier: variable.implicitArray
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);
function collectItems(): void
{
$items[] = 'first';
$items[] = 'second';
}
Why is it reported? #
This error is reported by the phpstan-strict-rules extension.
An array is being implicitly created by assigning to an array offset on a variable that has not been defined. In the example above, $items does not exist before the first assignment, so PHP implicitly creates an empty array. While PHP allows this behaviour, it can mask bugs where a variable was expected to already exist, or where a variable name was misspelled.
How to fix it #
Explicitly initialize the variable as an array before appending to it:
<?php declare(strict_types = 1);
function collectItems(): void
{
+ $items = [];
$items[] = 'first';
$items[] = 'second';
}
How to ignore this error #
You can use the identifier variable.implicitArray to ignore this error using a comment:
// @phpstan-ignore variable.implicitArray
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: variable.implicitArray