Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix empty string not quoted in keyed array offset #7309

Merged
merged 2 commits into from Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -1921,6 +1921,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