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

Support multiple lines for array shape #33

Merged
merged 9 commits into from
Oct 26, 2019
Merged

Support multiple lines for array shape #33

merged 9 commits into from
Oct 26, 2019

Conversation

shmax
Copy link
Contributor

@shmax shmax commented Oct 22, 2019

Possible fix for phpstan/phpstan#2497

Hey there, great library, fantastic work to all involved. So, like a lot of folks I'm excited about using array shapes, but without multiline syntax it doesn't really seem practical.

I haven't done much with parsers since my school days of long ago, and I was told that you have previously dismissed this idea as not feasible, so please tell me what I'm doing wrong.

@ondrejmirtes
Copy link
Member

Only @JanTvrdik or @arnaud-lb are able to review this 😊

@@ -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

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

$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

@ondrejmirtes
Copy link
Member

If we're able to finish this and merge it, I'm gonna release next phpdoc-parser in the current 0.3.x series and PHPStan 0.11.19 with support for multiline array shapes! 👍

@shmax
Copy link
Contributor Author

shmax commented Oct 22, 2019

Did my best to add the requested features. I feel like the parseArrayShape method could be cleaned up a bit; something like

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

But then it just sort of runs wild and doesn't throw when an expected closing curly isn't encountered. Any suggestions?

Also, what's the best way to run lint and stan on this project? I didn't see a scripts section in composer.json, so I've just been running phpcs directly...

@ondrejmirtes
Copy link
Member

You can always check what .travis.yml does :) It's vendor/bin/phing.

@shmax
Copy link
Contributor Author

shmax commented Oct 22, 2019

You can always check what .travis.yml does :) It's vendor/bin/phing.

That's a bit more comprehensive, thank you. The /tools/abnfgen file doesn't build in my environment, but I assume that's because I'm using windows.

@ondrejmirtes
Copy link
Member

I'm happy with this. If someone has any more comments, we can fix them on master :) Thank you very much!

@ondrejmirtes ondrejmirtes merged commit 18b0e92 into phpstan:master Oct 26, 2019
@shmax shmax deleted the support-multiline-array-shape branch October 26, 2019 20:42
@shmax
Copy link
Contributor Author

shmax commented Oct 26, 2019

Yee-haw, thanks much. Looking forward to the next release...

@shmax
Copy link
Contributor Author

shmax commented Oct 27, 2019

@ondrejmirtes By the way, can you shed any light on what is going on with slevomat/coding-standard? Array-shape support was added in June, but there hasn't been a release since March ...

@ondrejmirtes
Copy link
Member

Nothing bad is going on with Slevomat CS, it’s an actively maintained project. @kukulich is planning to do a release.

@shmax
Copy link
Contributor Author

shmax commented Oct 27, 2019

Very well, then, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants