From bdc19eb91ce9c3234a97718fe93472eabe660b23 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 20 Nov 2026 18:00:23 +0100 Subject: [PATCH] [Php84] Remove all wrapping parentheses and keep arguments parenthesis on NewMethodCallWithoutParenthesesRector --- .../Fixture/double_parentheses.php.inc | 29 +++++++++++ .../Fixture/multiline_parentheses.php.inc | 33 ++++++++++++ .../NewMethodCallWithoutParenthesesRector.php | 51 +++++++++++++++---- 3 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/double_parentheses.php.inc create mode 100644 rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/multiline_parentheses.php.inc diff --git a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/double_parentheses.php.inc b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/double_parentheses.php.inc new file mode 100644 index 00000000000..e865d4dbc74 --- /dev/null +++ b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/double_parentheses.php.inc @@ -0,0 +1,29 @@ +withMethod('GET'); + $message = (((new Regex('some-pattern'))))->message; + } +} + +?> +----- +withMethod('GET'); + $message = new Regex('some-pattern')->message; + } +} + +?> diff --git a/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/multiline_parentheses.php.inc b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/multiline_parentheses.php.inc new file mode 100644 index 00000000000..be812af7050 --- /dev/null +++ b/rules-tests/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector/Fixture/multiline_parentheses.php.inc @@ -0,0 +1,33 @@ +message; + $value = (new Regex('some-pattern') /* comment */)->message; + } +} + +?> +----- +message; + $value = new Regex('some-pattern') /* comment */->message; + } +} + +?> diff --git a/rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php b/rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php index 688fa0741d8..364cb8ac008 100644 --- a/rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php +++ b/rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php @@ -94,24 +94,57 @@ public function refactor(Node $node): ?Node return null; } - // start node - if (! isset($oldTokens[$node->getStartTokenPos()])) { + // "new" must end with arguments, e.g. skip "new Foo" or anonymous class + if (! isset($oldTokens[$new->getEndTokenPos()]) || (string) $oldTokens[$new->getEndTokenPos()] !== ')') { return null; } - // end of "new" node - if (! isset($oldTokens[$new->getEndTokenPos()])) { + // all wrapping "(" between the node start and "new" + $openingPositions = []; + for ($position = $node->getStartTokenPos(); $position < $new->getStartTokenPos(); ++$position) { + if (! isset($oldTokens[$position])) { + return null; + } + + if ($oldTokens[$position]->isIgnorable()) { + continue; + } + + if ((string) $oldTokens[$position] !== '(') { + return null; + } + + $openingPositions[] = $position; + } + + if ($openingPositions === []) { return null; } - if ((string) $oldTokens[$node->getStartTokenPos()] === '(' && (string) $oldTokens[$new->getEndTokenPos()] === ')') { - $oldTokens[$node->getStartTokenPos()]->text = ''; - $oldTokens[$new->getEndTokenPos()]->text = ''; + // the matching ")" after "new", not the arguments ")" + $closingPositions = []; + $position = $new->getEndTokenPos() + 1; + while (count($closingPositions) < count($openingPositions)) { + if (! isset($oldTokens[$position])) { + return null; + } + + if (! $oldTokens[$position]->isIgnorable()) { + if ((string) $oldTokens[$position] !== ')') { + return null; + } + + $closingPositions[] = $position; + } + + ++$position; + } - return $node; + foreach ([...$openingPositions, ...$closingPositions] as $parenthesisPosition) { + $oldTokens[$parenthesisPosition]->text = ''; } - return null; + return $node; } public function provideMinPhpVersion(): int