Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Transform] Add RectorConfigBuilderRector (#5551)
* [Transform] Add RectorConfigBuilderRector * skip complex logic * adding withRules() * adding withRules() * [ci-review] Rector Rectify * handled withRules() * [ci-review] Rector Rectify * return node * [ci-review] Rector Rectify * rules * rules * fix phpstan * clean up --------- Co-authored-by: GitHub Action <actions@github.com>
- Loading branch information
1 parent
b85682b
commit 081cf7f
Showing
8 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...outNamespace/RectorConfigBuilderRector/Fixture/skip_already_rector_config_builder.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Transform\Rector\FileWithoutNamespace\RectorConfigBuilderRector; | ||
|
||
return RectorConfig::configure() | ||
->withRules([RectorConfigBuilderRector::class]); |
13 changes: 13 additions & 0 deletions
13
.../Rector/FileWithoutNamespace/RectorConfigBuilderRector/Fixture/skip_complex_logic.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use OndraM\CiDetector\CiDetector; | ||
use Rector\Caching\ValueObject\Storage\MemoryCacheStorage; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
if ((new CiDetector())->isCiDetected()) { | ||
$rectorConfig->cacheClass(MemoryCacheStorage::class); | ||
} | ||
}; |
23 changes: 23 additions & 0 deletions
23
...Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector/Fixture/with_rule.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Transform\Rector\FileWithoutNamespace\RectorConfigBuilderRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(RectorConfigBuilderRector::class); | ||
}; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Transform\Rector\FileWithoutNamespace\RectorConfigBuilderRector; | ||
|
||
return \Rector\Config\RectorConfig::configure()->withRules([RectorConfigBuilderRector::class]); | ||
|
||
?> |
26 changes: 26 additions & 0 deletions
26
...ransform/Rector/FileWithoutNamespace/RectorConfigBuilderRector/Fixture/with_rules.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Transform\Rector\FileWithoutNamespace\RectorConfigBuilderRector; | ||
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(RectorConfigBuilderRector::class); | ||
$rectorConfig->rule(TypedPropertyFromAssignsRector::class); | ||
}; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Transform\Rector\FileWithoutNamespace\RectorConfigBuilderRector; | ||
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector; | ||
|
||
return \Rector\Config\RectorConfig::configure()->withRules([RectorConfigBuilderRector::class, TypedPropertyFromAssignsRector::class]); | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
...m/Rector/FileWithoutNamespace/RectorConfigBuilderRector/RectorConfigBuilderRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Transform\Rector\FileWithoutNamespace\RectorConfigBuilderRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class RectorConfigBuilderRectorTest 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'; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ransform/Rector/FileWithoutNamespace/RectorConfigBuilderRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Transform\Rector\FileWithoutNamespace\RectorConfigBuilderRector; | ||
|
||
return RectorConfig::configure() | ||
->withRules([RectorConfigBuilderRector::class]); |
129 changes: 129 additions & 0 deletions
129
rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Transform\Rector\FileWithoutNamespace; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Expr\ArrayItem; | ||
use PhpParser\Node\Expr\Closure; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\Node\Stmt\Return_; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Exception\ShouldNotHappenException; | ||
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\Transform\Rector\FileWithoutNamespace\RectorConfigBuilderRector\RectorConfigBuilderRectorTest | ||
*/ | ||
final class RectorConfigBuilderRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change RectorConfig to RectorConfigBuilder', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(SomeRector::class); | ||
}; | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
return RectorConfig::configure()->rules([SomeRector::class]); | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [FileWithoutNamespace::class]; | ||
} | ||
|
||
/** | ||
* @param FileWithoutNamespace $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$hasChanged = false; | ||
foreach ($node->stmts as $stmt) { | ||
if (! $stmt instanceof Return_) { | ||
continue; | ||
} | ||
|
||
if (! $stmt->expr instanceof Closure) { | ||
continue; | ||
} | ||
|
||
if (count($stmt->expr->params) !== 1) { | ||
continue; | ||
} | ||
|
||
$param = $stmt->expr->params[0]; | ||
if (! $param->type instanceof FullyQualified) { | ||
continue; | ||
} | ||
|
||
if ($param->type->toString() !== 'Rector\Config\RectorConfig') { | ||
continue; | ||
} | ||
|
||
$stmts = $stmt->expr->stmts; | ||
if ($stmts === []) { | ||
throw new ShouldNotHappenException('RectorConfig must have at least 1 stmts'); | ||
} | ||
|
||
$newExpr = new StaticCall(new FullyQualified('Rector\Config\RectorConfig'), 'configure'); | ||
|
||
$rules = new Array_(); | ||
foreach ($stmts as $rectorConfigStmt) { | ||
// complex stmts should be skipped, eg: with if else | ||
if (! $rectorConfigStmt instanceof Expression) { | ||
return null; | ||
} | ||
|
||
// debugging or variable init? skip | ||
if (! $rectorConfigStmt->expr instanceof MethodCall) { | ||
return null; | ||
} | ||
|
||
// another method call? skip | ||
if (! $this->isObjectType($rectorConfigStmt->expr->var, new ObjectType('Rector\Config\RectorConfig'))) { | ||
return null; | ||
} | ||
|
||
if ($rectorConfigStmt->expr->isFirstClassCallable()) { | ||
return null; | ||
} | ||
|
||
if ($this->isName($rectorConfigStmt->expr->name, 'rule')) { | ||
$rules->items[] = new ArrayItem($rectorConfigStmt->expr->getArgs()[0]->value); | ||
} else { | ||
// implementing method by method | ||
return null; | ||
} | ||
} | ||
|
||
if ($rules->items !== []) { | ||
$stmt->expr = $this->nodeFactory->createMethodCall($newExpr, 'withRules', [$rules]); | ||
$hasChanged = true; | ||
} | ||
} | ||
|
||
if ($hasChanged) { | ||
return $node; | ||
} | ||
|
||
return null; | ||
} | ||
} |