4
4
5
5
use LogicException ;
6
6
use PhpParser \Comment \Doc ;
7
+ use PhpParser \Internal \TokenStream ;
7
8
use PhpParser \Node as PhpNode ;
8
9
use PhpParser \NodeTraverser as PhpParserNodeTraverser ;
9
10
use PhpParser \NodeVisitor \CloningVisitor as PhpParserCloningVisitor ;
10
11
use PhpParser \NodeVisitorAbstract ;
11
12
use PhpParser \ParserFactory ;
13
+ use PhpParser \PrettyPrinter \Standard ;
12
14
use PHPStan \PhpDocParser \Ast \AbstractNodeVisitor ;
13
15
use PHPStan \PhpDocParser \Ast \Node ;
14
16
use PHPStan \PhpDocParser \Ast \NodeTraverser ;
28
30
29
31
class IntegrationPrinterWithPhpParserTest extends TestCase
30
32
{
33
+ private const TAB_WIDTH = 4 ;
31
34
32
35
/**
33
36
* @return iterable<array{string, string, NodeVisitor}>
@@ -73,7 +76,6 @@ public function testPrint(string $file, string $expectedFile, NodeVisitor $visit
73
76
$ phpTraverser = new PhpParserNodeTraverser ();
74
77
$ phpTraverser ->addVisitor (new PhpParserCloningVisitor ());
75
78
76
- $ printer = new PhpPrinter ();
77
79
$ fileContents = file_get_contents ($ file );
78
80
if ($ fileContents === false ) {
79
81
$ this ->fail ('Could not read ' . $ file );
@@ -85,6 +87,11 @@ public function testPrint(string $file, string $expectedFile, NodeVisitor $visit
85
87
}
86
88
$ oldTokens = $ phpParser ->getTokens ();
87
89
90
+ $ phpTraverserIndent = new PhpParserNodeTraverser ();
91
+ $ indentDetector = new PhpPrinterIndentationDetectorVisitor (new TokenStream ($ oldTokens , self ::TAB_WIDTH ));
92
+ $ phpTraverserIndent ->addVisitor ($ indentDetector );
93
+ $ phpTraverserIndent ->traverse ($ oldStmts );
94
+
88
95
$ phpTraverser2 = new PhpParserNodeTraverser ();
89
96
$ phpTraverser2 ->addVisitor (new class ($ visitor ) extends NodeVisitorAbstract {
90
97
@@ -134,6 +141,7 @@ public function enterNode(PhpNode $phpNode)
134
141
$ newStmts = $ phpTraverser ->traverse ($ oldStmts );
135
142
$ newStmts = $ phpTraverser2 ->traverse ($ newStmts );
136
143
144
+ $ printer = new Standard (['indent ' => str_repeat ($ indentDetector ->indentCharacter , $ indentDetector ->indentSize )]);
137
145
$ newCode = $ printer ->printFormatPreserving ($ newStmts , $ oldStmts , $ oldTokens );
138
146
$ this ->assertStringEqualsFile ($ expectedFile , $ newCode );
139
147
}
0 commit comments