Skip to content

Commit 07f5cc6

Browse files
authored
[Php84] Add NewMethodCallWithoutParenthesesRector (#6802)
* [Php84] Add NewMethodCallWithoutParenthesesRector * [Php84] Add NewMethodCallWithoutParenthesesRector * implemented * implemented
1 parent cf82f6a commit 07f5cc6

File tree

7 files changed

+170
-1
lines changed

7 files changed

+170
-1
lines changed

config/set/php84.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
use Rector\Config\RectorConfig;
66
use Rector\Php84\Rector\FuncCall\AddEscapeArgumentRector;
77
use Rector\Php84\Rector\FuncCall\RoundingModeEnumRector;
8+
use Rector\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector;
89
use Rector\Php84\Rector\Param\ExplicitNullableParamTypeRector;
910

1011
return static function (RectorConfig $rectorConfig): void {
1112
$rectorConfig->rules(
12-
[ExplicitNullableParamTypeRector::class, RoundingModeEnumRector::class, AddEscapeArgumentRector::class]
13+
[ExplicitNullableParamTypeRector::class, RoundingModeEnumRector::class, AddEscapeArgumentRector::class, NewMethodCallWithoutParenthesesRector::class]
1314
);
1415
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector\Fixture;
4+
5+
final class Fixture
6+
{
7+
public function run()
8+
{
9+
(new Request())->withMethod('GET')->withUri('/hello-world');
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector\Fixture;
18+
19+
final class Fixture
20+
{
21+
public function run()
22+
{
23+
new Request()->withMethod('GET')->withUri('/hello-world');
24+
}
25+
}
26+
27+
?>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector\Fixture;
4+
5+
final class SkipWithoutParentheses
6+
{
7+
public function run()
8+
{
9+
new Request()->withMethod('GET')->withUri('/hello-world');
10+
}
11+
}
12+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class NewMethodCallWithoutParenthesesRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector;
7+
use Rector\ValueObject\PhpVersion;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->rule(NewMethodCallWithoutParenthesesRector::class);
11+
12+
$rectorConfig->phpVersion(PhpVersion::PHP_84);
13+
};
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Php84\Rector\MethodCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\MethodCall;
9+
use PhpParser\Node\Expr\New_;
10+
use Rector\Rector\AbstractRector;
11+
use Rector\ValueObject\PhpVersionFeature;
12+
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
13+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
14+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
15+
16+
/**
17+
* @see \Rector\Tests\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector\NewMethodCallWithoutParenthesesRectorTest
18+
*/
19+
final class NewMethodCallWithoutParenthesesRector extends AbstractRector implements MinPhpVersionInterface
20+
{
21+
/**
22+
* @return array<class-string<Node>>
23+
*/
24+
public function getNodeTypes(): array
25+
{
26+
return [MethodCall::class];
27+
}
28+
29+
public function getRuleDefinition(): RuleDefinition
30+
{
31+
return new RuleDefinition(
32+
'Remove parentheses on new method call with parentheses',
33+
[
34+
new CodeSample(
35+
<<<'CODE_SAMPLE'
36+
(new Request())->withMethod('GET')->withUri('/hello-world');
37+
CODE_SAMPLE
38+
,
39+
<<<'CODE_SAMPLE'
40+
new Request()->withMethod('GET')->withUri('/hello-world');
41+
CODE_SAMPLE
42+
),
43+
]
44+
);
45+
}
46+
47+
/**
48+
* @param MethodCall $node
49+
*/
50+
public function refactor(Node $node): ?Node
51+
{
52+
if (! $node->var instanceof New_) {
53+
return null;
54+
}
55+
56+
$oldTokens = $this->file->getOldTokens();
57+
58+
// start node
59+
if (! isset($oldTokens[$node->getStartTokenPos()])) {
60+
return null;
61+
}
62+
63+
// end of "var" node
64+
if (! isset($oldTokens[$node->var->getEndTokenPos()])) {
65+
return null;
66+
}
67+
68+
if ((string) $oldTokens[$node->getStartTokenPos()] === '(' && (string) $oldTokens[$node->var->getEndTokenPos()] === ')') {
69+
$oldTokens[$node->getStartTokenPos()]->text = '';
70+
$oldTokens[$node->var->getEndTokenPos()]->text = '';
71+
72+
return $node;
73+
}
74+
75+
return null;
76+
}
77+
78+
public function provideMinPhpVersion(): int
79+
{
80+
return PhpVersionFeature::NEW_METHOD_CALL_WITHOUT_PARENTHESES;
81+
}
82+
}

src/ValueObject/PhpVersionFeature.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,12 @@ final class PhpVersionFeature
684684
*/
685685
public const DEPRECATE_IMPLICIT_NULLABLE_PARAM_TYPE = PhpVersion::PHP_84;
686686

687+
/**
688+
* @see https://wiki.php.net/rfc/new_without_parentheses
689+
* @var int
690+
*/
691+
public const NEW_METHOD_CALL_WITHOUT_PARENTHESES = PhpVersion::PHP_84;
692+
687693
/**
688694
* @see https://wiki.php.net/rfc/correctly_name_the_rounding_mode_and_make_it_an_enum
689695
* @var int

0 commit comments

Comments
 (0)