Skip to content

Commit

Permalink
Catch parse exception when consuming TOKEN_CLOSE_PHPDOC
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 22, 2021
1 parent 86b0a00 commit 2269e47
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,21 @@ public function parse(TokenIterator $tokens): Ast\PhpDoc\PhpDocNode
}
}

$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_PHPDOC);
try {
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_PHPDOC);
} catch (\PHPStan\PhpDocParser\Parser\ParserException $e) {
$name = '';
if (count($children) > 0) {
$lastChild = $children[count($children) - 1];
if ($lastChild instanceof Ast\PhpDoc\PhpDocTagNode) {
$name = $lastChild->name;
}
}
$tokens->forwardToTheEnd();
return new Ast\PhpDoc\PhpDocNode([
new Ast\PhpDoc\PhpDocTagNode($name, new Ast\PhpDoc\InvalidTagValueNode($e->getMessage(), $e)),
]);
}

return new Ast\PhpDoc\PhpDocNode(array_values($children));
}
Expand Down
6 changes: 6 additions & 0 deletions src/Parser/TokenIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ public function next(): void
$this->index++;
}

public function forwardToTheEnd(): void
{
$lastToken = count($this->tokens) - 1;
$this->index = $lastToken;
}


public function pushSavePoint(): void
{
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,25 @@ public function provideVarTagsData(): \Iterator
),
]),
];

yield [
'invalid object shape',
'/** @psalm-type PARTSTRUCTURE_PARAM = object{attribute:string, value?:string} */',
new PhpDocNode([
new PhpDocTagNode(
'@psalm-type',
new InvalidTagValueNode(
'Unexpected token "{", expected \'*/\' at offset 44',
new \PHPStan\PhpDocParser\Parser\ParserException(
'{',
Lexer::TOKEN_OPEN_CURLY_BRACKET,
44,
Lexer::TOKEN_CLOSE_PHPDOC
)
)
),
]),
];
}


Expand Down

0 comments on commit 2269e47

Please sign in to comment.