File: CodeParsingTest.php

package info (click to toggle)
php-parser 5.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,532 kB
  • sloc: php: 23,585; yacc: 1,272; makefile: 39; sh: 8
file content (100 lines) | stat: -rw-r--r-- 3,636 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php declare(strict_types=1);

namespace PhpParser;

use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
use PHPUnit\Framework\Attributes\DataProvider;

class CodeParsingTest extends CodeTestAbstract {
    #[DataProvider('provideTestParse')]
    public function testParse($name, $code, $expected, $modeLine): void {
        $modes = $this->parseModeLine($modeLine);
        $parser = $this->createParser($modes['version'] ?? null);
        list($stmts, $output) = $this->getParseOutput($parser, $code, $modes);

        $this->assertSame($expected, $output, $name);
        $this->checkAttributes($stmts);
    }

    public function createParser(?string $version): Parser {
        $factory = new ParserFactory();
        $version = $version === null
            ? PhpVersion::getNewestSupported() : PhpVersion::fromString($version);
        return $factory->createForVersion($version);
    }

    // Must be public for updateTests.php
    public function getParseOutput(Parser $parser, $code, array $modes) {
        $dumpPositions = isset($modes['positions']);
        $dumpOtherAttributes = isset($modes['attributes']);

        $errors = new ErrorHandler\Collecting();
        $stmts = $parser->parse($code, $errors);

        $output = '';
        foreach ($errors->getErrors() as $error) {
            $output .= $this->formatErrorMessage($error, $code) . "\n";
        }

        if (null !== $stmts) {
            $dumper = new NodeDumper([
                'dumpComments' => true,
                'dumpPositions' => $dumpPositions,
                'dumpOtherAttributes' => $dumpOtherAttributes,
            ]);
            $output .= $dumper->dump($stmts, $code);
        }

        return [$stmts, canonicalize($output)];
    }

    public static function provideTestParse() {
        return self::getTests(__DIR__ . '/../code/parser', 'test');
    }

    private function formatErrorMessage(Error $e, $code) {
        if ($e->hasColumnInfo()) {
            return $e->getMessageWithColumnInfo($code);
        }

        return $e->getMessage();
    }

    private function checkAttributes($stmts): void {
        if ($stmts === null) {
            return;
        }

        $traverser = new NodeTraverser(new class () extends NodeVisitorAbstract {
            public function enterNode(Node $node): void {
                $startLine = $node->getStartLine();
                $endLine = $node->getEndLine();
                $startFilePos = $node->getStartFilePos();
                $endFilePos = $node->getEndFilePos();
                $startTokenPos = $node->getStartTokenPos();
                $endTokenPos = $node->getEndTokenPos();
                if ($startLine < 0 || $endLine < 0 ||
                    $startFilePos < 0 || $endFilePos < 0 ||
                    $startTokenPos < 0 || $endTokenPos < 0
                ) {
                    throw new \Exception('Missing location information on ' . $node->getType());
                }

                if ($endLine < $startLine ||
                    $endFilePos < $startFilePos ||
                    $endTokenPos < $startTokenPos
                ) {
                    // Nop and Error can have inverted order, if they are empty.
                    // This can also happen for a Param containing an Error.
                    if (!$node instanceof Stmt\Nop && !$node instanceof Expr\Error &&
                        !$node instanceof Node\Param
                    ) {
                        throw new \Exception('End < start on ' . $node->getType());
                    }
                }
            }
        });
        $traverser->traverse($stmts);
    }
}