plugin-php
plugin-php copied to clipboard
PHP8: Inconsistent formatting when using attributes
Prettier 2.4.1
PHP Plugin 0.17.6
Input:
use JetBrains\PhpStorm\Pure;
class Test
{
/**
* Method with an attribute.
* @param string $foo
* @return string
*/
#[Pure]
public function withAttribute(string $foo): string
{
return $foo;
}
/**
* Method without any attributes.
* @param string $foo
* @return string
*/
public function withoutAttribute(string $foo): string
{
return $foo;
}
}
Expected: Code is already correctly formatted => runnning Prettier should not result in any changes.
Result: The opening brace of the method that contains the attribute gets moved, the other one does not.
public function withAttribute(string $foo): string {
return $foo;
}
Additional note:
Removing the string $foo
parameter from the method signature makes the problem go away. So it seems to be a rather weird combination of factors that trigger the bug.
Same thing is happening here too.
Here's the playground for reference: https://loilo.github.io/prettier-php-playground/#N4IgDgTgpgLjCWUIHkwIPYDsDOIBcok8mMA6vACYwAW+AHAAwA0IMAhgEblW14AsLAK7YoAFU648AMzYAbES2zEA5rKgBFQehhR8M+VBZhqYAGpIlWfCDoA6AIwgWMCG3iyVAYXQBbH2wAFAAkA-BdBQxAOVwBjKABlGABPNWswbAgAWgAmJxBoAEdBeGgA12V-PTkFEGIRCBgytgq2KoMAXxYoCngYdBQ0eCxJUDZsGDaRTtrMMEEJvBAAHgB+YzAAHUwt4SgAAgApWAAhVzqNjYCTRP6fC4DBaABuLa2Y2THsPdEoca3gLZ7IF7AD0ACowYDgXswXsALKwajoCh7ADuvWoezYmCxcAg8A48ygtihwNhAAEwGxXD49uN8ZhlHsACRSdDoUlAinQGCPHH0lScmEgzkAYgA2g9oABdTlzDgeGJ7KSCTAxDA49E0ACCeIJRIAFALGSy2egAJR4OkuQU44EAu3QoE8vmm9kvR17dqvT3gyGemHwxHItEY9DzLGYJK4m2EnTYEkBilUmnWhlM1nsoXc2Cu43KbMiz3yxXK1XqoaasPzXWxw35t0Wq35zkOp3Al0QHGZ9Ae6HezDtEDtIA
The error resides within this block of code: https://github.com/prettier/plugin-php/blob/1c9a8dd06eb5bfd0f1008056d0a027cca2c15c2b/src/printer.js#L1458-L1472
The issue ultimately comes down to the willBreak function that is part of prettier core: https://github.com/prettier/prettier/blob/893f43a599dcaba249f012fecbc28a3dac008f81/src/document/doc-utils.js#L153
Any attribute returns:
{
"type": "concat",
"parts": [
{
"type": "line",
"hard": true
},
{
"type": "break-parent"
}
]
}
This triggers willBreak to return true. I guess a work around is needed in php prettier for this condition.
@czosel what if we filter out attributes from declaration
before sending it to some(willBreak)
I am experiencing the same issue. Any updates regarding this?
@tm1000 Sounds good - PRs welcome! :)
I think I have figured this out, we including the attrs as part of the is this a multiline function declaration check.
Fixed in v0.19.2
:tada: