Skip to content

Commit 53b8c8d

Browse files
committed
ParserFactory to correctly create Php7/Php8
1 parent e33f4cb commit 53b8c8d

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
@@ -1864,7 +1864,11 @@ services:
18641864
autowired: false
18651865

18661866
currentPhpVersionPhpParser:
1867-
class: PhpParser\Parser\Php8 # todo use factory and create Php7/Php8
1867+
factory: @currentPhpVersionPhpParserFactory::create()
1868+
autowired: false
1869+
1870+
currentPhpVersionPhpParserFactory:
1871+
class: PHPStan\Parser\PhpParserFactory
18681872
arguments:
18691873
lexer: @currentPhpVersionLexer
18701874
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)