Skip to content

Commit c6a1334

Browse files
committed
Update php parser.
1 parent e9607d6 commit c6a1334

File tree

3 files changed

+6
-26
lines changed

3 files changed

+6
-26
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
]
3535
},
3636
"require": {
37-
"php": "^7.3 || ^8.0",
38-
"nikic/php-parser": "^4.4"
37+
"php": "^8.0",
38+
"nikic/php-parser": "^5.3"
3939
},
4040
"require-dev": {
4141
"phpstan/phpstan": "^1.12",

src/CodeConverter.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PhpParser\NodeVisitor\NameResolver;
2121
use PhpParser\Parser;
2222
use PhpParser\Parser\Php7;
23+
use PhpParser\ParserFactory;
2324
use PhpParser\PrettyPrinter\Standard;
2425
use RuntimeException;
2526

@@ -41,11 +42,6 @@ class CodeConverter
4142
*/
4243
protected $parser;
4344

44-
/**
45-
* @var Lexer The PHP Lexer.
46-
*/
47-
protected $lexer;
48-
4945
/**
5046
* @var NodeTraverser The PHP Node Traverser.
5147
*/
@@ -62,23 +58,19 @@ class CodeConverter
6258
protected $nodeFinder;
6359

6460
/**
65-
* @param Lexer|null $lexer The PHP Lexer.
6661
* @param Parser|null $parser The PHP Parser.
6762
* @param NodeTraverser|null $traverser The PHP Node Traverser - make sure that the traverser has a CloningVisitor
6863
* and a NameResolver visitor.
6964
* @param Standard|null $printer The PHP Printer.
7065
* @param NodeFinder|null $nodeFinder The PHP Node Finder.
7166
*/
7267
public function __construct(
73-
?Lexer $lexer = null,
7468
?Parser $parser = null,
7569
?NodeTraverser $traverser = null,
7670
?Standard $printer = null,
7771
?NodeFinder $nodeFinder = null
7872
) {
79-
$this->lexer = $lexer ?? $this->defaultLexer();
80-
81-
$this->parser = $parser ?? new Php7($this->lexer);
73+
$this->parser = $parser ?? (new ParserFactory())->createForNewestSupportedVersion();
8274

8375
if ($traverser === null) {
8476
$traverser = new NodeTraverser();
@@ -92,18 +84,6 @@ public function __construct(
9284
$this->nodeFinder = $nodeFinder ?? new NodeFinder();
9385
}
9486

95-
/**
96-
* @return Lexer
97-
*/
98-
private function defaultLexer(): Lexer
99-
{
100-
return new Emulative(
101-
[
102-
'usedAttributes' => ['comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos'],
103-
]
104-
);
105-
}
106-
10787
/**
10888
* Convert the given source code.
10989
*
@@ -119,7 +99,7 @@ public function convert(string $code, array $functionCallMap): string
11999
throw new RuntimeException('Code could not be parsed.');
120100
}
121101

122-
$oldTokens = $this->lexer->getTokens();
102+
$oldTokens = $this->parser->getTokens();
123103

124104
$newStmts = $this->traverser->traverse($oldStmts);
125105

tests/CodeConverterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testExceptionIfParserReturnsNull(): void
4444
/** @var Parser $parser */
4545
$parser = $parserProphecy->reveal();
4646

47-
$converter = new CodeConverter(null, $parser);
47+
$converter = new CodeConverter($parser);
4848
$converter->convert('<?php echo "1";', []);
4949
}
5050
}

0 commit comments

Comments
 (0)