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 e8007b2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Psalm/Type/Atomic/TKeyedArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ private function escapeAndQuote($name)
if (is_string($name)) {
$quote = false;

if ($name === '' || preg_match('/[^a-zA-Z0-9_]/', $name)) {
if ($name === '' || preg_match('/[^a-zA-Z0-9-]/', $name)) {
$quote = true;
}

Expand All @@ -725,6 +725,10 @@ private function escapeAndQuote($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 e8007b2

Please sign in to comment.