PrettyPrinterTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. use PhpParser\Node\Expr;
  4. use PhpParser\Node\Name;
  5. use PhpParser\Node\Scalar\DNumber;
  6. use PhpParser\Node\Scalar\Encapsed;
  7. use PhpParser\Node\Scalar\EncapsedStringPart;
  8. use PhpParser\Node\Scalar\LNumber;
  9. use PhpParser\Node\Scalar\String_;
  10. use PhpParser\Node\Stmt;
  11. use PhpParser\PrettyPrinter\Standard;
  12. require_once __DIR__ . '/CodeTestAbstract.php';
  13. class PrettyPrinterTest extends CodeTestAbstract
  14. {
  15. protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) {
  16. $lexer = new Lexer\Emulative;
  17. $parser5 = new Parser\Php5($lexer);
  18. $parser7 = new Parser\Php7($lexer);
  19. list($version, $options) = $this->parseModeLine($modeLine);
  20. $prettyPrinter = new Standard($options);
  21. try {
  22. $output5 = canonicalize($prettyPrinter->$method($parser5->parse($code)));
  23. } catch (Error $e) {
  24. $output5 = null;
  25. if ('php7' !== $version) {
  26. throw $e;
  27. }
  28. }
  29. try {
  30. $output7 = canonicalize($prettyPrinter->$method($parser7->parse($code)));
  31. } catch (Error $e) {
  32. $output7 = null;
  33. if ('php5' !== $version) {
  34. throw $e;
  35. }
  36. }
  37. if ('php5' === $version) {
  38. $this->assertSame($expected, $output5, $name);
  39. $this->assertNotSame($expected, $output7, $name);
  40. } elseif ('php7' === $version) {
  41. $this->assertSame($expected, $output7, $name);
  42. $this->assertNotSame($expected, $output5, $name);
  43. } else {
  44. $this->assertSame($expected, $output5, $name);
  45. $this->assertSame($expected, $output7, $name);
  46. }
  47. }
  48. /**
  49. * @dataProvider provideTestPrettyPrint
  50. * @covers \PhpParser\PrettyPrinter\Standard<extended>
  51. */
  52. public function testPrettyPrint($name, $code, $expected, $mode) {
  53. $this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode);
  54. }
  55. /**
  56. * @dataProvider provideTestPrettyPrintFile
  57. * @covers \PhpParser\PrettyPrinter\Standard<extended>
  58. */
  59. public function testPrettyPrintFile($name, $code, $expected, $mode) {
  60. $this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected, $mode);
  61. }
  62. public function provideTestPrettyPrint() {
  63. return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test');
  64. }
  65. public function provideTestPrettyPrintFile() {
  66. return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'file-test');
  67. }
  68. public function testPrettyPrintExpr() {
  69. $prettyPrinter = new Standard;
  70. $expr = new Expr\BinaryOp\Mul(
  71. new Expr\BinaryOp\Plus(new Expr\Variable('a'), new Expr\Variable('b')),
  72. new Expr\Variable('c')
  73. );
  74. $this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr));
  75. $expr = new Expr\Closure([
  76. 'stmts' => [new Stmt\Return_(new String_("a\nb"))]
  77. ]);
  78. $this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr));
  79. }
  80. public function testCommentBeforeInlineHTML() {
  81. $prettyPrinter = new PrettyPrinter\Standard;
  82. $comment = new Comment\Doc("/**\n * This is a comment\n */");
  83. $stmts = [new Stmt\InlineHTML('Hello World!', ['comments' => [$comment]])];
  84. $expected = "<?php\n\n/**\n * This is a comment\n */\n?>\nHello World!";
  85. $this->assertSame($expected, $prettyPrinter->prettyPrintFile($stmts));
  86. }
  87. private function parseModeLine($modeLine) {
  88. $parts = explode(' ', (string) $modeLine, 2);
  89. $version = $parts[0] ?? 'both';
  90. $options = isset($parts[1]) ? json_decode($parts[1], true) : [];
  91. return [$version, $options];
  92. }
  93. public function testArraySyntaxDefault() {
  94. $prettyPrinter = new Standard(['shortArraySyntax' => true]);
  95. $expr = new Expr\Array_([
  96. new Expr\ArrayItem(new String_('val'), new String_('key'))
  97. ]);
  98. $expected = "['key' => 'val']";
  99. $this->assertSame($expected, $prettyPrinter->prettyPrintExpr($expr));
  100. }
  101. /**
  102. * @dataProvider provideTestKindAttributes
  103. */
  104. public function testKindAttributes($node, $expected) {
  105. $prttyPrinter = new PrettyPrinter\Standard;
  106. $result = $prttyPrinter->prettyPrintExpr($node);
  107. $this->assertSame($expected, $result);
  108. }
  109. public function provideTestKindAttributes() {
  110. $nowdoc = ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR'];
  111. $heredoc = ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR'];
  112. return [
  113. // Defaults to single quoted
  114. [new String_('foo'), "'foo'"],
  115. // Explicit single/double quoted
  116. [new String_('foo', ['kind' => String_::KIND_SINGLE_QUOTED]), "'foo'"],
  117. [new String_('foo', ['kind' => String_::KIND_DOUBLE_QUOTED]), '"foo"'],
  118. // Fallback from doc string if no label
  119. [new String_('foo', ['kind' => String_::KIND_NOWDOC]), "'foo'"],
  120. [new String_('foo', ['kind' => String_::KIND_HEREDOC]), '"foo"'],
  121. // Fallback if string contains label
  122. [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'A']), "'A\nB\nC'"],
  123. [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'B']), "'A\nB\nC'"],
  124. [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'C']), "'A\nB\nC'"],
  125. [new String_("STR;", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']), "'STR;'"],
  126. // Doc string if label not contained (or not in ending position)
  127. [new String_("foo", $nowdoc), "<<<'STR'\nfoo\nSTR\n"],
  128. [new String_("foo", $heredoc), "<<<STR\nfoo\nSTR\n"],
  129. [new String_("STRx", $nowdoc), "<<<'STR'\nSTRx\nSTR\n"],
  130. [new String_("xSTR", $nowdoc), "<<<'STR'\nxSTR\nSTR\n"],
  131. // Empty doc string variations (encapsed variant does not occur naturally)
  132. [new String_("", $nowdoc), "<<<'STR'\nSTR\n"],
  133. [new String_("", $heredoc), "<<<STR\nSTR\n"],
  134. [new Encapsed([new EncapsedStringPart('')], $heredoc), "<<<STR\nSTR\n"],
  135. // Encapsed doc string variations
  136. [new Encapsed([new EncapsedStringPart('foo')], $heredoc), "<<<STR\nfoo\nSTR\n"],
  137. [new Encapsed([new EncapsedStringPart('foo'), new Expr\Variable('y')], $heredoc), "<<<STR\nfoo{\$y}\nSTR\n"],
  138. [new Encapsed([new EncapsedStringPart("\nSTR"), new Expr\Variable('y')], $heredoc), "<<<STR\n\nSTR{\$y}\nSTR\n"],
  139. [new Encapsed([new EncapsedStringPart("\nSTR"), new Expr\Variable('y')], $heredoc), "<<<STR\n\nSTR{\$y}\nSTR\n"],
  140. [new Encapsed([new Expr\Variable('y'), new EncapsedStringPart("STR\n")], $heredoc), "<<<STR\n{\$y}STR\n\nSTR\n"],
  141. // Encapsed doc string fallback
  142. [new Encapsed([new Expr\Variable('y'), new EncapsedStringPart("\nSTR")], $heredoc), '"{$y}\\nSTR"'],
  143. [new Encapsed([new EncapsedStringPart("STR\n"), new Expr\Variable('y')], $heredoc), '"STR\\n{$y}"'],
  144. [new Encapsed([new EncapsedStringPart("STR")], $heredoc), '"STR"'],
  145. ];
  146. }
  147. /** @dataProvider provideTestUnnaturalLiterals */
  148. public function testUnnaturalLiterals($node, $expected) {
  149. $prttyPrinter = new PrettyPrinter\Standard;
  150. $result = $prttyPrinter->prettyPrintExpr($node);
  151. $this->assertSame($expected, $result);
  152. }
  153. public function provideTestUnnaturalLiterals() {
  154. return [
  155. [new LNumber(-1), '-1'],
  156. [new LNumber(-PHP_INT_MAX - 1), '(-' . PHP_INT_MAX . '-1)'],
  157. [new LNumber(-1, ['kind' => LNumber::KIND_BIN]), '-0b1'],
  158. [new LNumber(-1, ['kind' => LNumber::KIND_OCT]), '-01'],
  159. [new LNumber(-1, ['kind' => LNumber::KIND_HEX]), '-0x1'],
  160. [new DNumber(\INF), '\INF'],
  161. [new DNumber(-\INF), '-\INF'],
  162. [new DNumber(-\NAN), '\NAN'],
  163. ];
  164. }
  165. public function testPrettyPrintWithError() {
  166. $this->expectException(\LogicException::class);
  167. $this->expectExceptionMessage('Cannot pretty-print AST with Error nodes');
  168. $stmts = [new Stmt\Expression(
  169. new Expr\PropertyFetch(new Expr\Variable('a'), new Expr\Error())
  170. )];
  171. $prettyPrinter = new PrettyPrinter\Standard;
  172. $prettyPrinter->prettyPrint($stmts);
  173. }
  174. public function testPrettyPrintWithErrorInClassConstFetch() {
  175. $this->expectException(\LogicException::class);
  176. $this->expectExceptionMessage('Cannot pretty-print AST with Error nodes');
  177. $stmts = [new Stmt\Expression(
  178. new Expr\ClassConstFetch(new Name('Foo'), new Expr\Error())
  179. )];
  180. $prettyPrinter = new PrettyPrinter\Standard;
  181. $prettyPrinter->prettyPrint($stmts);
  182. }
  183. public function testPrettyPrintEncapsedStringPart() {
  184. $this->expectException(\LogicException::class);
  185. $this->expectExceptionMessage('Cannot directly print EncapsedStringPart');
  186. $expr = new Node\Scalar\EncapsedStringPart('foo');
  187. $prettyPrinter = new PrettyPrinter\Standard;
  188. $prettyPrinter->prettyPrintExpr($expr);
  189. }
  190. /**
  191. * @dataProvider provideTestFormatPreservingPrint
  192. * @covers \PhpParser\PrettyPrinter\Standard<extended>
  193. */
  194. public function testFormatPreservingPrint($name, $code, $modification, $expected, $modeLine) {
  195. $lexer = new Lexer\Emulative([
  196. 'usedAttributes' => [
  197. 'comments',
  198. 'startLine', 'endLine',
  199. 'startTokenPos', 'endTokenPos',
  200. ],
  201. ]);
  202. $parser = new Parser\Php7($lexer);
  203. $traverser = new NodeTraverser();
  204. $traverser->addVisitor(new NodeVisitor\CloningVisitor());
  205. $printer = new PrettyPrinter\Standard();
  206. $oldStmts = $parser->parse($code);
  207. $oldTokens = $lexer->getTokens();
  208. $newStmts = $traverser->traverse($oldStmts);
  209. /** @var callable $fn */
  210. eval(<<<CODE
  211. use PhpParser\Comment;
  212. use PhpParser\Node;
  213. use PhpParser\Node\Expr;
  214. use PhpParser\Node\Scalar;
  215. use PhpParser\Node\Stmt;
  216. \$fn = function(&\$stmts) { $modification };
  217. CODE
  218. );
  219. $fn($newStmts);
  220. $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
  221. $this->assertSame(canonicalize($expected), canonicalize($newCode), $name);
  222. }
  223. public function provideTestFormatPreservingPrint() {
  224. return $this->getTests(__DIR__ . '/../code/formatPreservation', 'test', 3);
  225. }
  226. /**
  227. * @dataProvider provideTestRoundTripPrint
  228. * @covers \PhpParser\PrettyPrinter\Standard<extended>
  229. */
  230. public function testRoundTripPrint($name, $code, $expected, $modeLine) {
  231. /**
  232. * This test makes sure that the format-preserving pretty printer round-trips for all
  233. * the pretty printer tests (i.e. returns the input if no changes occurred).
  234. */
  235. list($version) = $this->parseModeLine($modeLine);
  236. $lexer = new Lexer\Emulative([
  237. 'usedAttributes' => [
  238. 'comments',
  239. 'startLine', 'endLine',
  240. 'startTokenPos', 'endTokenPos',
  241. ],
  242. ]);
  243. $parserClass = $version === 'php5' ? Parser\Php5::class : Parser\Php7::class;
  244. /** @var Parser $parser */
  245. $parser = new $parserClass($lexer);
  246. $traverser = new NodeTraverser();
  247. $traverser->addVisitor(new NodeVisitor\CloningVisitor());
  248. $printer = new PrettyPrinter\Standard();
  249. try {
  250. $oldStmts = $parser->parse($code);
  251. } catch (Error $e) {
  252. // Can't do a format-preserving print on a file with errors
  253. return;
  254. }
  255. $oldTokens = $lexer->getTokens();
  256. $newStmts = $traverser->traverse($oldStmts);
  257. $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
  258. $this->assertSame(canonicalize($code), canonicalize($newCode), $name);
  259. }
  260. public function provideTestRoundTripPrint() {
  261. return array_merge(
  262. $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test'),
  263. $this->getTests(__DIR__ . '/../code/parser', 'test')
  264. );
  265. }
  266. }