Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DowngradePhp84] Add DowngradeNewMethodCallWithoutParenthesesRector #253

Merged
merged 1 commit into from
Nov 27, 2024
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
14 changes: 14 additions & 0 deletions config/set/downgrade-php84.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->phpVersion(PhpVersion::PHP_83);
$rectorConfig->rules([
DowngradeNewMethodCallWithoutParenthesesRector::class,
]);
};
3 changes: 2 additions & 1 deletion config/set/level/down-to-php82.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\DowngradeLevelSetList;
use Rector\Set\ValueObject\DowngradeSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([DowngradeSetList::PHP_83]);
$rectorConfig->sets([DowngradeLevelSetList::DOWN_TO_PHP_83, DowngradeSetList::PHP_83]);
};
10 changes: 10 additions & 0 deletions config/set/level/down-to-php83.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\DowngradeSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([DowngradeSetList::PHP_84]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

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

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DowngradeNewMethodCallWithoutParenthesesRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
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;

class Fixture
{
public function run()
{
new Request()->withMethod('GET')->withUri('/hello-world');
}
}

?>
-----
<?php

declare(strict_types=1);

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

class Fixture
{
public function run()
{
(new Request())->withMethod('GET')->withUri('/hello-world');
}
}

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

declare(strict_types=1);

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

final class SkipAlreadyParentheses
{
public function run()
{
(new Request())->withMethod('GET')->withUri('/hello-world');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeNewMethodCallWithoutParenthesesRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp84\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://wiki.php.net/rfc/new_without_parentheses
*
* @see \Rector\Tests\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector\DowngradeNewMethodCallWithoutParenthesesRectorTest
*/
final class DowngradeNewMethodCallWithoutParenthesesRector extends AbstractRector
{
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add parentheses on new method call without parentheses',
[
new CodeSample(
<<<'CODE_SAMPLE'
new Request()->withMethod('GET')->withUri('/hello-world');
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
(new Request())->withMethod('GET')->withUri('/hello-world');
CODE_SAMPLE
),
]
);
}

/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->var instanceof New_) {
return null;
}

$oldTokens = $this->file->getOldTokens();
if (isset($oldTokens[$node->getStartTokenPos()]) && (string) $oldTokens[$node->getStartTokenPos()] === '(') {
return null;
}

$node->var->setAttribute(AttributeKey::ORIGINAL_NODE, null);
return $node;
}
}
5 changes: 5 additions & 0 deletions src/Set/ValueObject/DowngradeLevelSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
*/
final class DowngradeLevelSetList
{
/**
* @var string
*/
public const DOWN_TO_PHP_83 = __DIR__ . '/../../../config/set/level/down-to-php83.php';

/**
* @var string
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Set/ValueObject/DowngradeSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ final class DowngradeSetList
* @var string
*/
public const PHP_83 = __DIR__ . '/../../../config/set/downgrade-php83.php';

/**
* @var string
*/
public const PHP_84 = __DIR__ . '/../../../config/set/downgrade-php84.php';
}
Loading