diff --git a/src/Psalm/Type/Atomic/TKeyedArray.php b/src/Psalm/Type/Atomic/TKeyedArray.php index aea960c8346..c37c07d5fd0 100644 --- a/src/Psalm/Type/Atomic/TKeyedArray.php +++ b/src/Psalm/Type/Atomic/TKeyedArray.php @@ -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)) . '\''; } diff --git a/tests/AssertAnnotationTest.php b/tests/AssertAnnotationTest.php index 92f47dfa3a2..22442e8e1c5 100644 --- a/tests/AssertAnnotationTest.php +++ b/tests/AssertAnnotationTest.php @@ -1944,6 +1944,33 @@ function assertString(A $arg): bool {return $arg->b !== null;} function requiresString(string $_str): void {} ', ], + 'assertWithEmptyStringOnKeyedArray' => [ + ' ""]; + + /** @var array $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; + } + } + ', + ], ]; }