Skip to content

Commit ce9442e

Browse files
committed
Compatibility with PHP-Parser v5
1 parent 6c69e03 commit ce9442e

File tree

4 files changed

+21
-26
lines changed

4 files changed

+21
-26
lines changed

conf/config.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,7 +1859,7 @@ services:
18591859
autowired: false
18601860

18611861
currentPhpVersionPhpParser:
1862-
class: PhpParser\Parser\Php7
1862+
class: PhpParser\Parser\Php8 # todo use factory and create Php7/Php8
18631863
arguments:
18641864
lexer: @currentPhpVersionLexer
18651865
autowired: false
@@ -1971,7 +1971,7 @@ services:
19711971
autowired: false
19721972

19731973
php8PhpParser:
1974-
class: PhpParser\Parser\Php7
1974+
class: PhpParser\Parser\Php8
19751975
arguments:
19761976
lexer: @php8Lexer
19771977
autowired: false

src/Parser/LexerFactory.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,22 @@
99
class LexerFactory
1010
{
1111

12-
private const OPTIONS = ['usedAttributes' => ['comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos', 'startFilePos', 'endFilePos']];
13-
1412
public function __construct(private PhpVersion $phpVersion)
1513
{
1614
}
1715

1816
public function create(): Lexer
1917
{
20-
$options = self::OPTIONS;
2118
if ($this->phpVersion->getVersionId() === PHP_VERSION_ID) {
22-
return new Lexer($options);
19+
return new Lexer();
2320
}
2421

25-
$options['phpVersion'] = $this->phpVersion->getVersionString();
26-
27-
return new Lexer\Emulative($options);
22+
return new Lexer\Emulative(\PhpParser\PhpVersion::fromString($this->phpVersion->getVersionString()));
2823
}
2924

3025
public function createEmulative(): Lexer\Emulative
3126
{
32-
return new Lexer\Emulative(self::OPTIONS);
27+
return new Lexer\Emulative();
3328
}
3429

3530
}

src/Parser/PhpParserDecorator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ public function parse(string $code, ?ErrorHandler $errorHandler = null): array
3131
}
3232
}
3333

34+
public function getTokens(): array
35+
{
36+
return $this->wrappedParser->getTokens();
37+
}
38+
3439
}

src/Parser/RichParser.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\NodeTraverser;
99
use PhpParser\NodeVisitor\NameResolver;
10+
use PhpParser\Token;
1011
use PHPStan\Analyser\Ignore\IgnoreLexer;
1112
use PHPStan\Analyser\Ignore\IgnoreParseException;
1213
use PHPStan\DependencyInjection\Container;
@@ -17,7 +18,6 @@
1718
use function array_values;
1819
use function count;
1920
use function in_array;
20-
use function is_string;
2121
use function str_contains;
2222
use function strlen;
2323
use function strpos;
@@ -64,8 +64,7 @@ public function parseString(string $sourceCode): array
6464
$errorHandler = new Collecting();
6565
$nodes = $this->parser->parse($sourceCode, $errorHandler);
6666

67-
/** @var list<string|array{0:int,1:string,2:int}> $tokens */
68-
$tokens = $this->lexer->getTokens();
67+
$tokens = $this->parser->getTokens();
6968
if ($errorHandler->hasErrors()) {
7069
throw new ParserErrorsException($errorHandler->getErrors(), null);
7170
}
@@ -101,7 +100,7 @@ public function parseString(string $sourceCode): array
101100
}
102101

103102
/**
104-
* @param list<string|array{0:int,1:string,2:int}> $tokens
103+
* @param Token[] $tokens
105104
* @return array{lines: array<int, non-empty-list<string>|null>, errors: array<int, non-empty-list<string>>}
106105
*/
107106
private function getLinesToIgnore(array $tokens): array
@@ -111,12 +110,8 @@ private function getLinesToIgnore(array $tokens): array
111110
$pendingToken = null;
112111
$errors = [];
113112
foreach ($tokens as $token) {
114-
if (is_string($token)) {
115-
continue;
116-
}
117-
118-
$type = $token[0];
119-
$line = $token[2];
113+
$type = $token->id;
114+
$line = $token->line;
120115
if ($type !== T_COMMENT && $type !== T_DOC_COMMENT) {
121116
if ($type !== T_WHITESPACE) {
122117
if ($pendingToken !== null) {
@@ -147,14 +142,14 @@ private function getLinesToIgnore(array $tokens): array
147142
continue;
148143
}
149144

150-
$text = $token[1];
145+
$text = $token->text;
151146
$isNextLine = str_contains($text, '@phpstan-ignore-next-line');
152147
$isCurrentLine = str_contains($text, '@phpstan-ignore-line');
153148
if ($isNextLine) {
154149
$line++;
155150
}
156151
if ($isNextLine || $isCurrentLine) {
157-
$line += substr_count($token[1], "\n");
152+
$line += substr_count($token->text, "\n");
158153

159154
$lines[$line] = null;
160155
continue;
@@ -167,20 +162,20 @@ private function getLinesToIgnore(array $tokens): array
167162

168163
$ignoreLine = substr_count(substr($text, 0, $ignorePos), "\n") - 1;
169164

170-
if ($previousToken !== null && $previousToken[2] === $line) {
165+
if ($previousToken !== null && $previousToken->line === $line) {
171166
try {
172167
foreach ($this->parseIdentifiers($text, $ignorePos) as $identifier) {
173168
$lines[$line][] = $identifier;
174169
}
175170
} catch (IgnoreParseException $e) {
176-
$errors[] = [$token[2] + $e->getPhpDocLine() + $ignoreLine, $e->getMessage()];
171+
$errors[] = [$token->line + $e->getPhpDocLine() + $ignoreLine, $e->getMessage()];
177172
}
178173

179174
continue;
180175
}
181176

182-
$line += substr_count($token[1], "\n");
183-
$pendingToken = [$text, $ignorePos, $token[2] + $ignoreLine, $line];
177+
$line += substr_count($token->text, "\n");
178+
$pendingToken = [$text, $ignorePos, $token->line + $ignoreLine, $line];
184179
}
185180

186181
if ($pendingToken !== null) {

0 commit comments

Comments
 (0)