Error Identifier: offsetAssign.dimType
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 doFoo(): void
{
$value = 'Hello';
$value['foo'] = 'x'; // error: Cannot assign offset 'foo' to string.
}
Why is it reported? #
Strings in PHP support offset access only with integer keys (e.g., $str[0] = 'a'). Attempting to assign a value at a non-integer offset (like a string key) to a string variable is not valid. Similarly, appending with $str[] = ... is not supported for strings. This will result in a runtime error.
How to fix it #
If the variable should be an array, initialize it as one.
function doFoo(): void
{
- $value = 'Hello';
+ $value = [];
$value['foo'] = 'x';
}
If working with a string, use an integer offset.
function doFoo(): void
{
$value = 'Hello';
- $value['foo'] = 'x';
+ $value[0] = 'x';
}
How to ignore this error #
You can use the identifier offsetAssign.dimType to ignore this error using a comment:
// @phpstan-ignore offsetAssign.dimType
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: offsetAssign.dimType