From 8552ceb2910462415a39a2122a84cbadb143d5a4 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 9 Oct 2024 10:24:57 +0200 Subject: [PATCH 1/5] Fix IntegrationPrinterWithPhpParserTest --- .../IntegrationPrinterWithPhpParserTest.php | 10 +++- tests/PHPStan/Printer/PhpPrinter.php | 58 ------------------- .../PhpPrinterIndentationDetectorVisitor.php | 3 +- 3 files changed, 11 insertions(+), 60 deletions(-) delete mode 100644 tests/PHPStan/Printer/PhpPrinter.php diff --git a/tests/PHPStan/Printer/IntegrationPrinterWithPhpParserTest.php b/tests/PHPStan/Printer/IntegrationPrinterWithPhpParserTest.php index 232bd145..a5df0ea0 100644 --- a/tests/PHPStan/Printer/IntegrationPrinterWithPhpParserTest.php +++ b/tests/PHPStan/Printer/IntegrationPrinterWithPhpParserTest.php @@ -4,11 +4,13 @@ use LogicException; use PhpParser\Comment\Doc; +use PhpParser\Internal\TokenStream; use PhpParser\Node as PhpNode; use PhpParser\NodeTraverser as PhpParserNodeTraverser; use PhpParser\NodeVisitor\CloningVisitor as PhpParserCloningVisitor; use PhpParser\NodeVisitorAbstract; use PhpParser\ParserFactory; +use PhpParser\PrettyPrinter\Standard; use PHPStan\PhpDocParser\Ast\AbstractNodeVisitor; use PHPStan\PhpDocParser\Ast\Node; use PHPStan\PhpDocParser\Ast\NodeTraverser; @@ -28,6 +30,7 @@ class IntegrationPrinterWithPhpParserTest extends TestCase { + private const TAB_WIDTH = 4; /** * @return iterable @@ -73,7 +76,6 @@ public function testPrint(string $file, string $expectedFile, NodeVisitor $visit $phpTraverser = new PhpParserNodeTraverser(); $phpTraverser->addVisitor(new PhpParserCloningVisitor()); - $printer = new PhpPrinter(); $fileContents = file_get_contents($file); if ($fileContents === false) { $this->fail('Could not read ' . $file); @@ -85,6 +87,11 @@ public function testPrint(string $file, string $expectedFile, NodeVisitor $visit } $oldTokens = $phpParser->getTokens(); + $phpTraverserIndent = new PhpParserNodeTraverser(); + $indentDetector = new PhpPrinterIndentationDetectorVisitor(new TokenStream($oldTokens, self::TAB_WIDTH)); + $phpTraverserIndent->addVisitor($indentDetector); + $phpTraverserIndent->traverse($oldStmts); + $phpTraverser2 = new PhpParserNodeTraverser(); $phpTraverser2->addVisitor(new class ($visitor) extends NodeVisitorAbstract { @@ -134,6 +141,7 @@ public function enterNode(PhpNode $phpNode) $newStmts = $phpTraverser->traverse($oldStmts); $newStmts = $phpTraverser2->traverse($newStmts); + $printer = new Standard(['indent' => str_repeat($indentDetector->indentCharacter, $indentDetector->indentSize)]); $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens); $this->assertStringEqualsFile($expectedFile, $newCode); } diff --git a/tests/PHPStan/Printer/PhpPrinter.php b/tests/PHPStan/Printer/PhpPrinter.php deleted file mode 100644 index e904313d..00000000 --- a/tests/PHPStan/Printer/PhpPrinter.php +++ /dev/null @@ -1,58 +0,0 @@ -indentCharacter = ' '; - $this->indentSize = 4; - } - - protected function preprocessNodes(array $nodes): void - { - parent::preprocessNodes($nodes); - if ($this->origTokens === null) { - return; - } - - $traverser = new NodeTraverser(); - - $visitor = new PhpPrinterIndentationDetectorVisitor($this->origTokens); - $traverser->addVisitor($visitor); - $traverser->traverse($nodes); - - $this->indentCharacter = $visitor->indentCharacter; - $this->indentSize = $visitor->indentSize; - } - - protected function setIndentLevel(int $level): void - { - $this->indentLevel = $level; - $this->nl = "\n" . str_repeat($this->indentCharacter, $level); - } - - protected function indent(): void - { - $this->indentLevel += $this->indentSize; - $this->nl = "\n" . str_repeat($this->indentCharacter, $this->indentLevel); - } - - protected function outdent(): void - { - $this->indentLevel -= $this->indentSize; - $this->nl = "\n" . str_repeat($this->indentCharacter, $this->indentLevel); - } - -} diff --git a/tests/PHPStan/Printer/PhpPrinterIndentationDetectorVisitor.php b/tests/PHPStan/Printer/PhpPrinterIndentationDetectorVisitor.php index d0da8f46..ef9524a6 100644 --- a/tests/PHPStan/Printer/PhpPrinterIndentationDetectorVisitor.php +++ b/tests/PHPStan/Printer/PhpPrinterIndentationDetectorVisitor.php @@ -5,6 +5,7 @@ use PhpParser\Internal\TokenStream; use PhpParser\Node; use PhpParser\NodeTraverser; +use PhpParser\NodeVisitor; use PhpParser\NodeVisitorAbstract; use function count; use function preg_match; @@ -71,7 +72,7 @@ public function enterNode(Node $node) $this->indentCharacter = $char; $this->indentSize = $size; - return NodeTraverser::STOP_TRAVERSAL; + return NodeVisitor::STOP_TRAVERSAL; } return null; From 635452c0a2fbf70b6b3228b2666452f728bf81c2 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 9 Oct 2024 10:30:53 +0200 Subject: [PATCH 2/5] rais min php-parser version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index bf3e95ec..8047c49e 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ }, "require-dev": { "doctrine/annotations": "^2.0", - "nikic/php-parser": "^5.1", + "nikic/php-parser": "^5.3.0", "php-parallel-lint/php-parallel-lint": "^1.2", "phpstan/extension-installer": "^1.0", "phpstan/phpstan": "^2.0", From fef8cbe4e2b58e0810d3124ae2c450628dbbbd0f Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 9 Oct 2024 10:36:15 +0200 Subject: [PATCH 3/5] Update PhpPrinterIndentationDetectorVisitor.php --- tests/PHPStan/Printer/PhpPrinterIndentationDetectorVisitor.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/PHPStan/Printer/PhpPrinterIndentationDetectorVisitor.php b/tests/PHPStan/Printer/PhpPrinterIndentationDetectorVisitor.php index ef9524a6..49987eb3 100644 --- a/tests/PHPStan/Printer/PhpPrinterIndentationDetectorVisitor.php +++ b/tests/PHPStan/Printer/PhpPrinterIndentationDetectorVisitor.php @@ -4,7 +4,6 @@ use PhpParser\Internal\TokenStream; use PhpParser\Node; -use PhpParser\NodeTraverser; use PhpParser\NodeVisitor; use PhpParser\NodeVisitorAbstract; use function count; From 9303ea4fcab211230f33fc91f45e739887a11cb0 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 9 Oct 2024 10:40:37 +0200 Subject: [PATCH 4/5] Update phpstan-baseline.neon --- phpstan-baseline.neon | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index d772636c..3bc8ae89 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,26 +1,31 @@ parameters: ignoreErrors: - - message: "#^Method PHPStan\\\\PhpDocParser\\\\Ast\\\\ConstExpr\\\\ConstExprStringNode\\:\\:escapeDoubleQuotedString\\(\\) should return string but returns string\\|null\\.$#" + message: '#^Method PHPStan\\PhpDocParser\\Ast\\ConstExpr\\ConstExprStringNode\:\:escapeDoubleQuotedString\(\) should return string but returns string\|null\.$#' + identifier: return.type count: 1 path: src/Ast/ConstExpr/ConstExprStringNode.php - - message: "#^Cannot use array destructuring on array\\\\|int\\|string\\>\\|null\\.$#" + message: '#^Cannot use array destructuring on list\\|int\|string\>\|null\.$#' + identifier: offsetAccess.nonArray count: 1 path: src/Ast/NodeTraverser.php - - message: "#^Variable property access on PHPStan\\\\PhpDocParser\\\\Ast\\\\Node\\.$#" + message: '#^Variable property access on PHPStan\\PhpDocParser\\Ast\\Node\.$#' + identifier: property.dynamicName count: 1 path: src/Ast/NodeTraverser.php - - message: "#^Method PHPStan\\\\PhpDocParser\\\\Parser\\\\StringUnescaper\\:\\:parseEscapeSequences\\(\\) should return string but returns string\\|null\\.$#" + message: '#^Method PHPStan\\PhpDocParser\\Parser\\StringUnescaper\:\:parseEscapeSequences\(\) should return string but returns string\|null\.$#' + identifier: return.type count: 1 path: src/Parser/StringUnescaper.php - - message: "#^Variable property access on PHPStan\\\\PhpDocParser\\\\Ast\\\\Node\\.$#" + message: '#^Variable property access on PHPStan\\PhpDocParser\\Ast\\Node\.$#' + identifier: property.dynamicName count: 2 path: src/Printer/Printer.php From 3d61f787f1ac2ba4a7dc179ebc9cb5c561a6fb27 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 9 Oct 2024 10:41:58 +0200 Subject: [PATCH 5/5] manual cs fix --- tests/PHPStan/Printer/IntegrationPrinterWithPhpParserTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/PHPStan/Printer/IntegrationPrinterWithPhpParserTest.php b/tests/PHPStan/Printer/IntegrationPrinterWithPhpParserTest.php index a5df0ea0..045156e8 100644 --- a/tests/PHPStan/Printer/IntegrationPrinterWithPhpParserTest.php +++ b/tests/PHPStan/Printer/IntegrationPrinterWithPhpParserTest.php @@ -27,9 +27,11 @@ use PHPStan\PhpDocParser\ParserConfig; use PHPUnit\Framework\TestCase; use function file_get_contents; +use function str_repeat; class IntegrationPrinterWithPhpParserTest extends TestCase { + private const TAB_WIDTH = 4; /**