diff --git a/src/Parser/TokenIterator.php b/src/Parser/TokenIterator.php index 9be7593d..fd1e9fe5 100644 --- a/src/Parser/TokenIterator.php +++ b/src/Parser/TokenIterator.php @@ -9,6 +9,7 @@ use function count; use function in_array; use function strlen; +use function strpos; use function substr; class TokenIterator @@ -380,4 +381,37 @@ public function hasParentheses(int $startPos, int $endPos): bool && $this->hasTokenImmediatelyAfter($endPos, Lexer::TOKEN_CLOSE_PARENTHESES); } + /** + * Strip PHP style "one-line" comments (text starting with //) + */ + public function stripComments(): void + { + $line = 1; + $cleanTokens = []; + for ($i = 0; $i < count($this->tokens); $i++) { + $token = $this->tokens[$i]; + if ( + $token[Lexer::TYPE_OFFSET] === Lexer::TOKEN_OTHER + && strpos($token[Lexer::VALUE_OFFSET], '//') === 0 + ) { + while ( + strpos($this->tokens[$i][Lexer::VALUE_OFFSET], "\n") === false + && $i < count($this->tokens) + ) { + $i++; + } + if ($this->tokens[$i][Lexer::LINE_OFFSET] !== $line) { + for ($j = $i + 1; $j < count($this->tokens); $j++) { + $this->tokens[$j][Lexer::LINE_OFFSET]--; + } + } + } else { + $cleanTokens[] = $token; + $line = $token[Lexer::LINE_OFFSET]; + } + } + + $this->tokens = $cleanTokens; + } + } diff --git a/src/Parser/TypeParser.php b/src/Parser/TypeParser.php index 645f544b..a360d113 100644 --- a/src/Parser/TypeParser.php +++ b/src/Parser/TypeParser.php @@ -43,6 +43,7 @@ public function __construct( /** @phpstan-impure */ public function parse(TokenIterator $tokens): Ast\Type\TypeNode { + $tokens->stripComments(); $startLine = $tokens->currentTokenLine(); $startIndex = $tokens->currentTokenIndex(); if ($tokens->isCurrentTokenType(Lexer::TOKEN_NULLABLE)) { diff --git a/tests/PHPStan/Parser/TypeParserTest.php b/tests/PHPStan/Parser/TypeParserTest.php index 85ae0db8..29b09eed 100644 --- a/tests/PHPStan/Parser/TypeParserTest.php +++ b/tests/PHPStan/Parser/TypeParserTest.php @@ -1289,6 +1289,122 @@ public function provideParseData(): array 'int|array{}', new UnionTypeNode([new IdentifierTypeNode('int'), new ArrayShapeNode([])]), ], + [ + <<