Skip to content

Commit 8552ceb

Browse files
committed
Fix IntegrationPrinterWithPhpParserTest
1 parent 5260317 commit 8552ceb

File tree

3 files changed

+11
-60
lines changed

3 files changed

+11
-60
lines changed

tests/PHPStan/Printer/IntegrationPrinterWithPhpParserTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use LogicException;
66
use PhpParser\Comment\Doc;
7+
use PhpParser\Internal\TokenStream;
78
use PhpParser\Node as PhpNode;
89
use PhpParser\NodeTraverser as PhpParserNodeTraverser;
910
use PhpParser\NodeVisitor\CloningVisitor as PhpParserCloningVisitor;
1011
use PhpParser\NodeVisitorAbstract;
1112
use PhpParser\ParserFactory;
13+
use PhpParser\PrettyPrinter\Standard;
1214
use PHPStan\PhpDocParser\Ast\AbstractNodeVisitor;
1315
use PHPStan\PhpDocParser\Ast\Node;
1416
use PHPStan\PhpDocParser\Ast\NodeTraverser;
@@ -28,6 +30,7 @@
2830

2931
class IntegrationPrinterWithPhpParserTest extends TestCase
3032
{
33+
private const TAB_WIDTH = 4;
3134

3235
/**
3336
* @return iterable<array{string, string, NodeVisitor}>
@@ -73,7 +76,6 @@ public function testPrint(string $file, string $expectedFile, NodeVisitor $visit
7376
$phpTraverser = new PhpParserNodeTraverser();
7477
$phpTraverser->addVisitor(new PhpParserCloningVisitor());
7578

76-
$printer = new PhpPrinter();
7779
$fileContents = file_get_contents($file);
7880
if ($fileContents === false) {
7981
$this->fail('Could not read ' . $file);
@@ -85,6 +87,11 @@ public function testPrint(string $file, string $expectedFile, NodeVisitor $visit
8587
}
8688
$oldTokens = $phpParser->getTokens();
8789

90+
$phpTraverserIndent = new PhpParserNodeTraverser();
91+
$indentDetector = new PhpPrinterIndentationDetectorVisitor(new TokenStream($oldTokens, self::TAB_WIDTH));
92+
$phpTraverserIndent->addVisitor($indentDetector);
93+
$phpTraverserIndent->traverse($oldStmts);
94+
8895
$phpTraverser2 = new PhpParserNodeTraverser();
8996
$phpTraverser2->addVisitor(new class ($visitor) extends NodeVisitorAbstract {
9097

@@ -134,6 +141,7 @@ public function enterNode(PhpNode $phpNode)
134141
$newStmts = $phpTraverser->traverse($oldStmts);
135142
$newStmts = $phpTraverser2->traverse($newStmts);
136143

144+
$printer = new Standard(['indent' => str_repeat($indentDetector->indentCharacter, $indentDetector->indentSize)]);
137145
$newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
138146
$this->assertStringEqualsFile($expectedFile, $newCode);
139147
}

tests/PHPStan/Printer/PhpPrinter.php

Lines changed: 0 additions & 58 deletions
This file was deleted.

tests/PHPStan/Printer/PhpPrinterIndentationDetectorVisitor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Internal\TokenStream;
66
use PhpParser\Node;
77
use PhpParser\NodeTraverser;
8+
use PhpParser\NodeVisitor;
89
use PhpParser\NodeVisitorAbstract;
910
use function count;
1011
use function preg_match;
@@ -71,7 +72,7 @@ public function enterNode(Node $node)
7172
$this->indentCharacter = $char;
7273
$this->indentSize = $size;
7374

74-
return NodeTraverser::STOP_TRAVERSAL;
75+
return NodeVisitor::STOP_TRAVERSAL;
7576
}
7677

7778
return null;

0 commit comments

Comments
 (0)