Menu

← Back to offsetAccess.*

Error Identifier: offsetAccess.invalidOffset

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);

$a = [];

$foo = $a[new DateTimeImmutable()];
$a[[]] = $foo;

Why is it reported? #

The value used as an array key is not a valid array key type. PHP arrays only accept int and string as keys. Using objects, arrays, or other types as array keys results in a runtime error.

Types that are implicitly converted to int or string (like bool, float, or null) may also be reported depending on the PHP version and PHPStan configuration, because these implicit conversions are often unintentional and can lead to subtle bugs.

How to fix it #

Convert the value to a valid array key type:

-$foo = $a[new DateTimeImmutable()];
+$foo = $a[$dateTime->format('Y-m-d')];
-$a[[]] = $foo;
+$a[implode(',', $keys)] = $foo;

If you need to use objects as keys, use SplObjectStorage or WeakMap:

-$a = [];
-$a[new DateTimeImmutable()] = 'value';
+$a = new SplObjectStorage();
+$a[new DateTimeImmutable()] = 'value';

How to ignore this error #

You can use the identifier offsetAccess.invalidOffset to ignore this error using a comment:

// @phpstan-ignore offsetAccess.invalidOffset
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.invalidOffset

Rules that report this error #

  • PHPStan\Rules\Arrays\InvalidKeyInArrayDimFetchRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.