From 76db1bfeb1e8162a8ca12ca9714df042e1964d80 Mon Sep 17 00:00:00 2001 From: Arnaud Le Blanc Date: Wed, 22 May 2019 09:28:27 +0200 Subject: [PATCH] Allow only atomic types as template bound --- src/Parser/PhpDocParser.php | 4 ++-- src/Parser/TypeParser.php | 2 +- tests/PHPStan/Parser/PhpDocParserTest.php | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Parser/PhpDocParser.php b/src/Parser/PhpDocParser.php index a0efa34f..44f8e3ce 100644 --- a/src/Parser/PhpDocParser.php +++ b/src/Parser/PhpDocParser.php @@ -254,13 +254,13 @@ private function parseTemplateTagValue(TokenIterator $tokens): Ast\PhpDoc\Templa $tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER); if ($tokens->tryConsumeTokenValue('of')) { - $bound = $this->typeParser->parse($tokens); + $bound = $this->typeParser->parseAtomic($tokens); } else { $bound = new IdentifierTypeNode('mixed'); } - $description = $this->parseOptionalDescription($tokens); + $description = $this->parseOptionalDescription($tokens, true); return new Ast\PhpDoc\TemplateTagValueNode($name, $bound, $description); } diff --git a/src/Parser/TypeParser.php b/src/Parser/TypeParser.php index 596a0d52..d6922ec2 100644 --- a/src/Parser/TypeParser.php +++ b/src/Parser/TypeParser.php @@ -28,7 +28,7 @@ public function parse(TokenIterator $tokens): Ast\Type\TypeNode } - private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode + public function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode { if ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_PARENTHESES)) { $type = $this->parse($tokens); diff --git a/tests/PHPStan/Parser/PhpDocParserTest.php b/tests/PHPStan/Parser/PhpDocParserTest.php index dd9f3aff..c13576eb 100644 --- a/tests/PHPStan/Parser/PhpDocParserTest.php +++ b/tests/PHPStan/Parser/PhpDocParserTest.php @@ -2300,6 +2300,25 @@ public function provideTemplateTagsData(): \Iterator ), ]), ]; + + yield [ + 'invalid union bound', + '/** @template T of int|float */', + new PhpDocNode([ + new PhpDocTagNode( + '@template', + new InvalidTagValueNode( + 'T of int|float', + new \PHPStan\PhpDocParser\Parser\ParserException( + '|', + Lexer::TOKEN_UNION, + 22, + Lexer::TOKEN_OTHER + ) + ) + ), + ]), + ]; } }