From d579798d2fefdc27de8227ab4a5b4eabe9532be7 Mon Sep 17 00:00:00 2001 From: Richard van Velzen Date: Wed, 8 Jun 2022 14:22:21 +0200 Subject: [PATCH] Require whitespace before description with limited start tokens --- src/Parser/PhpDocParser.php | 4 ++ tests/PHPStan/Parser/PhpDocParserTest.php | 80 +++++++++++++++++++---- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/src/Parser/PhpDocParser.php b/src/Parser/PhpDocParser.php index fd034d7..d0286d5 100644 --- a/src/Parser/PhpDocParser.php +++ b/src/Parser/PhpDocParser.php @@ -474,6 +474,10 @@ class PhpDocParser $tokens->consumeTokenType(Lexer::TOKEN_OTHER); // will throw exception } + + if (!$tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL) && !$tokens->isPrecededByHorizontalWhitespace()) { + $tokens->consumeTokenType(Lexer::TOKEN_HORIZONTAL_WS); // will throw exception + } } return $this->parseText($tokens)->text; diff --git a/tests/PHPStan/Parser/PhpDocParserTest.php b/tests/PHPStan/Parser/PhpDocParserTest.php index 20451b7..31bb024 100644 --- a/tests/PHPStan/Parser/PhpDocParserTest.php +++ b/tests/PHPStan/Parser/PhpDocParserTest.php @@ -1310,6 +1310,58 @@ class PhpDocParserTest extends TestCase ), ]), ]; + + yield [ + 'OK variadic callable', + '/** @return \Closure(int ...$u, string): string */', + new PhpDocNode([ + new PhpDocTagNode( + '@return', + new ReturnTagValueNode( + new CallableTypeNode( + new IdentifierTypeNode('\Closure'), + [ + new CallableTypeParameterNode( + new IdentifierTypeNode('int'), + false, + true, + '$u', + false + ), + new CallableTypeParameterNode( + new IdentifierTypeNode('string'), + false, + false, + '', + false + ), + ], + new IdentifierTypeNode('string') + ), + '' + ) + ), + ]), + ]; + + yield [ + 'invalid variadic callable', + '/** @return \Closure(...int, string): string */', + new PhpDocNode([ + new PhpDocTagNode( + '@return', + new InvalidTagValueNode( + '\Closure(...int, string): string', + new ParserException( + '(', + Lexer::TOKEN_OPEN_PARENTHESES, + 20, + Lexer::TOKEN_HORIZONTAL_WS + ) + ) + ), + ]), + ]; } @@ -2104,10 +2156,14 @@ class PhpDocParserTest extends TestCase new PhpDocNode([ new PhpDocTagNode( '@var', - new VarTagValueNode( - new IdentifierTypeNode('callable'), - '', - '(int)' + new InvalidTagValueNode( + 'callable(int)', + new ParserException( + '(', + Lexer::TOKEN_OPEN_PARENTHESES, + 17, + Lexer::TOKEN_HORIZONTAL_WS + ) ) ), ]), @@ -4076,14 +4132,14 @@ Finder::findFiles('*.php') new PhpDocNode([ new PhpDocTagNode( '@return', - new ReturnTagValueNode( - new GenericTypeNode( - new IdentifierTypeNode('Foo'), - [ - new IdentifierTypeNode('strong'), - ] - ), - 'Important description' + new InvalidTagValueNode( + 'Foo Important description', + new ParserException( + 'Important', + Lexer::TOKEN_IDENTIFIER, + 27, + Lexer::TOKEN_HORIZONTAL_WS + ) ) ), ]),