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,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\Fixture;

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

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\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,31 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\Fixture;

final class NestedNew
{
public function run()
{
$value = new Outer(new Inner()->value)->run();
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\Fixture;

final class NestedNew
{
public function run()
{
$value = (new Outer((new Inner())->value))->run();
}
}

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

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\Fixture;

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

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\Fixture;

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

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

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\Fixture;

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

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\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,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\Fixture;

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

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\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,19 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\Fixture;

final class SkipAlreadyParenthesesOtherDereferences
{
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())();
$callback = $this->callback;
$items = $this->items['key'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\Fixture;

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

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\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\DowngradePhp84\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,33 +32,48 @@ final class DowngradeNewMethodCallWithoutParenthesesRector extends AbstractRecto
*/
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(
'Add parentheses on new method call without parentheses',
'Add parentheses around new 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;
}

Expand All @@ -69,13 +93,27 @@ public function refactor(Node $node): ?Node

// idempotency: on a previous pass the opening paren was prepended to the New_ var token,
// so check that token too, otherwise the rule keeps wrapping the same call every pass
if (str_starts_with((string) $oldTokens[$node->var->getStartTokenPos()], '(')) {
if (str_starts_with((string) $oldTokens[$new->getStartTokenPos()], '(')) {
return null;
}

$oldTokens[$node->var->getStartTokenPos()]->text = '(' . $oldTokens[$node->var->getStartTokenPos()];
$oldTokens[$node->var->getEndTokenPos()]->text .= ')';
$oldTokens[$new->getStartTokenPos()]->text = '(' . $oldTokens[$new->getStartTokenPos()];
$oldTokens[$new->getEndTokenPos()]->text .= ')';

return $node;
}

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;
}
}
Loading