Skip to content

Commit 2269e47

Browse files
committed
Catch parse exception when consuming TOKEN_CLOSE_PHPDOC
1 parent 86b0a00 commit 2269e47

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/Parser/PhpDocParser.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,21 @@ public function parse(TokenIterator $tokens): Ast\PhpDoc\PhpDocNode
4141
}
4242
}
4343

44-
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_PHPDOC);
44+
try {
45+
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_PHPDOC);
46+
} catch (\PHPStan\PhpDocParser\Parser\ParserException $e) {
47+
$name = '';
48+
if (count($children) > 0) {
49+
$lastChild = $children[count($children) - 1];
50+
if ($lastChild instanceof Ast\PhpDoc\PhpDocTagNode) {
51+
$name = $lastChild->name;
52+
}
53+
}
54+
$tokens->forwardToTheEnd();
55+
return new Ast\PhpDoc\PhpDocNode([
56+
new Ast\PhpDoc\PhpDocTagNode($name, new Ast\PhpDoc\InvalidTagValueNode($e->getMessage(), $e)),
57+
]);
58+
}
4559

4660
return new Ast\PhpDoc\PhpDocNode(array_values($children));
4761
}

src/Parser/TokenIterator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ public function next(): void
153153
$this->index++;
154154
}
155155

156+
public function forwardToTheEnd(): void
157+
{
158+
$lastToken = count($this->tokens) - 1;
159+
$this->index = $lastToken;
160+
}
161+
156162

157163
public function pushSavePoint(): void
158164
{

tests/PHPStan/Parser/PhpDocParserTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,25 @@ public function provideVarTagsData(): \Iterator
620620
),
621621
]),
622622
];
623+
624+
yield [
625+
'invalid object shape',
626+
'/** @psalm-type PARTSTRUCTURE_PARAM = object{attribute:string, value?:string} */',
627+
new PhpDocNode([
628+
new PhpDocTagNode(
629+
'@psalm-type',
630+
new InvalidTagValueNode(
631+
'Unexpected token "{", expected \'*/\' at offset 44',
632+
new \PHPStan\PhpDocParser\Parser\ParserException(
633+
'{',
634+
Lexer::TOKEN_OPEN_CURLY_BRACKET,
635+
44,
636+
Lexer::TOKEN_CLOSE_PHPDOC
637+
)
638+
)
639+
),
640+
]),
641+
];
623642
}
624643

625644

0 commit comments

Comments
 (0)