Menu

← Back to offsetAccess.*

Error Identifier: offsetAccess.nonArray

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(stdClass $obj): void
{
	[$a, $b] = $obj; // error: Cannot use array destructuring on stdClass.
}

Why is it reported? #

Array destructuring ([$a, $b] = ...) requires the right-hand side to be an array or an object implementing ArrayAccess. When the expression being destructured is neither of those types, PHP cannot access its elements by offset, which will result in an error at runtime.

How to fix it #

Ensure the value being destructured is an array.

-function doFoo(stdClass $obj): void
+function doFoo(array $data): void
 {
-	[$a, $b] = $obj;
+	[$a, $b] = $data;
 }

If the value can sometimes be null, check for it before destructuring.

 function doFoo(?array $data): void
 {
-	[$a, $b] = $data;
+	if ($data !== null) {
+		[$a, $b] = $data;
+	}
 }

How to ignore this error #

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

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

Rules that report this error #

  • PHPStan\Rules\Arrays\ArrayDestructuringRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.