Skip to content

Commit 8470519

Browse files
authored
[DowngradePhp84] Add DowngradeNewMethodCallWithoutParenthesesRector (#253)
1 parent 207269f commit 8470519

File tree

10 files changed

+183
-1
lines changed

10 files changed

+183
-1
lines changed

config/set/downgrade-php84.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector;
7+
use Rector\ValueObject\PhpVersion;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->phpVersion(PhpVersion::PHP_83);
11+
$rectorConfig->rules([
12+
DowngradeNewMethodCallWithoutParenthesesRector::class,
13+
]);
14+
};

config/set/level/down-to-php82.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6+
use Rector\Set\ValueObject\DowngradeLevelSetList;
67
use Rector\Set\ValueObject\DowngradeSetList;
78

89
return static function (RectorConfig $rectorConfig): void {
9-
$rectorConfig->sets([DowngradeSetList::PHP_83]);
10+
$rectorConfig->sets([DowngradeLevelSetList::DOWN_TO_PHP_83, DowngradeSetList::PHP_83]);
1011
};

config/set/level/down-to-php83.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Set\ValueObject\DowngradeSetList;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->sets([DowngradeSetList::PHP_84]);
10+
};
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\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DowngradeNewMethodCallWithoutParenthesesRectorTest 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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\Fixture;
6+
7+
class Fixture
8+
{
9+
public function run()
10+
{
11+
new Request()->withMethod('GET')->withUri('/hello-world');
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
declare(strict_types=1);
20+
21+
namespace Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\Fixture;
22+
23+
class Fixture
24+
{
25+
public function run()
26+
{
27+
(new Request())->withMethod('GET')->withUri('/hello-world');
28+
}
29+
}
30+
31+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\Fixture;
6+
7+
final class SkipAlreadyParentheses
8+
{
9+
public function run()
10+
{
11+
(new Request())->withMethod('GET')->withUri('/hello-world');
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(DowngradeNewMethodCallWithoutParenthesesRector::class);
10+
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DowngradePhp84\Rector\MethodCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\MethodCall;
9+
use PhpParser\Node\Expr\New_;
10+
use Rector\NodeTypeResolver\Node\AttributeKey;
11+
use Rector\Rector\AbstractRector;
12+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
13+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
14+
15+
/**
16+
* @changelog https://wiki.php.net/rfc/new_without_parentheses
17+
*
18+
* @see \Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\DowngradeNewMethodCallWithoutParenthesesRectorTest
19+
*/
20+
final class DowngradeNewMethodCallWithoutParenthesesRector extends AbstractRector
21+
{
22+
/**
23+
* @return array<class-string<Node>>
24+
*/
25+
public function getNodeTypes(): array
26+
{
27+
return [MethodCall::class];
28+
}
29+
30+
public function getRuleDefinition(): RuleDefinition
31+
{
32+
return new RuleDefinition(
33+
'Add parentheses on new method call without parentheses',
34+
[
35+
new CodeSample(
36+
<<<'CODE_SAMPLE'
37+
new Request()->withMethod('GET')->withUri('/hello-world');
38+
CODE_SAMPLE
39+
,
40+
<<<'CODE_SAMPLE'
41+
(new Request())->withMethod('GET')->withUri('/hello-world');
42+
CODE_SAMPLE
43+
),
44+
]
45+
);
46+
}
47+
48+
/**
49+
* @param MethodCall $node
50+
*/
51+
public function refactor(Node $node): ?Node
52+
{
53+
if (! $node->var instanceof New_) {
54+
return null;
55+
}
56+
57+
$oldTokens = $this->file->getOldTokens();
58+
if (isset($oldTokens[$node->getStartTokenPos()]) && (string) $oldTokens[$node->getStartTokenPos()] === '(') {
59+
return null;
60+
}
61+
62+
$node->var->setAttribute(AttributeKey::ORIGINAL_NODE, null);
63+
return $node;
64+
}
65+
}

src/Set/ValueObject/DowngradeLevelSetList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
*/
1111
final class DowngradeLevelSetList
1212
{
13+
/**
14+
* @var string
15+
*/
16+
public const DOWN_TO_PHP_83 = __DIR__ . '/../../../config/set/level/down-to-php83.php';
17+
1318
/**
1419
* @var string
1520
*/

src/Set/ValueObject/DowngradeSetList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ final class DowngradeSetList
4444
* @var string
4545
*/
4646
public const PHP_83 = __DIR__ . '/../../../config/set/downgrade-php83.php';
47+
48+
/**
49+
* @var string
50+
*/
51+
public const PHP_84 = __DIR__ . '/../../../config/set/downgrade-php84.php';
4752
}

0 commit comments

Comments
 (0)