Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require whitespace before description with limited start tokens #128

Merged
merged 1 commit into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,10 @@ private function parseOptionalDescription(TokenIterator $tokens, bool $limitStar

$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;
Expand Down
80 changes: 68 additions & 12 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,58 @@ public function provideReturnTagsData(): Iterator
),
]),
];

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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for throwing at least

as all other types in \Closure phpdoc does not require a variable name, can support for unnamed variadic phpdoc be added?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is already supported, but the type always has to come first.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know, thank you ❤️

new ParserException(
'(',
Lexer::TOKEN_OPEN_PARENTHESES,
20,
Lexer::TOKEN_HORIZONTAL_WS
)
)
),
]),
];
}


Expand Down Expand Up @@ -2104,10 +2156,14 @@ public function provideSingleLinePhpDocData(): Iterator
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
)
)
),
]),
Expand Down Expand Up @@ -4076,14 +4132,14 @@ public function provideDescriptionWithOrWithoutHtml(): Iterator
new PhpDocNode([
new PhpDocTagNode(
'@return',
new ReturnTagValueNode(
new GenericTypeNode(
new IdentifierTypeNode('Foo'),
[
new IdentifierTypeNode('strong'),
]
),
'Important description'
new InvalidTagValueNode(
'Foo <strong>Important description',
new ParserException(
'Important',
Lexer::TOKEN_IDENTIFIER,
27,
Lexer::TOKEN_HORIZONTAL_WS
)
)
),
]),
Expand Down