From 821a89cd915be2665d24137558b1a9334d507513 Mon Sep 17 00:00:00 2001 From: yoosef alipour Date: Wed, 17 Jan 2024 21:21:03 +0330 Subject: [PATCH] Extractor: supports PHP-Parser 5.0 (#150) --- composer.json | 2 +- src/PhpGenerator/Extractor.php | 5 ++--- tests/PhpGenerator/fixtures/extractor.php | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 78827f66..fe9fc3e8 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ }, "require-dev": { "nette/tester": "^2.4", - "nikic/php-parser": "^4.15", + "nikic/php-parser": "^5.0", "tracy/tracy": "^2.8", "phpstan/phpstan": "^1.0", "jetbrains/phpstorm-attributes": "dev-master" diff --git a/src/PhpGenerator/Extractor.php b/src/PhpGenerator/Extractor.php index f12e1435..e44091fa 100644 --- a/src/PhpGenerator/Extractor.php +++ b/src/PhpGenerator/Extractor.php @@ -46,9 +46,8 @@ private function parseCode(string $code): void throw new Nette\InvalidStateException('The input string is not a PHP code.'); } - $this->code = Nette\Utils\Strings::normalizeNewlines($code); - $lexer = new PhpParser\Lexer\Emulative(['usedAttributes' => ['startFilePos', 'endFilePos', 'comments']]); - $parser = (new ParserFactory)->create(ParserFactory::ONLY_PHP7, $lexer); + $this->code = Nette\Utils\Strings::unixNewLines($code); + $parser = (new ParserFactory)->createForNewestSupportedVersion(); $stmts = $parser->parse($this->code); $traverser = new PhpParser\NodeTraverser; diff --git a/tests/PhpGenerator/fixtures/extractor.php b/tests/PhpGenerator/fixtures/extractor.php index 936b5c0a..d8d80890 100644 --- a/tests/PhpGenerator/fixtures/extractor.php +++ b/tests/PhpGenerator/fixtures/extractor.php @@ -18,7 +18,7 @@ function comment1() function comment2() { // comment - 'bar'; + "bar"; } function comment3()