Menu

Error Identifier: phpstanPlayground.arrayKeyCast

← Back to phpstanPlayground.*

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 = [
	'1' => 'one',   // key '1' (string) will be cast to 1 (int)
	null => 'empty', // key null will be cast to '' (string)
	2.5 => 'two',    // key 2.5 (float) will be cast to 2 (int)
	true => 'yes',   // key true (bool) will be cast to 1 (int)
	false => 'no',   // key false (bool) will be cast to 0 (int)
];

Why is it reported? #

PHP silently casts array keys to int or string when constructing arrays. A numeric string like '1' becomes the integer 1, null becomes the empty string '', floats are truncated to integers, and booleans become 0 or 1. This means the array may not have the keys the developer intended, and values can silently overwrite each other.

This rule is only active on the PHPStan playground.

Learn more: Why Array String Keys Are Not Type-Safe in PHP

How to fix it #

Use array keys of the type that PHP will actually store:

 $a = [
-	'1' => 'one',
+	1 => 'one',
 ];

For null keys, use the empty string explicitly:

 $a = [
-	null => 'empty',
+	'' => 'empty',
 ];

For float keys, use the truncated integer:

 $a = [
-	2.5 => 'two',
+	2 => 'two',
 ];

For boolean keys, use the corresponding integer:

 $a = [
-	true => 'yes',
-	false => 'no',
+	1 => 'yes',
+	0 => 'no',
 ];

Non-ignorable error #

This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.

Rules that report this error #

  • PHPStan\Rules\Playground\LiteralArrayKeyCastRule [1]
Theme
A
© 2026 PHPStan s.r.o.