Skip to content

Multi Line Generic Types are invalid #42

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

Closed
ServerExe opened this issue May 4, 2020 · 9 comments
Closed

Multi Line Generic Types are invalid #42

ServerExe opened this issue May 4, 2020 · 9 comments

Comments

@ServerExe
Copy link

Bug report

PhpStan-Version: 0.12.19

The following error occurs when trying to declare a multi lined Generics annotation:

PHPDoc tag @implements has invalid value (HelloInterface< Foo, Bar): Unexpected token "\n *", expected type at offset 34

Code snippet that reproduces the problem

https://phpstan.org/r/80a149d9-5820-4041-bf3b-4b6e19de6b09

Expected output

No errors! as it is in:
https://phpstan.org/r/08857dad-5e7b-4615-9c99-f3397aa204ce

@ondrejmirtes
Copy link
Member

Yes, this isn't currently supported. Moving to phpdoc-parser repo if someone wanted to implement that.

@ondrejmirtes ondrejmirtes transferred this issue from phpstan/phpstan May 4, 2020
@icanhazstring
Copy link
Contributor

icanhazstring commented May 4, 2020

@ondrejmirtes thanks for moving. Can you point ppl in the right direction, like the file where the parsing for this takes place. So maybe someone can get startet right away :)

@ondrejmirtes
Copy link
Member

See how multiline array shapes are implemented:

private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type): Ast\Type\ArrayShapeNode
{
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET);
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
$items = [$this->parseArrayShapeItem($tokens)];
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_CLOSE_CURLY_BRACKET)) {
// trailing comma case
return new Ast\Type\ArrayShapeNode($items);
}
$items[] = $this->parseArrayShapeItem($tokens);
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
}
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_CURLY_BRACKET);
return new Ast\Type\ArrayShapeNode($items);
}

This is where generic types are parsed:

public function parseGeneric(TokenIterator $tokens, Ast\Type\IdentifierTypeNode $baseType): Ast\Type\GenericTypeNode
{
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET);
$genericTypes = [$this->parse($tokens)];
while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
$genericTypes[] = $this->parse($tokens);
}
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET);
return new Ast\Type\GenericTypeNode($baseType, $genericTypes);
}

@icanhazstring
Copy link
Contributor

Will try to tackle this one :)

@icanhazstring
Copy link
Contributor

@ondrejmirtes would it be wise to update the Tokenzier to avoid tokenizing the \n as a token itself.
The only thing "breaking" the code right now is the "new_line" token. With this removed, everything should work fine.

@icanhazstring
Copy link
Contributor

Nevermind, just found out how the parsing works exactly :)

ondrejmirtes pushed a commit that referenced this issue May 6, 2020
@icanhazstring
Copy link
Contributor

This can be closed then @ondrejmirtes :)

@ondrejmirtes
Copy link
Member

Yes, implemented in #44 :)

@github-actions
Copy link

github-actions bot commented May 1, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants