Skip to content

Commit

Permalink
Fix parsing phpdoc with no trailing whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanvelzen authored and ondrejmirtes committed Jun 8, 2022
1 parent 9d45205 commit 7c621a2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private function parseText(TokenIterator $tokens): Ast\PhpDoc\PhpDocTextNode
$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)) {

This comment has been minimized.

Copy link
@TomasVotruba

TomasVotruba Jun 9, 2022

Contributor

So when I rollback this line, it works. Interesting 😄

This comment has been minimized.

Copy link
@TomasVotruba

TomasVotruba Jun 9, 2022

Contributor

It possibly because PHPStan is using scoped version, that does not support multiple args. And during outloading it wins over vendor/autoload.php

Just theory :)

$tokens->rollback();
break;
}
Expand Down Expand Up @@ -491,7 +491,10 @@ private function parseOptionalDescription(TokenIterator $tokens, bool $limitStar
$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
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Parser/TokenIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function isCurrentTokenValue(string $tokenValue): bool
}


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


Expand Down
30 changes: 30 additions & 0 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,36 @@ public function provideVarTagsData(): Iterator
]),
];

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 */',
Expand Down

0 comments on commit 7c621a2

Please sign in to comment.