|
| 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\CallableTypeNode; |
| 11 | +use PHPStan\PhpDocParser\Ast\Type\CallableTypeParameterNode; |
| 12 | +use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
| 13 | +use PHPStan\PhpDocParser\Lexer\Lexer; |
| 14 | +use PHPStan\PhpDocParser\Parser\TokenIterator; |
| 15 | +use function array_unshift; |
| 16 | + |
| 17 | +class PrintCallableWithSingleLineCommentTest extends PrinterTestBase |
| 18 | +{ |
| 19 | + |
| 20 | + /** |
| 21 | + * @return iterable<array{string, string}> |
| 22 | + */ |
| 23 | + public function dataAddCommentToParamsFront(): iterable |
| 24 | + { |
| 25 | + yield [ |
| 26 | + self::nowdoc(' |
| 27 | + /** |
| 28 | + * @param callable(Bar $bar): int $a |
| 29 | + */'), |
| 30 | + self::nowdoc(' |
| 31 | + /** |
| 32 | + * @param callable(// never pet a burning dog |
| 33 | + * Foo $foo, |
| 34 | + * Bar $bar): int $a |
| 35 | + */'), |
| 36 | + ]; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @dataProvider dataAddCommentToParamsFront |
| 41 | + */ |
| 42 | + public function testAddCommentToParamsFront(string $phpDoc, string $expectedResult): void |
| 43 | + { |
| 44 | + $visitor = new class extends AbstractNodeVisitor { |
| 45 | + |
| 46 | + public function enterNode(Node $node) |
| 47 | + { |
| 48 | + if ($node instanceof CallableTypeNode) { |
| 49 | + array_unshift($node->parameters, PrinterTestBase::withComment( |
| 50 | + new CallableTypeParameterNode(new IdentifierTypeNode('Foo'), false, false, '$foo', false), |
| 51 | + '// never pet a burning dog' |
| 52 | + )); |
| 53 | + } |
| 54 | + |
| 55 | + return $node; |
| 56 | + } |
| 57 | + |
| 58 | + }; |
| 59 | + |
| 60 | + $lexer = new Lexer(true); |
| 61 | + $tokens = new TokenIterator($lexer->tokenize($phpDoc)); |
| 62 | + $phpDocNode = $this->phpDocParser->parse($tokens); |
| 63 | + $cloningTraverser = new NodeTraverser([new NodeVisitor\CloningVisitor()]); |
| 64 | + $newNodes = $cloningTraverser->traverse([$phpDocNode]); |
| 65 | + |
| 66 | + $changingTraverser = new NodeTraverser([$visitor]); |
| 67 | + |
| 68 | + /** @var PhpDocNode $newNode */ |
| 69 | + [$newNode] = $changingTraverser->traverse($newNodes); |
| 70 | + |
| 71 | + $printer = new Printer(); |
| 72 | + $actualResult = $printer->printFormatPreserving($newNode, $phpDocNode, $tokens); |
| 73 | + $this->assertSame($expectedResult, $actualResult); |
| 74 | + |
| 75 | + $this->assertEquals( |
| 76 | + $this->unsetAttributes($newNode), |
| 77 | + $this->unsetAttributes($this->phpDocParser->parse(new TokenIterator($lexer->tokenize($actualResult)))) |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + /** |
| 83 | + * @return iterable<array{string, string}> |
| 84 | + */ |
| 85 | + public function dataPrintArrayFormatPreservingAddMiddle(): iterable |
| 86 | + { |
| 87 | + yield [ |
| 88 | + self::nowdoc(' |
| 89 | + /** |
| 90 | + * @param callable(Foo $foo): int $a |
| 91 | + */'), |
| 92 | + self::nowdoc(' |
| 93 | + /** |
| 94 | + * @param callable(Foo $foo, |
| 95 | + * // never pet a burning dog |
| 96 | + * Bar $bar): int $a |
| 97 | + */'), |
| 98 | + ]; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @dataProvider dataPrintArrayFormatPreservingAddMiddle |
| 103 | + */ |
| 104 | + public function testPrintFormatPreservingSingleLineAddMiddle(string $phpDoc, string $expectedResult): void |
| 105 | + { |
| 106 | + $visitor = new class extends AbstractNodeVisitor { |
| 107 | + |
| 108 | + public function enterNode(Node $node) |
| 109 | + { |
| 110 | + if ($node instanceof CallableTypeNode) { |
| 111 | + $node->parameters[] = PrinterTestBase::withComment( |
| 112 | + new CallableTypeParameterNode(new IdentifierTypeNode('Bar'), false, false, '$bar', false), |
| 113 | + '// never pet a burning dog' |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + return $node; |
| 118 | + } |
| 119 | + |
| 120 | + }; |
| 121 | + |
| 122 | + $lexer = new Lexer(true); |
| 123 | + $tokens = new TokenIterator($lexer->tokenize($phpDoc)); |
| 124 | + $phpDocNode = $this->phpDocParser->parse($tokens); |
| 125 | + $cloningTraverser = new NodeTraverser([new NodeVisitor\CloningVisitor()]); |
| 126 | + $newNodes = $cloningTraverser->traverse([$phpDocNode]); |
| 127 | + |
| 128 | + $changingTraverser = new NodeTraverser([$visitor]); |
| 129 | + |
| 130 | + /** @var PhpDocNode $newNode */ |
| 131 | + [$newNode] = $changingTraverser->traverse($newNodes); |
| 132 | + |
| 133 | + $printer = new Printer(); |
| 134 | + $actualResult = $printer->printFormatPreserving($newNode, $phpDocNode, $tokens); |
| 135 | + $this->assertSame($expectedResult, $actualResult); |
| 136 | + |
| 137 | + $this->assertEquals( |
| 138 | + $this->unsetAttributes($newNode), |
| 139 | + $this->unsetAttributes($this->phpDocParser->parse(new TokenIterator($lexer->tokenize($actualResult)))) |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments