Skip to content

Commit 31742d1

Browse files
committed
Compatibility with PHP-Parser v5
1 parent 5c68a14 commit 31742d1

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

conf/config.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,7 +2045,7 @@ services:
20452045
autowired: false
20462046

20472047
currentPhpVersionPhpParser:
2048-
class: PhpParser\Parser\Php7
2048+
class: PhpParser\Parser\Php8 # todo use factory and create Php7/Php8
20492049
arguments:
20502050
lexer: @currentPhpVersionLexer
20512051
autowired: false
@@ -2161,7 +2161,7 @@ services:
21612161
autowired: false
21622162

21632163
php8PhpParser:
2164-
class: PhpParser\Parser\Php7
2164+
class: PhpParser\Parser\Php8
21652165
arguments:
21662166
lexer: @php8Lexer
21672167
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
final 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: 10 additions & 15 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 count;
1819
use function implode;
1920
use function in_array;
20-
use function is_string;
2121
use function preg_match_all;
2222
use function sprintf;
2323
use function str_contains;
@@ -72,8 +72,7 @@ public function parseString(string $sourceCode): array
7272
$errorHandler = new Collecting();
7373
$nodes = $this->parser->parse($sourceCode, $errorHandler);
7474

75-
/** @var list<string|array{0:int,1:string,2:int}> $tokens */
76-
$tokens = $this->lexer->getTokens();
75+
$tokens = $this->parser->getTokens();
7776
if ($errorHandler->hasErrors()) {
7877
throw new ParserErrorsException($errorHandler->getErrors(), null);
7978
}
@@ -109,7 +108,7 @@ public function parseString(string $sourceCode): array
109108
}
110109

111110
/**
112-
* @param list<string|array{0:int,1:string,2:int}> $tokens
111+
* @param Token[] $tokens
113112
* @return array{lines: array<int, non-empty-list<string>|null>, errors: array<int, non-empty-list<string>>}
114113
*/
115114
private function getLinesToIgnore(array $tokens): array
@@ -119,12 +118,8 @@ private function getLinesToIgnore(array $tokens): array
119118
$pendingToken = null;
120119
$errors = [];
121120
foreach ($tokens as $token) {
122-
if (is_string($token)) {
123-
continue;
124-
}
125-
126-
$type = $token[0];
127-
$line = $token[2];
121+
$type = $token->id;
122+
$line = $token->line;
128123
if ($type !== T_COMMENT && $type !== T_DOC_COMMENT) {
129124
if ($type !== T_WHITESPACE) {
130125
if ($pendingToken !== null) {
@@ -155,7 +150,7 @@ private function getLinesToIgnore(array $tokens): array
155150
continue;
156151
}
157152

158-
$text = $token[1];
153+
$text = $token->text;
159154
$isNextLine = str_contains($text, '@phpstan-ignore-next-line');
160155
$isCurrentLine = str_contains($text, '@phpstan-ignore-line');
161156

@@ -204,20 +199,20 @@ private function getLinesToIgnore(array $tokens): array
204199

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

207-
if ($previousToken !== null && $previousToken[2] === $line) {
202+
if ($previousToken !== null && $previousToken->line === $line) {
208203
try {
209204
foreach ($this->parseIdentifiers($text, $ignorePos) as $identifier) {
210205
$lines[$line][] = $identifier;
211206
}
212207
} catch (IgnoreParseException $e) {
213-
$errors[] = [$token[2] + $e->getPhpDocLine() + $ignoreLine, $e->getMessage()];
208+
$errors[] = [$token->line + $e->getPhpDocLine() + $ignoreLine, $e->getMessage()];
214209
}
215210

216211
continue;
217212
}
218213

219-
$line += substr_count($token[1], "\n");
220-
$pendingToken = [$text, $ignorePos, $token[2] + $ignoreLine, $line];
214+
$line += substr_count($token->text, "\n");
215+
$pendingToken = [$text, $ignorePos, $token->line + $ignoreLine, $line];
221216
}
222217

223218
if ($pendingToken !== null) {

0 commit comments

Comments
 (0)