Skip to content

[Php84] Handle property fetch, static access, array access and invocation on NewMethodCallWithoutParenthesesRector - #9009

Merged
TomasVotruba merged 2 commits into
rectorphp:mainfrom
alexndlm:new-without-parentheses-all-dereferences
Nov 20, 2026
Merged

TomasVotruba merged 2 commits into
rectorphp:mainfrom
alexndlm:new-without-parentheses-all-dereferences

Conversation

@alexndlm

Copy link
Copy Markdown
Contributor

NewMethodCallWithoutParenthesesRector only handles MethodCall, so the redundant parentheses stay on the other dereferences PHP 8.4 allows on new with arguments:

$message = (new Regex('some-pattern'))->message;
(new Request())?->withMethod('GET');
$constant = (new Request())::METHOD_GET;
$value = (new ArrayObject($items))['key'];
$result = (new Invokable())();

All of these are valid without parentheses since PHP 8.4 (https://wiki.php.net/rfc/new_without_parentheses), e.g. new Regex('some-pattern')->message.

The rule now also handles PropertyFetch, NullsafeMethodCall, NullsafePropertyFetch, StaticCall, StaticPropertyFetch, ClassConstFetch, ArrayDimFetch and FuncCall with a New_ name. The token check is unchanged, so new Foo without argument parentheses, new (expr) and anonymous classes are still skipped.

Added a fixture for each form, and skip fixtures for new without arguments, dynamic class name, anonymous class, already unwrapped code and non-new expressions.

…tion on NewMethodCallWithoutParenthesesRector
@TomasVotruba

Copy link
Copy Markdown
Member

Thank you, LGTM 👍

@TomasVotruba
TomasVotruba merged commit 09cacdc into rectorphp:main Nov 20, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants