Skip to content

Commit

Permalink
fix empty string in keyed array offset
Browse files Browse the repository at this point in the history
  • Loading branch information
orklah committed Jan 5, 2022
1 parent e41fc67 commit 7e2b9d0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TKeyedArray.php
Original file line number Diff line number Diff line change
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
26 changes: 26 additions & 0 deletions tests/AssertAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1921,6 +1921,32 @@ 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
* @psalm-assert =T $actual
*/
public function assertSame(mixed $expected, mixed $actual): void
{
return;
}
}
',
],
];
}

Expand Down

0 comments on commit 7e2b9d0

Please sign in to comment.