From 7c621a221778202175b5c5df3ff2bcbaea5efd59 Mon Sep 17 00:00:00 2001 From: Richard van Velzen Date: Wed, 8 Jun 2022 18:29:54 +0200 Subject: [PATCH] Fix parsing phpdoc with no trailing whitespace --- src/Parser/PhpDocParser.php | 7 ++++-- src/Parser/TokenIterator.php | 4 +-- tests/PHPStan/Parser/PhpDocParserTest.php | 30 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/Parser/PhpDocParser.php b/src/Parser/PhpDocParser.php index 1e9abc0..e817652 100644 --- a/src/Parser/PhpDocParser.php +++ b/src/Parser/PhpDocParser.php @@ -90,7 +90,7 @@ class PhpDocParser $tokens->pushSavePoint(); $tokens->next(); - if ($tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_TAG) || $tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL) || $tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC) || $tokens->isCurrentTokenType(Lexer::TOKEN_END)) { + if ($tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_TAG, Lexer::TOKEN_PHPDOC_EOL, Lexer::TOKEN_CLOSE_PHPDOC, Lexer::TOKEN_END)) { $tokens->rollback(); break; } @@ -491,7 +491,10 @@ class PhpDocParser $tokens->consumeTokenType(Lexer::TOKEN_OTHER); // will throw exception } - if (!$tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL) && !$tokens->isPrecededByHorizontalWhitespace()) { + if ( + !$tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL, Lexer::TOKEN_CLOSE_PHPDOC, Lexer::TOKEN_END) + && !$tokens->isPrecededByHorizontalWhitespace() + ) { $tokens->consumeTokenType(Lexer::TOKEN_HORIZONTAL_WS); // will throw exception } } diff --git a/src/Parser/TokenIterator.php b/src/Parser/TokenIterator.php index 855ff5c..569a932 100644 --- a/src/Parser/TokenIterator.php +++ b/src/Parser/TokenIterator.php @@ -63,9 +63,9 @@ class TokenIterator } - public function isCurrentTokenType(int $tokenType): bool + public function isCurrentTokenType(int ...$tokenType): bool { - return $this->tokens[$this->index][Lexer::TYPE_OFFSET] === $tokenType; + return in_array($this->tokens[$this->index][Lexer::TYPE_OFFSET], $tokenType, true); } diff --git a/tests/PHPStan/Parser/PhpDocParserTest.php b/tests/PHPStan/Parser/PhpDocParserTest.php index e91a404..aaa992c 100644 --- a/tests/PHPStan/Parser/PhpDocParserTest.php +++ b/tests/PHPStan/Parser/PhpDocParserTest.php @@ -692,6 +692,36 @@ class PhpDocParserTest extends TestCase ]), ]; + yield [ + 'OK with no description and no trailing whitespace', + '/** @var Foo $var*/', + new PhpDocNode([ + new PhpDocTagNode( + '@var', + new VarTagValueNode( + new IdentifierTypeNode('Foo'), + '$var', + '' + ) + ), + ]), + ]; + + yield [ + 'OK with no variable name and description and no trailing whitespace', + '/** @var Foo*/', + new PhpDocNode([ + new PhpDocTagNode( + '@var', + new VarTagValueNode( + new IdentifierTypeNode('Foo'), + '', + '' + ) + ), + ]), + ]; + yield [ 'invalid without type, variable name and description', '/** @var */',