Skip to content

Commit abe060a

Browse files
committed
Make parser work with v2
1 parent 77ceeef commit abe060a

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/parser/FileParser.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
use phootwork\collection\Set;
77
use phootwork\file\exception\FileNotFoundException;
88
use phootwork\file\File;
9-
use PhpParser\Lexer\Emulative;
109
use PhpParser\Node;
1110
use PhpParser\NodeTraverser;
1211
use PhpParser\NodeVisitorAbstract;
13-
use PhpParser\Parser;
1412

1513
class FileParser extends NodeVisitorAbstract {
1614

@@ -47,12 +45,21 @@ public function parse() {
4745
throw new FileNotFoundException(sprintf('File (%s) does not exist.', $this->filename));
4846
}
4947

50-
$parser = new Parser(new Emulative());
48+
$parser = $this->getParser();
5149
$traverser = new NodeTraverser();
5250
$traverser->addVisitor($this);
5351
$traverser->traverse($parser->parse($file->read()));
5452
}
5553

54+
private function getParser() {
55+
if (class_exists('\\PhpParser\\ParserFactory')) {
56+
$factory = new \PhpParser\ParserFactory();
57+
return $factory->create(\PhpParser\ParserFactory::PREFER_PHP7);
58+
} else {
59+
return new \PhpParser\Parser(new \PhpParser\Lexer\Emulative());
60+
}
61+
}
62+
5663
public function enterNode(Node $node) {
5764
foreach ($this->visitors as $visitor) {
5865
switch ($node->getType()) {

0 commit comments

Comments
 (0)