Fix parsing phpdoc with no trailing whitespace

This commit is contained in:
Richard van Velzen 2022-06-08 18:29:54 +02:00 committed by Ondřej Mirtes
parent 9d452051cb
commit 7c621a2217
3 changed files with 37 additions and 4 deletions

View File

@ -90,7 +90,7 @@ class PhpDocParser
$tokens->pushSavePoint(); $tokens->pushSavePoint();
$tokens->next(); $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(); $tokens->rollback();
break; break;
} }
@ -491,7 +491,10 @@ class PhpDocParser
$tokens->consumeTokenType(Lexer::TOKEN_OTHER); // will throw exception $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 $tokens->consumeTokenType(Lexer::TOKEN_HORIZONTAL_WS); // will throw exception
} }
} }

View File

@ -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);
} }

View File

@ -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 [ yield [
'invalid without type, variable name and description', 'invalid without type, variable name and description',
'/** @var */', '/** @var */',