diff --git a/src/Psalm/Type/Atomic/TKeyedArray.php b/src/Psalm/Type/Atomic/TKeyedArray.php index 98915b7df..848ba5f6d 100644 --- a/src/Psalm/Type/Atomic/TKeyedArray.php +++ b/src/Psalm/Type/Atomic/TKeyedArray.php @@ -720,11 +720,19 @@ class TKeyedArray extends Atomic $quote = true; } + if (preg_match('/^[1-9][0-9]*_([0-9]+_)*[0-9]+$/', $name)) { + $quote = true; + } + // 08 should be quoted since it's numeric but it's handled as string and not cast to int if (preg_match('/^0[0-9]+$/', $name)) { $quote = true; } + if (preg_match('/^[0-9]+e-?[0-9]+$/', $name)) { + $quote = true; + } + if ($quote) { $name = '\'' . str_replace("\n", '\n', addslashes($name)) . '\''; } diff --git a/tests/ArrayKeysTest.php b/tests/ArrayKeysTest.php index 2f8593865..0f5087330 100644 --- a/tests/ArrayKeysTest.php +++ b/tests/ArrayKeysTest.php @@ -193,6 +193,8 @@ class ArrayKeysTest extends TestCase // see https://github.com/php/php-src/issues/9029#issuecomment-1186226676 $e = ["+15" => "a"]; $f = ["015" => "a"]; + $g = ["1e2" => "a"]; + $h = ["1_0" => "a"]; ', 'assertions' => [ '$a===' => "array{15: 'a'}", @@ -201,6 +203,8 @@ class ArrayKeysTest extends TestCase '$d===' => "array{-15: 'a'}", '$e===' => "array{'+15': 'a'}", '$f===' => "array{'015': 'a'}", + '$g===' => "array{'1e2': 'a'}", + '$h===' => "array{'1_0': 'a'}", ], ], ];