Skip to content

Commit 2cd0981

Browse files
committed
Quote array / object keys when necessary
Fixes #251
1 parent 5ceb0e3 commit 2cd0981

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/Printer/Printer.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@ public function printFormatPreserving(PhpDocNode $node, PhpDocNode $originalNode
186186
return $this->print($node);
187187
}
188188

189+
public function maybePrintQuoted(Node $node): string
190+
{
191+
$printed = $this->print($node);
192+
193+
if (!str_contains($printed, ' ')) {
194+
return $printed;
195+
}
196+
197+
return sprintf("'%s'", $printed);
198+
}
199+
189200
public function print(Node $node): string
190201
{
191202
if ($node instanceof PhpDocNode) {
@@ -382,7 +393,7 @@ private function printType(TypeNode $node): string
382393
if ($node->keyName !== null) {
383394
return sprintf(
384395
'%s%s: %s',
385-
$this->print($node->keyName),
396+
$this->maybePrintQuoted($node->keyName),
386397
$node->optional ? '?' : '',
387398
$this->printType($node->valueType)
388399
);
@@ -489,7 +500,7 @@ private function printType(TypeNode $node): string
489500
if ($node->keyName !== null) {
490501
return sprintf(
491502
'%s%s: %s',
492-
$this->print($node->keyName),
503+
$this->maybePrintQuoted($node->keyName),
493504
$node->optional ? '?' : '',
494505
$this->printType($node->valueType)
495506
);

tests/PHPStan/Printer/PrinterTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
2727
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
2828
use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasImportTagValueNode;
29+
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
2930
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
3031
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
3132
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeUnsealedTypeNode;
@@ -2064,4 +2065,24 @@ public function testPrintPhpDocNode(PhpDocNode $node, string $expectedResult): v
20642065
);
20652066
}
20662067

2068+
public function testMaybePrintQuoted(): void
2069+
{
2070+
$input = "array{states: array{'Hello World': int}, object{key: string, 'Other Key': int}}";
2071+
$expected = "array{states: array{'Hello World': int}, object{key: string, 'Other Key': int}}";
2072+
$lexer = new Lexer();
2073+
$constExprParser = new ConstExprParser(true, true);
2074+
$typeParser = new TypeParser($constExprParser);
2075+
$phpDocParser = new PhpDocParser($typeParser, $constExprParser);
2076+
2077+
$tokens = new TokenIterator($lexer->tokenize($input));
2078+
$phpDocNode = $phpDocParser->parseTagValue($tokens, '@var');
2079+
2080+
$this->assertInstanceOf(VarTagValueNode::class, $phpDocNode);
2081+
$this->assertInstanceOf(ArrayShapeNode::class, $phpDocNode->type);
2082+
2083+
$printer = new Printer();
2084+
$printed = $printer->print($phpDocNode);
2085+
$this->assertSame($expected, $printed);
2086+
}
2087+
20672088
}

0 commit comments

Comments
 (0)