From 1bfa684d7f805f20c9c474ab0bb59994a572d09e Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Sat, 23 Mar 2024 01:01:51 +0100 Subject: [PATCH] scientific and underscore notation should be quoted too, since they won't be type juggled --- src/Psalm/Type/Atomic/TKeyedArray.php | 8 ++++++++ tests/ArrayKeysTest.php | 4 ++++ 2 files changed, 12 insertions(+) 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'}", ], ], ];