Skip to content

Commit 8dafccb

Browse files
committed
ParserFactory to correctly create Php7/Php8
1 parent 89bc1e6 commit 8dafccb

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

conf/config.neon

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2050,7 +2050,11 @@ services:
20502050
autowired: false
20512051

20522052
currentPhpVersionPhpParser:
2053-
class: PhpParser\Parser\Php8 # todo use factory and create Php7/Php8
2053+
factory: @currentPhpVersionPhpParserFactory::create()
2054+
autowired: false
2055+
2056+
currentPhpVersionPhpParserFactory:
2057+
class: PHPStan\Parser\PhpParserFactory
20542058
arguments:
20552059
lexer: @currentPhpVersionLexer
20562060
autowired: false

src/Parser/PhpParserFactory.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Parser;
4+
5+
use PhpParser\Lexer;
6+
use PhpParser\Parser\Php7;
7+
use PhpParser\Parser\Php8;
8+
use PhpParser\ParserAbstract;
9+
use PHPStan\Php\PhpVersion;
10+
11+
class PhpParserFactory
12+
{
13+
14+
public function __construct(private Lexer $lexer, private PhpVersion $phpVersion)
15+
{
16+
}
17+
18+
public function create(): ParserAbstract
19+
{
20+
$phpVersion = \PhpParser\PhpVersion::fromString($this->phpVersion->getVersionString());
21+
if ($this->phpVersion->getVersionId() >= 80000) {
22+
return new Php8($this->lexer, $phpVersion);
23+
}
24+
25+
return new Php7($this->lexer, $phpVersion);
26+
}
27+
28+
}

0 commit comments

Comments
 (0)