1 <?php declare(strict_types=1);
5 use PhpParser\Node\Expr;
6 use PhpParser\Node\Name;
7 use PhpParser\Node\Scalar\DNumber;
8 use PhpParser\Node\Scalar\Encapsed;
9 use PhpParser\Node\Scalar\EncapsedStringPart;
10 use PhpParser\Node\Scalar\LNumber;
11 use PhpParser\Node\Scalar\String_;
12 use PhpParser\Node\Stmt;
13 use PhpParser\PrettyPrinter\Standard;
15 require_once __DIR__ . '/CodeTestAbstract.php';
17 class PrettyPrinterTest extends CodeTestAbstract
19 protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) {
20 $lexer = new Lexer\Emulative;
21 $parser5 = new Parser\Php5($lexer);
22 $parser7 = new Parser\Php7($lexer);
24 list($version, $options) = $this->parseModeLine($modeLine);
25 $prettyPrinter = new Standard($options);
28 $output5 = canonicalize($prettyPrinter->$method($parser5->parse($code)));
31 if ('php7' !== $version) {
37 $output7 = canonicalize($prettyPrinter->$method($parser7->parse($code)));
40 if ('php5' !== $version) {
45 if ('php5' === $version) {
46 $this->assertSame($expected, $output5, $name);
47 $this->assertNotSame($expected, $output7, $name);
48 } elseif ('php7' === $version) {
49 $this->assertSame($expected, $output7, $name);
50 $this->assertNotSame($expected, $output5, $name);
52 $this->assertSame($expected, $output5, $name);
53 $this->assertSame($expected, $output7, $name);
58 * @dataProvider provideTestPrettyPrint
59 * @covers \PhpParser\PrettyPrinter\Standard<extended>
61 public function testPrettyPrint($name, $code, $expected, $mode) {
62 $this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode);
66 * @dataProvider provideTestPrettyPrintFile
67 * @covers \PhpParser\PrettyPrinter\Standard<extended>
69 public function testPrettyPrintFile($name, $code, $expected, $mode) {
70 $this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected, $mode);
73 public function provideTestPrettyPrint() {
74 return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test');
77 public function provideTestPrettyPrintFile() {
78 return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'file-test');
81 public function testPrettyPrintExpr() {
82 $prettyPrinter = new Standard;
83 $expr = new Expr\BinaryOp\Mul(
84 new Expr\BinaryOp\Plus(new Expr\Variable('a'), new Expr\Variable('b')),
85 new Expr\Variable('c')
87 $this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr));
89 $expr = new Expr\Closure([
90 'stmts' => [new Stmt\Return_(new String_("a\nb"))]
92 $this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr));
95 public function testCommentBeforeInlineHTML() {
96 $prettyPrinter = new PrettyPrinter\Standard;
97 $comment = new Comment\Doc("/**\n * This is a comment\n */");
98 $stmts = [new Stmt\InlineHTML('Hello World!', ['comments' => [$comment]])];
99 $expected = "<?php\n\n/**\n * This is a comment\n */\n?>\nHello World!";
100 $this->assertSame($expected, $prettyPrinter->prettyPrintFile($stmts));
103 private function parseModeLine($modeLine) {
104 $parts = explode(' ', (string) $modeLine, 2);
105 $version = $parts[0] ?? 'both';
106 $options = isset($parts[1]) ? json_decode($parts[1], true) : [];
107 return [$version, $options];
110 public function testArraySyntaxDefault() {
111 $prettyPrinter = new Standard(['shortArraySyntax' => true]);
112 $expr = new Expr\Array_([
113 new Expr\ArrayItem(new String_('val'), new String_('key'))
115 $expected = "['key' => 'val']";
116 $this->assertSame($expected, $prettyPrinter->prettyPrintExpr($expr));
120 * @dataProvider provideTestKindAttributes
122 public function testKindAttributes($node, $expected) {
123 $prttyPrinter = new PrettyPrinter\Standard;
124 $result = $prttyPrinter->prettyPrintExpr($node);
125 $this->assertSame($expected, $result);
128 public function provideTestKindAttributes() {
129 $nowdoc = ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR'];
130 $heredoc = ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR'];
132 // Defaults to single quoted
133 [new String_('foo'), "'foo'"],
134 // Explicit single/double quoted
135 [new String_('foo', ['kind' => String_::KIND_SINGLE_QUOTED]), "'foo'"],
136 [new String_('foo', ['kind' => String_::KIND_DOUBLE_QUOTED]), '"foo"'],
137 // Fallback from doc string if no label
138 [new String_('foo', ['kind' => String_::KIND_NOWDOC]), "'foo'"],
139 [new String_('foo', ['kind' => String_::KIND_HEREDOC]), '"foo"'],
140 // Fallback if string contains label
141 [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'A']), "'A\nB\nC'"],
142 [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'B']), "'A\nB\nC'"],
143 [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'C']), "'A\nB\nC'"],
144 [new String_("STR;", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']), "'STR;'"],
145 // Doc string if label not contained (or not in ending position)
146 [new String_("foo", $nowdoc), "<<<'STR'\nfoo\nSTR\n"],
147 [new String_("foo", $heredoc), "<<<STR\nfoo\nSTR\n"],
148 [new String_("STRx", $nowdoc), "<<<'STR'\nSTRx\nSTR\n"],
149 [new String_("xSTR", $nowdoc), "<<<'STR'\nxSTR\nSTR\n"],
150 // Empty doc string variations (encapsed variant does not occur naturally)
151 [new String_("", $nowdoc), "<<<'STR'\nSTR\n"],
152 [new String_("", $heredoc), "<<<STR\nSTR\n"],
153 [new Encapsed([new EncapsedStringPart('')], $heredoc), "<<<STR\nSTR\n"],
154 // Encapsed doc string variations
155 [new Encapsed([new EncapsedStringPart('foo')], $heredoc), "<<<STR\nfoo\nSTR\n"],
156 [new Encapsed([new EncapsedStringPart('foo'), new Expr\Variable('y')], $heredoc), "<<<STR\nfoo{\$y}\nSTR\n"],
157 [new Encapsed([new EncapsedStringPart("\nSTR"), new Expr\Variable('y')], $heredoc), "<<<STR\n\nSTR{\$y}\nSTR\n"],
158 [new Encapsed([new EncapsedStringPart("\nSTR"), new Expr\Variable('y')], $heredoc), "<<<STR\n\nSTR{\$y}\nSTR\n"],
159 [new Encapsed([new Expr\Variable('y'), new EncapsedStringPart("STR\n")], $heredoc), "<<<STR\n{\$y}STR\n\nSTR\n"],
160 // Encapsed doc string fallback
161 [new Encapsed([new Expr\Variable('y'), new EncapsedStringPart("\nSTR")], $heredoc), '"{$y}\\nSTR"'],
162 [new Encapsed([new EncapsedStringPart("STR\n"), new Expr\Variable('y')], $heredoc), '"STR\\n{$y}"'],
163 [new Encapsed([new EncapsedStringPart("STR")], $heredoc), '"STR"'],
167 /** @dataProvider provideTestUnnaturalLiterals */
168 public function testUnnaturalLiterals($node, $expected) {
169 $prttyPrinter = new PrettyPrinter\Standard;
170 $result = $prttyPrinter->prettyPrintExpr($node);
171 $this->assertSame($expected, $result);
174 public function provideTestUnnaturalLiterals() {
176 [new LNumber(-1), '-1'],
177 [new LNumber(-PHP_INT_MAX - 1), '(-' . PHP_INT_MAX . '-1)'],
178 [new LNumber(-1, ['kind' => LNumber::KIND_BIN]), '-0b1'],
179 [new LNumber(-1, ['kind' => LNumber::KIND_OCT]), '-01'],
180 [new LNumber(-1, ['kind' => LNumber::KIND_HEX]), '-0x1'],
181 [new DNumber(\INF), '\INF'],
182 [new DNumber(-\INF), '-\INF'],
183 [new DNumber(-\NAN), '\NAN'],
187 public function testPrettyPrintWithError() {
188 $this->expectException(\LogicException::class);
189 $this->expectExceptionMessage('Cannot pretty-print AST with Error nodes');
190 $stmts = [new Stmt\Expression(
191 new Expr\PropertyFetch(new Expr\Variable('a'), new Expr\Error())
193 $prettyPrinter = new PrettyPrinter\Standard;
194 $prettyPrinter->prettyPrint($stmts);
197 public function testPrettyPrintWithErrorInClassConstFetch() {
198 $this->expectException(\LogicException::class);
199 $this->expectExceptionMessage('Cannot pretty-print AST with Error nodes');
200 $stmts = [new Stmt\Expression(
201 new Expr\ClassConstFetch(new Name('Foo'), new Expr\Error())
203 $prettyPrinter = new PrettyPrinter\Standard;
204 $prettyPrinter->prettyPrint($stmts);
207 public function testPrettyPrintEncapsedStringPart() {
208 $this->expectException(\LogicException::class);
209 $this->expectExceptionMessage('Cannot directly print EncapsedStringPart');
210 $expr = new Node\Scalar\EncapsedStringPart('foo');
211 $prettyPrinter = new PrettyPrinter\Standard;
212 $prettyPrinter->prettyPrintExpr($expr);
216 * @dataProvider provideTestFormatPreservingPrint
217 * @covers \PhpParser\PrettyPrinter\Standard<extended>
219 public function testFormatPreservingPrint($name, $code, $modification, $expected, $modeLine) {
220 $lexer = new Lexer\Emulative([
221 'usedAttributes' => [
223 'startLine', 'endLine',
224 'startTokenPos', 'endTokenPos',
228 $parser = new Parser\Php7($lexer);
229 $traverser = new NodeTraverser();
230 $traverser->addVisitor(new NodeVisitor\CloningVisitor());
232 $printer = new PrettyPrinter\Standard();
234 $oldStmts = $parser->parse($code);
235 $oldTokens = $lexer->getTokens();
237 $newStmts = $traverser->traverse($oldStmts);
239 /** @var callable $fn */
241 use PhpParser\Comment;
243 use PhpParser\Node\Expr;
244 use PhpParser\Node\Scalar;
245 use PhpParser\Node\Stmt;
246 \$fn = function(&\$stmts) { $modification };
251 $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
252 $this->assertSame(canonicalize($expected), canonicalize($newCode), $name);
255 public function provideTestFormatPreservingPrint() {
256 return $this->getTests(__DIR__ . '/../code/formatPreservation', 'test', 3);
260 * @dataProvider provideTestRoundTripPrint
261 * @covers \PhpParser\PrettyPrinter\Standard<extended>
263 public function testRoundTripPrint($name, $code, $expected, $modeLine) {
265 * This test makes sure that the format-preserving pretty printer round-trips for all
266 * the pretty printer tests (i.e. returns the input if no changes occurred).
269 list($version) = $this->parseModeLine($modeLine);
271 $lexer = new Lexer\Emulative([
272 'usedAttributes' => [
274 'startLine', 'endLine',
275 'startTokenPos', 'endTokenPos',
279 $parserClass = $version === 'php5' ? Parser\Php5::class : Parser\Php7::class;
280 /** @var Parser $parser */
281 $parser = new $parserClass($lexer);
283 $traverser = new NodeTraverser();
284 $traverser->addVisitor(new NodeVisitor\CloningVisitor());
286 $printer = new PrettyPrinter\Standard();
289 $oldStmts = $parser->parse($code);
291 // Can't do a format-preserving print on a file with errors
295 $oldTokens = $lexer->getTokens();
297 $newStmts = $traverser->traverse($oldStmts);
299 $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
300 $this->assertSame(canonicalize($code), canonicalize($newCode), $name);
303 public function provideTestRoundTripPrint() {
305 $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test'),
306 $this->getTests(__DIR__ . '/../code/parser', 'test')