Skip to content

Commit

Permalink
scientific and underscore notation should be quoted too, since they w…
Browse files Browse the repository at this point in the history
…on't be type juggled
  • Loading branch information
kkmuffme committed Mar 23, 2024
1 parent cd302f0 commit 1bfa684
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Psalm/Type/Atomic/TKeyedArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,11 +720,19 @@ private function escapeAndQuote($name)
$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)) . '\'';
}
Expand Down
4 changes: 4 additions & 0 deletions tests/ArrayKeysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ public function test(bool $v): array {
// 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'}",
Expand All @@ -201,6 +203,8 @@ public function test(bool $v): array {
'$d===' => "array{-15: 'a'}",
'$e===' => "array{'+15': 'a'}",
'$f===' => "array{'015': 'a'}",
'$g===' => "array{'1e2': 'a'}",
'$h===' => "array{'1_0': 'a'}",
],
],
];
Expand Down

0 comments on commit 1bfa684

Please sign in to comment.