CodeParsingTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. use PhpParser\Node\Expr;
  4. use PhpParser\Node\Stmt;
  5. class CodeParsingTest extends CodeTestAbstract {
  6. /**
  7. * @dataProvider provideTestParse
  8. */
  9. public function testParse($name, $code, $expected, $modeLine): void {
  10. $modes = $this->parseModeLine($modeLine);
  11. $parser = $this->createParser($modes['version'] ?? null);
  12. list($stmts, $output) = $this->getParseOutput($parser, $code, $modes);
  13. $this->assertSame($expected, $output, $name);
  14. $this->checkAttributes($stmts);
  15. }
  16. public function createParser(?string $version): Parser {
  17. $factory = new ParserFactory();
  18. $version = $version === null
  19. ? PhpVersion::getNewestSupported() : PhpVersion::fromString($version);
  20. return $factory->createForVersion($version);
  21. }
  22. // Must be public for updateTests.php
  23. public function getParseOutput(Parser $parser, $code, array $modes) {
  24. $dumpPositions = isset($modes['positions']);
  25. $dumpOtherAttributes = isset($modes['attributes']);
  26. $errors = new ErrorHandler\Collecting();
  27. $stmts = $parser->parse($code, $errors);
  28. $output = '';
  29. foreach ($errors->getErrors() as $error) {
  30. $output .= $this->formatErrorMessage($error, $code) . "\n";
  31. }
  32. if (null !== $stmts) {
  33. $dumper = new NodeDumper([
  34. 'dumpComments' => true,
  35. 'dumpPositions' => $dumpPositions,
  36. 'dumpOtherAttributes' => $dumpOtherAttributes,
  37. ]);
  38. $output .= $dumper->dump($stmts, $code);
  39. }
  40. return [$stmts, canonicalize($output)];
  41. }
  42. public static function provideTestParse() {
  43. return self::getTests(__DIR__ . '/../code/parser', 'test');
  44. }
  45. private function formatErrorMessage(Error $e, $code) {
  46. if ($e->hasColumnInfo()) {
  47. return $e->getMessageWithColumnInfo($code);
  48. }
  49. return $e->getMessage();
  50. }
  51. private function checkAttributes($stmts): void {
  52. if ($stmts === null) {
  53. return;
  54. }
  55. $traverser = new NodeTraverser(new class () extends NodeVisitorAbstract {
  56. public function enterNode(Node $node): void {
  57. $startLine = $node->getStartLine();
  58. $endLine = $node->getEndLine();
  59. $startFilePos = $node->getStartFilePos();
  60. $endFilePos = $node->getEndFilePos();
  61. $startTokenPos = $node->getStartTokenPos();
  62. $endTokenPos = $node->getEndTokenPos();
  63. if ($startLine < 0 || $endLine < 0 ||
  64. $startFilePos < 0 || $endFilePos < 0 ||
  65. $startTokenPos < 0 || $endTokenPos < 0
  66. ) {
  67. throw new \Exception('Missing location information on ' . $node->getType());
  68. }
  69. if ($endLine < $startLine ||
  70. $endFilePos < $startFilePos ||
  71. $endTokenPos < $startTokenPos
  72. ) {
  73. // Nop and Error can have inverted order, if they are empty.
  74. // This can also happen for a Param containing an Error.
  75. if (!$node instanceof Stmt\Nop && !$node instanceof Expr\Error &&
  76. !$node instanceof Node\Param
  77. ) {
  78. throw new \Exception('End < start on ' . $node->getType());
  79. }
  80. }
  81. }
  82. });
  83. $traverser->traverse($stmts);
  84. }
  85. }