Skip to content

Support multiple lines for array shape #33

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 9 commits into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/Ast/PhpDoc/TemplateTagValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(string $name, ?TypeNode $bound, string $description)

public function __toString(): string
{
$bound = $this->bound ? " of {$this->bound}" : '';
$bound = isset($this->bound) ? " of {$this->bound}" : '';
Copy link
Collaborator

Choose a reason for hiding this comment

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

should be $this->bound !== null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 7e0ad3a

return trim("{$this->name}{$bound} {$this->description}");
}

Expand Down
3 changes: 3 additions & 0 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,15 @@ private function tryParseArray(TokenIterator $tokens, Ast\Type\TypeNode $type):
private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type): Ast\Type\TypeNode
{
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET);
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
$items = [$this->parseArrayShapeItem($tokens)];

while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
$items[] = $this->parseArrayShapeItem($tokens);
}

$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_CURLY_BRACKET);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that we should also allow line breaks before the comma, so that this works:

array{
    int
    , int
    , int
}

We might want to allow trailing commas as well:

array{
    int,
    int,
    int,
}

Copy link
Member

Choose a reason for hiding this comment

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

I agree. Trailing commas are awesome :)

Copy link
Member

Choose a reason for hiding this comment

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

Each of these examples should have a separate test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Trailing commas 571feed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Line breaks before commas 1f26308


return new Ast\Type\ArrayShapeNode($items);
Expand Down
7 changes: 6 additions & 1 deletion tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,12 @@ public function provideParseData(): array
]),
],
[
'array{a: int, b: array{c: callable(): int}}',
'array{
a: int,
b: array{
c: callable(): int
}
}',
Copy link
Collaborator

Choose a reason for hiding this comment

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

You should add new test instead of modifying an existing one

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also the test is incorrect, as it's missing the *. What you are actually parsing is sth like

array{
 * a: int,
 * b: array{
 *   c: callable(): int
 * }
 * }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it all right if I just add a single test showing that the leading *s don't disrupt anything? I'd otherwise like to omit them for readability. 414daf3

new ArrayShapeNode([
new ArrayShapeItemNode(
new IdentifierTypeNode('a'),
Expand Down