Skip to content

Commit e9fc2fd

Browse files
authored
[Php84] Remove all wrapping parentheses and keep arguments parenthesis on NewMethodCallWithoutParenthesesRector (#9010)
1 parent 09cacdc commit e9fc2fd

3 files changed

Lines changed: 104 additions & 9 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector\Fixture;
4+
5+
final class DoubleParentheses
6+
{
7+
public function run()
8+
{
9+
((new Request()))->withMethod('GET');
10+
$message = (((new Regex('some-pattern'))))->message;
11+
}
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector\Fixture;
19+
20+
final class DoubleParentheses
21+
{
22+
public function run()
23+
{
24+
new Request()->withMethod('GET');
25+
$message = new Regex('some-pattern')->message;
26+
}
27+
}
28+
29+
?>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector\Fixture;
4+
5+
final class MultilineParentheses
6+
{
7+
public function run()
8+
{
9+
$message = (
10+
new Regex('some-pattern')
11+
)->message;
12+
$value = (new Regex('some-pattern') /* comment */)->message;
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector\Fixture;
21+
22+
final class MultilineParentheses
23+
{
24+
public function run()
25+
{
26+
$message =
27+
new Regex('some-pattern')
28+
->message;
29+
$value = new Regex('some-pattern') /* comment */->message;
30+
}
31+
}
32+
33+
?>

‎rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php‎

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,57 @@ public function refactor(Node $node): ?Node
9494
return null;
9595
}
9696

97-
// start node
98-
if (! isset($oldTokens[$node->getStartTokenPos()])) {
97+
// "new" must end with arguments, e.g. skip "new Foo" or anonymous class
98+
if (! isset($oldTokens[$new->getEndTokenPos()]) || (string) $oldTokens[$new->getEndTokenPos()] !== ')') {
9999
return null;
100100
}
101101

102-
// end of "new" node
103-
if (! isset($oldTokens[$new->getEndTokenPos()])) {
102+
// all wrapping "(" between the node start and "new"
103+
$openingPositions = [];
104+
for ($position = $node->getStartTokenPos(); $position < $new->getStartTokenPos(); ++$position) {
105+
if (! isset($oldTokens[$position])) {
106+
return null;
107+
}
108+
109+
if ($oldTokens[$position]->isIgnorable()) {
110+
continue;
111+
}
112+
113+
if ((string) $oldTokens[$position] !== '(') {
114+
return null;
115+
}
116+
117+
$openingPositions[] = $position;
118+
}
119+
120+
if ($openingPositions === []) {
104121
return null;
105122
}
106123

107-
if ((string) $oldTokens[$node->getStartTokenPos()] === '(' && (string) $oldTokens[$new->getEndTokenPos()] === ')') {
108-
$oldTokens[$node->getStartTokenPos()]->text = '';
109-
$oldTokens[$new->getEndTokenPos()]->text = '';
124+
// the matching ")" after "new", not the arguments ")"
125+
$closingPositions = [];
126+
$position = $new->getEndTokenPos() + 1;
127+
while (count($closingPositions) < count($openingPositions)) {
128+
if (! isset($oldTokens[$position])) {
129+
return null;
130+
}
131+
132+
if (! $oldTokens[$position]->isIgnorable()) {
133+
if ((string) $oldTokens[$position] !== ')') {
134+
return null;
135+
}
136+
137+
$closingPositions[] = $position;
138+
}
139+
140+
++$position;
141+
}
110142

111-
return $node;
143+
foreach ([...$openingPositions, ...$closingPositions] as $parenthesisPosition) {
144+
$oldTokens[$parenthesisPosition]->text = '';
112145
}
113146

114-
return null;
147+
return $node;
115148
}
116149

117150
public function provideMinPhpVersion(): int

0 commit comments

Comments
 (0)