Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IntegrationPrinterWithPhpParserTest #255

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 10 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -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, array\\<PHPStan\\\\PhpDocParser\\\\Ast\\\\Node\\>\\|int\\|string\\>\\|null\\.$#"
message: '#^Cannot use array destructuring on list\<array\<PHPStan\\PhpDocParser\\Ast\\Node\>\|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
12 changes: 11 additions & 1 deletion tests/PHPStan/Printer/IntegrationPrinterWithPhpParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,10 +27,13 @@
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;

/**
* @return iterable<array{string, string, NodeVisitor}>
*/
Expand Down Expand Up @@ -73,7 +78,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);
Expand All @@ -85,6 +89,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 {

Expand Down Expand Up @@ -134,6 +143,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);
}
Expand Down
58 changes: 0 additions & 58 deletions tests/PHPStan/Printer/PhpPrinter.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpParser\Internal\TokenStream;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use PhpParser\NodeVisitorAbstract;
use function count;
use function preg_match;
Expand Down Expand Up @@ -71,7 +71,7 @@ public function enterNode(Node $node)
$this->indentCharacter = $char;
$this->indentSize = $size;

return NodeTraverser::STOP_TRAVERSAL;
return NodeVisitor::STOP_TRAVERSAL;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by deprecation fix

}

return null;
Expand Down
Loading