Skip to content

Commit 7c621a2

Browse files
rvanvelzenondrejmirtes
authored andcommitted
Fix parsing phpdoc with no trailing whitespace
1 parent 9d45205 commit 7c621a2

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

Diff for: src/Parser/PhpDocParser.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private function parseText(TokenIterator $tokens): Ast\PhpDoc\PhpDocTextNode
9090
$tokens->pushSavePoint();
9191
$tokens->next();
9292

93-
if ($tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_TAG) || $tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL) || $tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC) || $tokens->isCurrentTokenType(Lexer::TOKEN_END)) {
93+
if ($tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_TAG, Lexer::TOKEN_PHPDOC_EOL, Lexer::TOKEN_CLOSE_PHPDOC, Lexer::TOKEN_END)) {
9494
$tokens->rollback();
9595
break;
9696
}
@@ -491,7 +491,10 @@ private function parseOptionalDescription(TokenIterator $tokens, bool $limitStar
491491
$tokens->consumeTokenType(Lexer::TOKEN_OTHER); // will throw exception
492492
}
493493

494-
if (!$tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL) && !$tokens->isPrecededByHorizontalWhitespace()) {
494+
if (
495+
!$tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL, Lexer::TOKEN_CLOSE_PHPDOC, Lexer::TOKEN_END)
496+
&& !$tokens->isPrecededByHorizontalWhitespace()
497+
) {
495498
$tokens->consumeTokenType(Lexer::TOKEN_HORIZONTAL_WS); // will throw exception
496499
}
497500
}

Diff for: src/Parser/TokenIterator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public function isCurrentTokenValue(string $tokenValue): bool
6363
}
6464

6565

66-
public function isCurrentTokenType(int $tokenType): bool
66+
public function isCurrentTokenType(int ...$tokenType): bool
6767
{
68-
return $this->tokens[$this->index][Lexer::TYPE_OFFSET] === $tokenType;
68+
return in_array($this->tokens[$this->index][Lexer::TYPE_OFFSET], $tokenType, true);
6969
}
7070

7171

Diff for: tests/PHPStan/Parser/PhpDocParserTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,36 @@ public function provideVarTagsData(): Iterator
692692
]),
693693
];
694694

695+
yield [
696+
'OK with no description and no trailing whitespace',
697+
'/** @var Foo $var*/',
698+
new PhpDocNode([
699+
new PhpDocTagNode(
700+
'@var',
701+
new VarTagValueNode(
702+
new IdentifierTypeNode('Foo'),
703+
'$var',
704+
''
705+
)
706+
),
707+
]),
708+
];
709+
710+
yield [
711+
'OK with no variable name and description and no trailing whitespace',
712+
'/** @var Foo*/',
713+
new PhpDocNode([
714+
new PhpDocTagNode(
715+
'@var',
716+
new VarTagValueNode(
717+
new IdentifierTypeNode('Foo'),
718+
'',
719+
''
720+
)
721+
),
722+
]),
723+
];
724+
695725
yield [
696726
'invalid without type, variable name and description',
697727
'/** @var */',

0 commit comments

Comments
 (0)