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 DynamicVariableClassName
{
public function run()
{
(new $className())->run();
$value = (new static())->value;
}
}

?>
-----
<?php

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

final class DynamicVariableClassName
{
public function run()
{
new $className()->run();
$value = new static()->value;
}
}

?>
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 Invoke
{
public function run()
{
$result = (new Invokable())();
$withArgs = (new Invokable(1))(2, 3);
}
}

?>
-----
<?php

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

final class Invoke
{
public function run()
{
$result = new Invokable()();
$withArgs = new Invokable(1)(2, 3);
}
}

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

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

final class NewOnArrayDimFetch
{
public function run()
{
$value = (new \ArrayObject(['key' => 'value']))['key'];
}
}

?>
-----
<?php

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

final class NewOnArrayDimFetch
{
public function run()
{
$value = new \ArrayObject(['key' => 'value'])['key'];
}
}

?>
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 NewOnPropertyFetch
{
public function run()
{
$message = (new Regex('some-pattern'))->message;
$nested = (new Request())->uri->path;
}
}

?>
-----
<?php

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

final class NewOnPropertyFetch
{
public function run()
{
$message = new Regex('some-pattern')->message;
$nested = new Request()->uri->path;
}
}

?>
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 Nullsafe
{
public function run()
{
(new Request())?->withMethod('GET');
$uri = (new Request())?->uri;
}
}

?>
-----
<?php

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

final class Nullsafe
{
public function run()
{
new Request()?->withMethod('GET');
$uri = new Request()?->uri;
}
}

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

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

final class SkipAlreadyWithoutParentheses
{
public function run()
{
$message = new Regex('some-pattern')->message;
new Request()?->withMethod('GET');
$constant = new Request()::METHOD_GET;
$value = new \ArrayObject(['key' => 'value'])['key'];
$result = new Invokable()();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

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

final class SkipAnonymousClass
{
public function run()
{
$value = (new class {
public $value = 1;
})->value;
$result = (new class() {
public function __invoke()
{
}
})();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

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

final class SkipDynamicClassNameExpr
{
public function run()
{
$message = (new ($this->resolveClass())())->message;
$constant = (new ($this->resolveClass())())::METHOD_GET;
$value = (new ($this->resolveClass())())['key'];
$result = (new ($this->resolveClass())())();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

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

final class SkipNoArgumentsParentheses
{
public function run()
{
(new Request)->withMethod('GET');
$message = (new Regex)->message;
(new Request)?->withMethod('GET');
$constant = (new Request)::METHOD_GET;
$value = (new \ArrayObject)['key'];
$result = (new Invokable)();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

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

final class SkipNotNew
{
public function run()
{
$message = ($this->regex)->message;
$value = ($this->items)['key'];
$result = ($this->callback)();
}
}
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 StaticAccess
{
public function run()
{
(new Request())::create();
$constant = (new Request())::METHOD_GET;
$property = (new Request())::$defaultMethod;
$className = (new Request())::class;
}
}

?>
-----
<?php

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

final class StaticAccess
{
public function run()
{
new Request()::create();
$constant = new Request()::METHOD_GET;
$property = new Request()::$defaultMethod;
$className = new Request()::class;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,47 +32,62 @@ 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
),
]
);
}

/**
* @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;
}

$oldTokens = $this->getFile()
->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;
}

Expand All @@ -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;
}
Expand All @@ -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;
}
}