From c6a133414b4cf9ac767105be1717c2a20d601c96 Mon Sep 17 00:00:00 2001 From: Adrian Suter Date: Mon, 2 Dec 2024 13:16:19 +0100 Subject: [PATCH 1/2] Update php parser. --- composer.json | 4 ++-- src/CodeConverter.php | 26 +++----------------------- tests/CodeConverterTest.php | 2 +- 3 files changed, 6 insertions(+), 26 deletions(-) diff --git a/composer.json b/composer.json index b9b2b5a..276f3a2 100644 --- a/composer.json +++ b/composer.json @@ -34,8 +34,8 @@ ] }, "require": { - "php": "^7.3 || ^8.0", - "nikic/php-parser": "^4.4" + "php": "^8.0", + "nikic/php-parser": "^5.3" }, "require-dev": { "phpstan/phpstan": "^1.12", diff --git a/src/CodeConverter.php b/src/CodeConverter.php index c2bba47..3a0db11 100644 --- a/src/CodeConverter.php +++ b/src/CodeConverter.php @@ -20,6 +20,7 @@ use PhpParser\NodeVisitor\NameResolver; use PhpParser\Parser; use PhpParser\Parser\Php7; +use PhpParser\ParserFactory; use PhpParser\PrettyPrinter\Standard; use RuntimeException; @@ -41,11 +42,6 @@ class CodeConverter */ protected $parser; - /** - * @var Lexer The PHP Lexer. - */ - protected $lexer; - /** * @var NodeTraverser The PHP Node Traverser. */ @@ -62,7 +58,6 @@ class CodeConverter protected $nodeFinder; /** - * @param Lexer|null $lexer The PHP Lexer. * @param Parser|null $parser The PHP Parser. * @param NodeTraverser|null $traverser The PHP Node Traverser - make sure that the traverser has a CloningVisitor * and a NameResolver visitor. @@ -70,15 +65,12 @@ class CodeConverter * @param NodeFinder|null $nodeFinder The PHP Node Finder. */ public function __construct( - ?Lexer $lexer = null, ?Parser $parser = null, ?NodeTraverser $traverser = null, ?Standard $printer = null, ?NodeFinder $nodeFinder = null ) { - $this->lexer = $lexer ?? $this->defaultLexer(); - - $this->parser = $parser ?? new Php7($this->lexer); + $this->parser = $parser ?? (new ParserFactory())->createForNewestSupportedVersion(); if ($traverser === null) { $traverser = new NodeTraverser(); @@ -92,18 +84,6 @@ public function __construct( $this->nodeFinder = $nodeFinder ?? new NodeFinder(); } - /** - * @return Lexer - */ - private function defaultLexer(): Lexer - { - return new Emulative( - [ - 'usedAttributes' => ['comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos'], - ] - ); - } - /** * Convert the given source code. * @@ -119,7 +99,7 @@ public function convert(string $code, array $functionCallMap): string throw new RuntimeException('Code could not be parsed.'); } - $oldTokens = $this->lexer->getTokens(); + $oldTokens = $this->parser->getTokens(); $newStmts = $this->traverser->traverse($oldStmts); diff --git a/tests/CodeConverterTest.php b/tests/CodeConverterTest.php index e878f77..24a504d 100644 --- a/tests/CodeConverterTest.php +++ b/tests/CodeConverterTest.php @@ -44,7 +44,7 @@ public function testExceptionIfParserReturnsNull(): void /** @var Parser $parser */ $parser = $parserProphecy->reveal(); - $converter = new CodeConverter(null, $parser); + $converter = new CodeConverter($parser); $converter->convert(' Date: Mon, 2 Dec 2024 13:29:47 +0100 Subject: [PATCH 2/2] Drop php 7. --- .github/workflows/tests.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7cb944b..1ee4706 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -10,11 +10,15 @@ jobs: strategy: fail-fast: false matrix: - php: [ 7.3, 7.4, 8.0, 8.1, 8.2 ] + php: [ 8.0, 8.1, 8.2, 8.3, 8.4 ] experimental: [ false ] include: - - php: 8.1 + - php: 8.2 analysis: true + - php: 8.3 + experimental: true + - php: 8.4 + experimental: true steps: - name: Checkout