phpdoc-parser icon indicating copy to clipboard operation
phpdoc-parser copied to clipboard

Return type in `@method` starting with `static` cannot be parsed

Open mvorisek opened this issue 1 year ago • 4 comments
trafficstars

Bug report

I know the current workaround is to place parenthesis around the return type, but this is problematic, as almost every CS fixer removes the parenthesis.

Like \Closure(): void, ie. type even with space is supported [1], unioned (and intersected) types should be supported as well.

[1] https://phpstan.org/r/e9b294c3-2f5f-42a1-b4a7-c2ad93a83c2f

Code snippet that reproduces the problem

https://phpstan.org/r/ad8c4bfc-4c57-48fc-838e-fc3d14a6d491

Expected output

no phpstan parser error

mvorisek avatar Feb 07 '24 18:02 mvorisek

https://github.com/phpstan/phpdoc-parser/blob/1.25.0/doc/grammars/phpdoc-method.peg#L11 grammar already allows in as defined in https://github.com/phpstan/phpdoc-parser/blob/1.25.0/doc/grammars/type.abnf#L6

I wonder why the method grammar files are named *.peg instead of *.abnf and if fuzzed/tested.

mvorisek avatar Feb 07 '24 18:02 mvorisek

That has nothing to do with the type being union. It because it's a grammar conflict with static methods.

So for example, if you could use any of the following, it works just fine:

  • $this|null
  • ?static
  • null|static

JanTvrdik avatar Feb 07 '24 22:02 JanTvrdik

I tried improving the conflict resolution logic we use: https://github.com/phpstan/phpdoc-parser/pull/230

JanTvrdik avatar Feb 07 '24 22:02 JanTvrdik

Jan, thank you!

mvorisek avatar Feb 07 '24 23:02 mvorisek

Implemented https://github.com/phpstan/phpdoc-parser/pull/230

ondrejmirtes avatar Feb 23 '24 16:02 ondrejmirtes

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[bot] avatar Mar 26 '24 00:03 github-actions[bot]