Skip to content

Fixed parsing description started with HTML tag #47

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

Merged
merged 1 commit into from
Jun 10, 2020
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
1 change: 0 additions & 1 deletion src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class PhpDocParser
private const DISALLOWED_DESCRIPTION_START_TOKENS = [
Lexer::TOKEN_UNION,
Lexer::TOKEN_INTERSECTION,
Lexer::TOKEN_OPEN_ANGLE_BRACKET,
];

/** @var TypeParser */
Expand Down
37 changes: 37 additions & 0 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode
if (!$tokens->isCurrentTokenType(Lexer::TOKEN_DOUBLE_COLON)) {
$tokens->dropSavePoint(); // because of ConstFetchNode
if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) {
$tokens->pushSavePoint();

$isHtml = $this->isHtml($tokens);
$tokens->rollback();
if ($isHtml) {
return $type;
}

$type = $this->parseGeneric($tokens, $type);

if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
Expand Down Expand Up @@ -161,6 +169,35 @@ private function parseNullable(TokenIterator $tokens): Ast\Type\TypeNode
return new Ast\Type\NullableTypeNode($type);
}

public function isHtml(TokenIterator $tokens): bool
{
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET);

if (!$tokens->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER)) {
return false;
}

$htmlTagName = $tokens->currentTokenValue();

$tokens->next();

if (!$tokens->tryConsumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET)) {
return false;
}

while (!$tokens->isCurrentTokenType(Lexer::TOKEN_END)) {
if (
$tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)
&& strpos($tokens->currentTokenValue(), '/' . $htmlTagName . '>') !== false
) {
return true;
}

$tokens->next();
}

return false;
}

public function parseGeneric(TokenIterator $tokens, Ast\Type\IdentifierTypeNode $baseType): Ast\Type\GenericTypeNode
{
Expand Down
73 changes: 73 additions & 0 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ protected function setUp(): void
* @dataProvider provideTemplateTagsData
* @dataProvider provideExtendsTagsData
* @dataProvider provideRealWorldExampleData
* @dataProvider provideDescriptionWithOrWithoutHtml
* @param string $label
* @param string $input
* @param PhpDocNode $expectedPhpDocNode
Expand Down Expand Up @@ -3130,6 +3131,78 @@ public function provideRealWorldExampleData(): \Iterator
];
}

public function provideDescriptionWithOrWithoutHtml(): \Iterator
{
yield [
'Description with HTML tags in @return tag (close tags together)',
'/**' . PHP_EOL .
' * @return Foo <strong>Important <i>description</i></strong>' . PHP_EOL .
' */',
new PhpDocNode([
new PhpDocTagNode(
'@return',
new ReturnTagValueNode(
new IdentifierTypeNode('Foo'),
'<strong>Important <i>description</i></strong>'
)
),
]),
];

yield [
'Description with HTML tags in @throws tag (closed tags with text between)',
'/**' . PHP_EOL .
' * @throws FooException <strong>Important <em>description</em> etc</strong>' . PHP_EOL .
' */',
new PhpDocNode([
new PhpDocTagNode(
'@throws',
new ThrowsTagValueNode(
new IdentifierTypeNode('FooException'),
'<strong>Important <em>description</em> etc</strong>'
)
),
]),
];

yield [
'Description with HTML tags in @mixin tag',
'/**' . PHP_EOL .
' * @mixin Mixin <strong>Important description</strong>' . PHP_EOL .
' */',
new PhpDocNode([
new PhpDocTagNode(
'@mixin',
new MixinTagValueNode(
new IdentifierTypeNode('Mixin'),
'<strong>Important description</strong>'
)
),
]),
];

yield [
'Description with unclosed HTML tags in @return tag - unclosed HTML tag is parsed as generics',
'/**' . PHP_EOL .
' * @return Foo <strong>Important description' . PHP_EOL .
' */',
new PhpDocNode([
new PhpDocTagNode(
'@return',
new ReturnTagValueNode(
new GenericTypeNode(
new IdentifierTypeNode('Foo'),
[
new IdentifierTypeNode('strong'),
]
),
'Important description'
)
),
]),
];
}

public function dataParseTagValue(): array
{
return [
Expand Down