diff --git a/src/Psalm/Type/Atomic/TKeyedArray.php b/src/Psalm/Type/Atomic/TKeyedArray.php index aea960c83..c37c07d5f 100644 --- a/src/Psalm/Type/Atomic/TKeyedArray.php +++ b/src/Psalm/Type/Atomic/TKeyedArray.php @@ -430,7 +430,7 @@ class TKeyedArray extends Atomic */ 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 92f47dfa3..22442e8e1 100644 --- a/tests/AssertAnnotationTest.php +++ b/tests/AssertAnnotationTest.php @@ -1944,6 +1944,33 @@ class AssertAnnotationTest extends TestCase 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; + } + } + ', + ], ]; }