TypeParser: add recovery from failure to parse callable type

This commit is contained in:
Jan Tvrdik 2018-04-25 09:09:25 +02:00
parent 5251b5b4d4
commit 044411a92d
2 changed files with 32 additions and 1 deletions

View File

@ -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 {

View File

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