Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector\Fixture;

final class DoubleParentheses
{
public function run()
{
((new Request()))->withMethod('GET');
$message = (((new Regex('some-pattern'))))->message;
}
}

?>
-----
<?php

namespace Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector\Fixture;

final class DoubleParentheses
{
public function run()
{
new Request()->withMethod('GET');
$message = new Regex('some-pattern')->message;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector\Fixture;

final class MultilineParentheses
{
public function run()
{
$message = (
new Regex('some-pattern')
)->message;
$value = (new Regex('some-pattern') /* comment */)->message;
}
}

?>
-----
<?php

namespace Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector\Fixture;

final class MultilineParentheses
{
public function run()
{
$message =
new Regex('some-pattern')
->message;
$value = new Regex('some-pattern') /* comment */->message;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading