plugin-php icon indicating copy to clipboard operation
plugin-php copied to clipboard

Bug: Prettier will remove parenthesis around new objects calling a function

Open gmitch215 opened this issue 3 months ago • 4 comments

If I have a line like so:

$timestamp = (new DateTimeImmutable())->format(DATE_ATOM);

Prettier will reformat it as:

$timestamp = new DateTimeImmutable()->format(DATE_ATOM)

which is not valid PHP code.

gmitch215 avatar Sep 25 '25 14:09 gmitch215

fwiw it's valid in PHP 8.4 : https://3v4l.org/MtjXX

nicoder avatar Sep 25 '25 15:09 nicoder

@nicoder thx, tho I'm on PHP 8.3 for some projects, would appreciate it if this rule could be disabled based on php.version in settings.json

gmitch215 avatar Sep 27 '25 18:09 gmitch215

I'm having the same problem.

Before:

return (new View) [...]

After:

return new View() [...]

Running PHP 8.2 so won't help if it would work on PHP 8.4.

espenlg avatar Sep 27 '25 22:09 espenlg

Thanks for the bug report @gmitch215! Contributions welcome :-)

czosel avatar Sep 29 '25 06:09 czosel

Are you correctly setting your PHP version in your .prettierrc.json and/or composer.json? The code that updates this does check for PHP 8.4 first...

https://github.com/prettier/plugin-php/blob/0c883a49850281077218007322f6149f853b2015/src/needs-parens.mjs#L130-L133

hackel avatar Dec 12 '25 23:12 hackel