From 1ac73fe61a3377e0a6ebcdedd16ebb048e7407fd Mon Sep 17 00:00:00 2001 From: Aleksey Polyvanyi Date: Fri, 20 Nov 2026 11:41:45 +0100 Subject: [PATCH 1/2] [Php84] Handle property fetch, static access, array access and invocation on NewMethodCallWithoutParenthesesRector --- .../Fixture/array_dim_fetch.php.inc | 27 +++++++++ .../dynamic_variable_class_name.php.inc | 29 +++++++++ .../Fixture/invoke.php.inc | 29 +++++++++ .../Fixture/nullsafe.php.inc | 29 +++++++++ .../Fixture/property_fetch.php.inc | 29 +++++++++ .../skip_already_without_parentheses.php.inc | 15 +++++ .../Fixture/skip_anonymous_class.php.inc | 18 ++++++ .../skip_dynamic_class_name_expr.php.inc | 14 +++++ .../skip_no_arguments_parentheses.php.inc | 16 +++++ .../Fixture/skip_not_new.php.inc | 13 ++++ .../Fixture/static_access.php.inc | 33 ++++++++++ .../NewMethodCallWithoutParenthesesRector.php | 60 +++++++++++++++---- 12 files changed, 301 insertions(+), 11 deletions(-) create mode 100644 rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/array_dim_fetch.php.inc create mode 100644 rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/dynamic_variable_class_name.php.inc create mode 100644 rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/invoke.php.inc create mode 100644 rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/nullsafe.php.inc create mode 100644 rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/property_fetch.php.inc create mode 100644 rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_already_without_parentheses.php.inc create mode 100644 rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_anonymous_class.php.inc create mode 100644 rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_dynamic_class_name_expr.php.inc create mode 100644 rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_no_arguments_parentheses.php.inc create mode 100644 rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_not_new.php.inc create mode 100644 rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/static_access.php.inc diff --git a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/array_dim_fetch.php.inc b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/array_dim_fetch.php.inc new file mode 100644 index 00000000000..5ae9e6f8b5d --- /dev/null +++ b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/array_dim_fetch.php.inc @@ -0,0 +1,27 @@ + 'value']))['key']; + } +} + +?> +----- + 'value'])['key']; + } +} + +?> diff --git a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/dynamic_variable_class_name.php.inc b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/dynamic_variable_class_name.php.inc new file mode 100644 index 00000000000..b02ed562e83 --- /dev/null +++ b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/dynamic_variable_class_name.php.inc @@ -0,0 +1,29 @@ +run(); + $value = (new static())->value; + } +} + +?> +----- +run(); + $value = new static()->value; + } +} + +?> diff --git a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/invoke.php.inc b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/invoke.php.inc new file mode 100644 index 00000000000..a46af3753d2 --- /dev/null +++ b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/invoke.php.inc @@ -0,0 +1,29 @@ + +----- + diff --git a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/nullsafe.php.inc b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/nullsafe.php.inc new file mode 100644 index 00000000000..51b42a62c85 --- /dev/null +++ b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/nullsafe.php.inc @@ -0,0 +1,29 @@ +withMethod('GET'); + $uri = (new Request())?->uri; + } +} + +?> +----- +withMethod('GET'); + $uri = new Request()?->uri; + } +} + +?> diff --git a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/property_fetch.php.inc b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/property_fetch.php.inc new file mode 100644 index 00000000000..b68e2396307 --- /dev/null +++ b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/property_fetch.php.inc @@ -0,0 +1,29 @@ +message; + $nested = (new Request())->uri->path; + } +} + +?> +----- +message; + $nested = new Request()->uri->path; + } +} + +?> diff --git a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_already_without_parentheses.php.inc b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_already_without_parentheses.php.inc new file mode 100644 index 00000000000..d6c7c9ca4e7 --- /dev/null +++ b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_already_without_parentheses.php.inc @@ -0,0 +1,15 @@ +message; + new Request()?->withMethod('GET'); + $constant = new Request()::METHOD_GET; + $value = new \ArrayObject(['key' => 'value'])['key']; + $result = new Invokable()(); + } +} diff --git a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_anonymous_class.php.inc b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_anonymous_class.php.inc new file mode 100644 index 00000000000..fe83df9ae27 --- /dev/null +++ b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_anonymous_class.php.inc @@ -0,0 +1,18 @@ +value; + $result = (new class() { + public function __invoke() + { + } + })(); + } +} diff --git a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_dynamic_class_name_expr.php.inc b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_dynamic_class_name_expr.php.inc new file mode 100644 index 00000000000..fd3bd0c4ab8 --- /dev/null +++ b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_dynamic_class_name_expr.php.inc @@ -0,0 +1,14 @@ +resolveClass())())->message; + $constant = (new ($this->resolveClass())())::METHOD_GET; + $value = (new ($this->resolveClass())())['key']; + $result = (new ($this->resolveClass())())(); + } +} diff --git a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_no_arguments_parentheses.php.inc b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_no_arguments_parentheses.php.inc new file mode 100644 index 00000000000..9e0395088fa --- /dev/null +++ b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_no_arguments_parentheses.php.inc @@ -0,0 +1,16 @@ +withMethod('GET'); + $message = (new Regex)->message; + (new Request)?->withMethod('GET'); + $constant = (new Request)::METHOD_GET; + $value = (new \ArrayObject)['key']; + $result = (new Invokable)(); + } +} diff --git a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_not_new.php.inc b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_not_new.php.inc new file mode 100644 index 00000000000..33a832c482a --- /dev/null +++ b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/skip_not_new.php.inc @@ -0,0 +1,13 @@ +regex)->message; + $value = ($this->items)['key']; + $result = ($this->callback)(); + } +} diff --git a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/static_access.php.inc b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/static_access.php.inc new file mode 100644 index 00000000000..e0aa81766cd --- /dev/null +++ b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/static_access.php.inc @@ -0,0 +1,33 @@ + +----- + diff --git a/rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php b/rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php index c2f628dfcb2..688fa0741d8 100644 --- a/rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php +++ b/rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php @@ -5,8 +5,17 @@ namespace Rector\Php84\Rector\MethodCall; use PhpParser\Node; +use PhpParser\Node\Expr; +use PhpParser\Node\Expr\ArrayDimFetch; +use PhpParser\Node\Expr\ClassConstFetch; +use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; +use PhpParser\Node\Expr\NullsafeMethodCall; +use PhpParser\Node\Expr\NullsafePropertyFetch; +use PhpParser\Node\Expr\PropertyFetch; +use PhpParser\Node\Expr\StaticCall; +use PhpParser\Node\Expr\StaticPropertyFetch; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; use Rector\RuleDoc\RuleDefinition; @@ -23,21 +32,35 @@ final class NewMethodCallWithoutParenthesesRector extends AbstractRector impleme */ public function getNodeTypes(): array { - return [MethodCall::class]; + return [ + MethodCall::class, + NullsafeMethodCall::class, + PropertyFetch::class, + NullsafePropertyFetch::class, + StaticCall::class, + StaticPropertyFetch::class, + ClassConstFetch::class, + ArrayDimFetch::class, + FuncCall::class, + ]; } public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( - 'Remove parentheses on new method call with parentheses', + 'Remove parentheses around new with arguments on method call, property fetch, static access, array access or invocation', [ new CodeSample( <<<'CODE_SAMPLE' (new Request())->withMethod('GET')->withUri('/hello-world'); +$message = (new Regex('some-pattern'))->message; +$value = (new ArrayObject($items))['key']; CODE_SAMPLE , <<<'CODE_SAMPLE' new Request()->withMethod('GET')->withUri('/hello-world'); +$message = new Regex('some-pattern')->message; +$value = new ArrayObject($items)['key']; CODE_SAMPLE ), ] @@ -45,11 +68,12 @@ public function getRuleDefinition(): RuleDefinition } /** - * @param MethodCall $node + * @param MethodCall|NullsafeMethodCall|PropertyFetch|NullsafePropertyFetch|StaticCall|StaticPropertyFetch|ClassConstFetch|ArrayDimFetch|FuncCall $node */ public function refactor(Node $node): ?Node { - if (! $node->var instanceof New_) { + $new = $this->resolveDereferencedExpr($node); + if (! $new instanceof New_) { return null; } @@ -57,13 +81,13 @@ public function refactor(Node $node): ?Node ->getOldTokens(); $loop = 1; - while (isset($oldTokens[$node->var->getStartTokenPos() + $loop])) { - if (trim((string) $oldTokens[$node->var->getStartTokenPos() + $loop]) === '') { + while (isset($oldTokens[$new->getStartTokenPos() + $loop])) { + if (trim((string) $oldTokens[$new->getStartTokenPos() + $loop]) === '') { ++$loop; continue; } - if ((string) $oldTokens[$node->var->getStartTokenPos() + $loop] !== '(') { + if ((string) $oldTokens[$new->getStartTokenPos() + $loop] !== '(') { break; } @@ -75,14 +99,14 @@ public function refactor(Node $node): ?Node return null; } - // end of "var" node - if (! isset($oldTokens[$node->var->getEndTokenPos()])) { + // end of "new" node + if (! isset($oldTokens[$new->getEndTokenPos()])) { return null; } - if ((string) $oldTokens[$node->getStartTokenPos()] === '(' && (string) $oldTokens[$node->var->getEndTokenPos()] === ')') { + if ((string) $oldTokens[$node->getStartTokenPos()] === '(' && (string) $oldTokens[$new->getEndTokenPos()] === ')') { $oldTokens[$node->getStartTokenPos()]->text = ''; - $oldTokens[$node->var->getEndTokenPos()]->text = ''; + $oldTokens[$new->getEndTokenPos()]->text = ''; return $node; } @@ -94,4 +118,18 @@ public function provideMinPhpVersion(): int { return PhpVersion::PHP_84; } + + private function resolveDereferencedExpr( + MethodCall|NullsafeMethodCall|PropertyFetch|NullsafePropertyFetch|StaticCall|StaticPropertyFetch|ClassConstFetch|ArrayDimFetch|FuncCall $node + ): ?Node { + if ($node instanceof StaticCall || $node instanceof StaticPropertyFetch || $node instanceof ClassConstFetch) { + return $node->class; + } + + if ($node instanceof FuncCall) { + return $node->name instanceof Expr ? $node->name : null; + } + + return $node->var; + } } From 4df78a9c1046d6a0ead7c83e44f6ef35cfe69483 Mon Sep 17 00:00:00 2001 From: Aleksey Polyvanyi Date: Fri, 20 Nov 2026 13:14:12 +0100 Subject: [PATCH 2/2] Rename fixtures to avoid native node short names --- ...array_dim_fetch.php.inc => new_on_array_dim_fetch.php.inc} | 4 ++-- .../{property_fetch.php.inc => new_on_property_fetch.php.inc} | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) rename rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/{array_dim_fetch.php.inc => new_on_array_dim_fetch.php.inc} (87%) rename rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/{property_fetch.php.inc => new_on_property_fetch.php.inc} (89%) diff --git a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/array_dim_fetch.php.inc b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/new_on_array_dim_fetch.php.inc similarity index 87% rename from rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/array_dim_fetch.php.inc rename to rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/new_on_array_dim_fetch.php.inc index 5ae9e6f8b5d..70b2aeba05e 100644 --- a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/array_dim_fetch.php.inc +++ b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/new_on_array_dim_fetch.php.inc @@ -2,7 +2,7 @@ namespace Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector\Fixture; -final class ArrayDimFetch +final class NewOnArrayDimFetch { public function run() { @@ -16,7 +16,7 @@ final class ArrayDimFetch namespace Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector\Fixture; -final class ArrayDimFetch +final class NewOnArrayDimFetch { public function run() { diff --git a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/property_fetch.php.inc b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/new_on_property_fetch.php.inc similarity index 89% rename from rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/property_fetch.php.inc rename to rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/new_on_property_fetch.php.inc index b68e2396307..164017565c3 100644 --- a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/property_fetch.php.inc +++ b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/new_on_property_fetch.php.inc @@ -2,7 +2,7 @@ namespace Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector\Fixture; -final class PropertyFetch +final class NewOnPropertyFetch { public function run() { @@ -17,7 +17,7 @@ final class PropertyFetch namespace Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector\Fixture; -final class PropertyFetch +final class NewOnPropertyFetch { public function run() {