rector icon indicating copy to clipboard operation
rector copied to clipboard

[PHP 8.4] new MyClass()->method() without parentheses

Open ruudk opened this issue 1 year ago • 3 comments

Feature Request

We should create a rule for this RFC:

https://wiki.php.net/rfc/new_without_parentheses

Diff

-$request = (new Request())->withMethod('GET')->withUri('/hello-world');
+$request = new Request()->withMethod('GET')->withUri('/hello-world');

ruudk avatar Jun 28 '24 05:06 ruudk

I think that need to be feature request for nikic/php-parser

samsonasik avatar Jun 28 '24 05:06 samsonasik

I already asked in php-parser last week. This will most likely require php-paser 5, PHPStan 2 and then Rector 2 due to BC breaks.

https://github.com/nikic/PHP-Parser/commit/7b0384cdbe03431c44bff3261bea2717743a2b53#commitcomment-143390979

TomasVotruba avatar Jun 28 '24 07:06 TomasVotruba

I created downgrade PR rule first:

  • https://github.com/rectorphp/rector-downgrade-php/pull/253

to allow code to be downgraded on the first place.

samsonasik avatar Nov 27 '24 12:11 samsonasik

This will most likely require php-paser 5, PHPStan 2 and then Rector 2 due to BC breaks.

@samsonasik it seems to me that these criterion are now met. Is there anything more needed to create this new rule ?

gnutix avatar Jan 13 '25 11:01 gnutix

The no parenthesis syntax is not only on method call, but also static call, property fetch, etc, per rfc

https://wiki.php.net/rfc/new_without_parentheses

which downgrade need to be exists first so full feature downgrade need to be exists first to avoid flip-flop lookup.

I am not fan of this syntax sugar, but feel free to create PR if you're interested, always start with improving downgrade part, there is starting point you can look:

  • https://github.com/rectorphp/rector-downgrade-php/pull/253

samsonasik avatar Jan 13 '25 12:01 samsonasik

This issue has been automatically locked because it has been closed for 150 days. Please open a new issue if you have a similar problem.

github-actions[bot] avatar Dec 01 '25 03:12 github-actions[bot]