Skip to content

Commit

Permalink
Merge pull request #7309 from orklah/emptyStringKeyedArray
Browse files Browse the repository at this point in the history
fix empty string not quoted in keyed array offset
  • Loading branch information
orklah committed Jan 6, 2022
2 parents 513b263 + 72216f9 commit 18ab5a0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TKeyedArray.php
Expand Up @@ -430,7 +430,7 @@ public function getList(): TNonEmptyList
*/
private function escapeAndQuote($name)
{
if (is_string($name) && preg_match('/[^a-zA-Z0-9_]/', $name)) {
if (is_string($name) && ($name === '' || preg_match('/[^a-zA-Z0-9_]/', $name))) {
$name = '\'' . str_replace("\n", '\n', addslashes($name)) . '\'';
}

Expand Down
27 changes: 27 additions & 0 deletions tests/AssertAnnotationTest.php
Expand Up @@ -1944,6 +1944,33 @@ function assertString(A $arg): bool {return $arg->b !== null;}
function requiresString(string $_str): void {}
',
],
'assertWithEmptyStringOnKeyedArray' => [
'<?php
class A
{
function test(): void
{
$a = ["" => ""];
/** @var array<string, mixed> $b */
$b = [];
$this->assertSame($a, $b);
}
/**
* @template T
* @param T $expected
* @param mixed $actual
* @psalm-assert =T $actual
*/
public function assertSame($expected, $actual): void
{
return;
}
}
',
],
];
}

Expand Down

0 comments on commit 18ab5a0

Please sign in to comment.