|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\PhpDocParser\Printer; |
| 4 | + |
| 5 | +use PHPStan\PhpDocParser\Ast\AbstractNodeVisitor; |
| 6 | +use PHPStan\PhpDocParser\Ast\Node; |
| 7 | +use PHPStan\PhpDocParser\Ast\NodeTraverser; |
| 8 | +use PHPStan\PhpDocParser\Ast\NodeVisitor; |
| 9 | +use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; |
| 10 | +use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
| 11 | +use PHPStan\PhpDocParser\Ast\Type\ObjectShapeItemNode; |
| 12 | +use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode; |
| 13 | +use PHPStan\PhpDocParser\Lexer\Lexer; |
| 14 | +use PHPStan\PhpDocParser\Parser\TokenIterator; |
| 15 | +use function array_splice; |
| 16 | +use function array_unshift; |
| 17 | +use function count; |
| 18 | + |
| 19 | +class PrintObjectWithSingleLineCommentTest extends PrinterTestBase |
| 20 | +{ |
| 21 | + |
| 22 | + /** |
| 23 | + * @return iterable<array{string, string}> |
| 24 | + */ |
| 25 | + public function dataPrintArrayFormatPreservingAddFront(): iterable |
| 26 | + { |
| 27 | + yield [ |
| 28 | + self::nowdoc(' |
| 29 | + /** |
| 30 | + * @param object{bar: string} $foo |
| 31 | + */'), |
| 32 | + self::nowdoc(' |
| 33 | + /** |
| 34 | + * @param object{// A fractional number |
| 35 | + * foo: float, |
| 36 | + * bar: string} $foo |
| 37 | + */'), |
| 38 | + ]; |
| 39 | + |
| 40 | + yield [ |
| 41 | + self::nowdoc(' |
| 42 | + /** |
| 43 | + * @param object{ |
| 44 | + * bar:string,naz:int} $foo |
| 45 | + */'), |
| 46 | + self::nowdoc(' |
| 47 | + /** |
| 48 | + * @param object{ |
| 49 | + * // A fractional number |
| 50 | + * foo: float, |
| 51 | + * bar:string,naz:int} $foo |
| 52 | + */'), |
| 53 | + ]; |
| 54 | + |
| 55 | + yield [ |
| 56 | + self::nowdoc(' |
| 57 | + /** |
| 58 | + * @param object{ |
| 59 | + * bar:string, |
| 60 | + * naz:int |
| 61 | + * } $foo |
| 62 | + */'), |
| 63 | + self::nowdoc(' |
| 64 | + /** |
| 65 | + * @param object{ |
| 66 | + * // A fractional number |
| 67 | + * foo: float, |
| 68 | + * bar:string, |
| 69 | + * naz:int |
| 70 | + * } $foo |
| 71 | + */'), |
| 72 | + ]; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @dataProvider dataPrintArrayFormatPreservingAddFront |
| 77 | + */ |
| 78 | + public function testPrintFormatPreservingSingleLineAddFront(string $phpDoc, string $expectedResult): void |
| 79 | + { |
| 80 | + $visitor = new class extends AbstractNodeVisitor { |
| 81 | + |
| 82 | + public function enterNode(Node $node) |
| 83 | + { |
| 84 | + if ($node instanceof ObjectShapeNode) { |
| 85 | + array_unshift($node->items, PrinterTestBase::withComment( |
| 86 | + new ObjectShapeItemNode(new IdentifierTypeNode('foo'), false, new IdentifierTypeNode('float')), |
| 87 | + '// A fractional number' |
| 88 | + )); |
| 89 | + } |
| 90 | + |
| 91 | + return $node; |
| 92 | + } |
| 93 | + |
| 94 | + }; |
| 95 | + |
| 96 | + $lexer = new Lexer(true); |
| 97 | + $tokens = new TokenIterator($lexer->tokenize($phpDoc)); |
| 98 | + $phpDocNode = $this->phpDocParser->parse($tokens); |
| 99 | + $cloningTraverser = new NodeTraverser([new NodeVisitor\CloningVisitor()]); |
| 100 | + $newNodes = $cloningTraverser->traverse([$phpDocNode]); |
| 101 | + |
| 102 | + $changingTraverser = new NodeTraverser([$visitor]); |
| 103 | + |
| 104 | + /** @var PhpDocNode $newNode */ |
| 105 | + [$newNode] = $changingTraverser->traverse($newNodes); |
| 106 | + |
| 107 | + $printer = new Printer(); |
| 108 | + $actualResult = $printer->printFormatPreserving($newNode, $phpDocNode, $tokens); |
| 109 | + $this->assertSame($expectedResult, $actualResult); |
| 110 | + |
| 111 | + $this->assertEquals( |
| 112 | + $this->unsetAttributes($newNode), |
| 113 | + $this->unsetAttributes($this->phpDocParser->parse(new TokenIterator($lexer->tokenize($actualResult)))) |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + /** |
| 119 | + * @return iterable<array{string, string}> |
| 120 | + */ |
| 121 | + public function dataPrintObjectFormatPreservingAddMiddle(): iterable |
| 122 | + { |
| 123 | + yield [ |
| 124 | + self::nowdoc(' |
| 125 | + /** |
| 126 | + * @param object{} $foo |
| 127 | + */'), |
| 128 | + self::nowdoc(' |
| 129 | + /** |
| 130 | + * @param object{bar: float} $foo |
| 131 | + */'), |
| 132 | + ]; |
| 133 | + |
| 134 | + yield [ |
| 135 | + self::nowdoc(' |
| 136 | + /** |
| 137 | + * @param object{foo:string} $foo |
| 138 | + */'), |
| 139 | + self::nowdoc(' |
| 140 | + /** |
| 141 | + * @param object{foo:string, |
| 142 | + * // A fractional number |
| 143 | + * bar: float} $foo |
| 144 | + */'), |
| 145 | + ]; |
| 146 | + |
| 147 | + yield [ |
| 148 | + self::nowdoc(' |
| 149 | + /** |
| 150 | + * @param object{ |
| 151 | + * foo:string,naz:int} $foo |
| 152 | + */'), |
| 153 | + self::nowdoc(' |
| 154 | + /** |
| 155 | + * @param object{ |
| 156 | + * foo:string, |
| 157 | + * // A fractional number |
| 158 | + * bar: float,naz:int} $foo |
| 159 | + */'), |
| 160 | + ]; |
| 161 | + |
| 162 | + yield [ |
| 163 | + self::nowdoc(' |
| 164 | + /** |
| 165 | + * @param object{ |
| 166 | + * foo:string, |
| 167 | + * naz:int |
| 168 | + * } $foo |
| 169 | + */'), |
| 170 | + self::nowdoc(' |
| 171 | + /** |
| 172 | + * @param object{ |
| 173 | + * foo:string, |
| 174 | + * // A fractional number |
| 175 | + * bar: float, |
| 176 | + * naz:int |
| 177 | + * } $foo |
| 178 | + */'), |
| 179 | + ]; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * @dataProvider dataPrintObjectFormatPreservingAddMiddle |
| 184 | + */ |
| 185 | + public function testPrintFormatPreservingSingleLineAddMiddle(string $phpDoc, string $expectedResult): void |
| 186 | + { |
| 187 | + $visitor = new class extends AbstractNodeVisitor { |
| 188 | + |
| 189 | + public function enterNode(Node $node) |
| 190 | + { |
| 191 | + if ($node instanceof ObjectShapeNode) { |
| 192 | + $newItem = PrinterTestBase::withComment( |
| 193 | + new ObjectShapeItemNode(new IdentifierTypeNode('bar'), false, new IdentifierTypeNode('float')), |
| 194 | + '// A fractional number' |
| 195 | + ); |
| 196 | + if (count($node->items) === 0) { |
| 197 | + $node->items[] = $newItem; |
| 198 | + } else { |
| 199 | + array_splice($node->items, 1, 0, [$newItem]); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + return $node; |
| 204 | + } |
| 205 | + |
| 206 | + }; |
| 207 | + |
| 208 | + $lexer = new Lexer(true); |
| 209 | + $tokens = new TokenIterator($lexer->tokenize($phpDoc)); |
| 210 | + $phpDocNode = $this->phpDocParser->parse($tokens); |
| 211 | + $cloningTraverser = new NodeTraverser([new NodeVisitor\CloningVisitor()]); |
| 212 | + $newNodes = $cloningTraverser->traverse([$phpDocNode]); |
| 213 | + |
| 214 | + $changingTraverser = new NodeTraverser([$visitor]); |
| 215 | + |
| 216 | + /** @var PhpDocNode $newNode */ |
| 217 | + [$newNode] = $changingTraverser->traverse($newNodes); |
| 218 | + |
| 219 | + $printer = new Printer(); |
| 220 | + $actualResult = $printer->printFormatPreserving($newNode, $phpDocNode, $tokens); |
| 221 | + $this->assertSame($expectedResult, $actualResult); |
| 222 | + |
| 223 | + $this->assertEquals( |
| 224 | + $this->unsetAttributes($newNode), |
| 225 | + $this->unsetAttributes($this->phpDocParser->parse(new TokenIterator($lexer->tokenize($actualResult)))) |
| 226 | + ); |
| 227 | + } |
| 228 | + |
| 229 | +} |
0 commit comments