Skip to content

Commit

Permalink
[Transform] Add RectorConfigBuilderRector (#5551)
Browse files Browse the repository at this point in the history
* [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
samsonasik and actions-user committed Feb 4, 2024
1 parent b85682b commit 081cf7f
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 0 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Expand Up @@ -92,6 +92,7 @@ parameters:
- rules/CodeQuality/Rector/Foreach_/SimplifyForeachToCoalescingRector.php
- rules/CodeQuality/Rector/Foreach_/ForeachToInArrayRector.php
- src/Configuration/RectorConfigBuilder.php
- rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php

# is nested expr
-
Expand Down
@@ -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]);
@@ -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);
}
};
@@ -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]);

?>
@@ -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]);

?>
@@ -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';
}
}
@@ -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]);
@@ -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;
}
}

0 comments on commit 081cf7f

Please sign in to comment.