Updated Rector to commit 081cf7f7e508428f259b83125883178632e350dc

081cf7f7e5  [Transform] Add RectorConfigBuilderRector  (#5551)
This commit is contained in:
Tomas Votruba 2024-02-04 11:05:31 +00:00
parent 5c6ad07e4c
commit b707660218
4 changed files with 111 additions and 2 deletions

View File

@ -0,0 +1,107 @@
<?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;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'b85682bedf3d32c32bea9f1fef01d983f3ae89cf';
public const PACKAGE_VERSION = '081cf7f7e508428f259b83125883178632e350dc';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-02-04 00:26:46';
public const RELEASE_DATE = '2024-02-04 12:03:25';
/**
* @var int
*/

View File

@ -2262,6 +2262,7 @@ return array(
'Rector\\Transform\\Rector\\Class_\\AddInterfaceByTraitRector' => $baseDir . '/rules/Transform/Rector/Class_/AddInterfaceByTraitRector.php',
'Rector\\Transform\\Rector\\Class_\\MergeInterfacesRector' => $baseDir . '/rules/Transform/Rector/Class_/MergeInterfacesRector.php',
'Rector\\Transform\\Rector\\Class_\\ParentClassToTraitsRector' => $baseDir . '/rules/Transform/Rector/Class_/ParentClassToTraitsRector.php',
'Rector\\Transform\\Rector\\FileWithoutNamespace\\RectorConfigBuilderRector' => $baseDir . '/rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php',
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToConstFetchRector' => $baseDir . '/rules/Transform/Rector/FuncCall/FuncCallToConstFetchRector.php',
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToMethodCallRector' => $baseDir . '/rules/Transform/Rector/FuncCall/FuncCallToMethodCallRector.php',
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToNewRector' => $baseDir . '/rules/Transform/Rector/FuncCall/FuncCallToNewRector.php',

View File

@ -2476,6 +2476,7 @@ class ComposerStaticInitf637847380e2ddf55dcae18dded1d2b3
'Rector\\Transform\\Rector\\Class_\\AddInterfaceByTraitRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/Class_/AddInterfaceByTraitRector.php',
'Rector\\Transform\\Rector\\Class_\\MergeInterfacesRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/Class_/MergeInterfacesRector.php',
'Rector\\Transform\\Rector\\Class_\\ParentClassToTraitsRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/Class_/ParentClassToTraitsRector.php',
'Rector\\Transform\\Rector\\FileWithoutNamespace\\RectorConfigBuilderRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php',
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToConstFetchRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FuncCall/FuncCallToConstFetchRector.php',
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToMethodCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FuncCall/FuncCallToMethodCallRector.php',
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToNewRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FuncCall/FuncCallToNewRector.php',