Error Identifier: offsetAccess.noDim
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);
$array = [1, 2, 3];
$value = $array[];
Why is it reported? #
The empty dimension syntax $array[] is being used for reading. In PHP, $array[] without a dimension index is only valid on the left side of an assignment (e.g., $array[] = $value), where it appends a new element. Using it for reading has no meaning and will produce a fatal error at runtime.
How to fix it #
Specify an explicit array index or key to access the desired element.
<?php declare(strict_types = 1);
$array = [1, 2, 3];
-$value = $array[];
+$value = $array[0];
How to ignore this error #
You can use the identifier offsetAccess.noDim to ignore this error using a comment:
// @phpstan-ignore offsetAccess.noDim
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: offsetAccess.noDim
Rules that report this error #
- PHPStan\Rules\Arrays\OffsetAccessWithoutDimForReadingRule [1]