mirror of
https://github.com/danog/phpdoc-parser.git
synced 2024-11-30 04:29:20 +01:00
TypeParser: add recovery from failure to parse callable type
This commit is contained in:
parent
5251b5b4d4
commit
044411a92d
@ -49,7 +49,7 @@ class TypeParser
|
||||
$type = $this->parseGeneric($tokens, $type);
|
||||
|
||||
} elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_PARENTHESES)) {
|
||||
$type = $this->parseCallable($tokens, $type);
|
||||
$type = $this->tryParseCallable($tokens, $type);
|
||||
|
||||
} elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
|
||||
$type = $this->tryParseArray($tokens, $type);
|
||||
@ -174,6 +174,22 @@ class TypeParser
|
||||
}
|
||||
|
||||
|
||||
private function tryParseCallable(TokenIterator $tokens, Ast\Type\IdentifierTypeNode $identifier): Ast\Type\TypeNode
|
||||
{
|
||||
try {
|
||||
$tokens->pushSavePoint();
|
||||
$type = $this->parseCallable($tokens, $identifier);
|
||||
$tokens->dropSavePoint();
|
||||
|
||||
} catch (\PHPStan\PhpDocParser\Parser\ParserException $e) {
|
||||
$tokens->rollback();
|
||||
$type = $identifier;
|
||||
}
|
||||
|
||||
return $type;
|
||||
}
|
||||
|
||||
|
||||
private function tryParseArray(TokenIterator $tokens, Ast\Type\TypeNode $type): Ast\Type\TypeNode
|
||||
{
|
||||
try {
|
||||
|
@ -377,6 +377,21 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase
|
||||
]),
|
||||
];
|
||||
|
||||
yield [
|
||||
'OK without variable name and description in parentheses',
|
||||
'/** @var Foo (Bar) */',
|
||||
new PhpDocNode([
|
||||
new PhpDocTagNode(
|
||||
'@var',
|
||||
new VarTagValueNode(
|
||||
new IdentifierTypeNode('Foo'),
|
||||
'',
|
||||
'(Bar)'
|
||||
)
|
||||
),
|
||||
]),
|
||||
];
|
||||
|
||||
yield [
|
||||
'OK with variable name and description',
|
||||
'/** @var Foo $foo optional description */',
|
||||
|
Loading…
Reference in New Issue
Block a user