rector/rules/renaming/src/Rector/Name/RenameClassRector.php

122 lines
2.9 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
2017-10-05 10:40:19 +00:00
namespace Rector\Renaming\Rector\Name;
2017-10-06 06:34:58 +00:00
use PhpParser\Node;
2019-03-15 20:12:45 +00:00
use PhpParser\Node\FunctionLike;
2017-10-06 06:34:58 +00:00
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Namespace_;
2019-03-15 19:37:48 +00:00
use PhpParser\Node\Stmt\Property;
use Rector\Core\Configuration\ChangeConfiguration;
2020-07-29 13:55:33 +00:00
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\ConfiguredCodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
2020-05-11 15:38:57 +00:00
use Rector\Renaming\NodeManipulator\ClassRenamer;
2017-10-05 10:40:19 +00:00
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Renaming\Tests\Rector\Name\RenameClassRector\RenameClassRectorTest
2019-09-03 09:11:45 +00:00
*/
2020-07-29 13:55:33 +00:00
final class RenameClassRector extends AbstractRector implements ConfigurableRectorInterface
2017-10-05 10:40:19 +00:00
{
2020-07-29 13:55:33 +00:00
/**
* @var string
*/
public const OLD_TO_NEW_CLASSES = 'old_to_new_classes';
2020-07-29 13:55:33 +00:00
2017-10-05 10:40:19 +00:00
/**
* @var string[]
*/
private $oldToNewClasses = [];
/**
2020-05-11 15:38:57 +00:00
* @var ClassRenamer
2019-08-30 18:21:21 +00:00
*/
2020-05-11 15:38:57 +00:00
private $classRenamer;
2019-08-30 18:21:21 +00:00
2017-10-05 10:40:19 +00:00
/**
2020-07-29 13:55:33 +00:00
* @var ChangeConfiguration
2017-10-05 10:40:19 +00:00
*/
2020-07-29 13:55:33 +00:00
private $changeConfiguration;
2020-02-06 19:12:48 +00:00
2020-07-29 13:55:33 +00:00
public function __construct(ChangeConfiguration $changeConfiguration, ClassRenamer $classRenamer)
{
$this->classRenamer = $classRenamer;
$this->changeConfiguration = $changeConfiguration;
2017-10-05 10:40:19 +00:00
}
2018-04-08 11:51:26 +00:00
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Replaces defined classes by new ones.', [
new ConfiguredCodeSample(
2019-09-18 06:14:35 +00:00
<<<'PHP'
namespace App;
2018-10-17 10:51:28 +00:00
use SomeOldClass;
2019-03-15 19:36:58 +00:00
function someFunction(SomeOldClass $someOldClass): SomeOldClass
2018-10-17 10:51:28 +00:00
{
if ($someOldClass instanceof SomeOldClass) {
2019-12-27 17:26:20 +00:00
return new SomeOldClass;
2018-10-17 10:51:28 +00:00
}
}
2019-09-18 06:14:35 +00:00
PHP
2018-10-17 10:51:28 +00:00
,
2019-09-18 06:14:35 +00:00
<<<'PHP'
namespace App;
2018-10-17 10:51:28 +00:00
use SomeNewClass;
2019-03-15 19:36:58 +00:00
function someFunction(SomeNewClass $someOldClass): SomeNewClass
2018-10-17 10:51:28 +00:00
{
if ($someOldClass instanceof SomeNewClass) {
return new SomeNewClass;
}
}
2019-09-18 06:14:35 +00:00
PHP
2018-10-17 10:51:28 +00:00
,
[
2020-07-29 23:39:41 +00:00
self::OLD_TO_NEW_CLASSES => [
2019-08-30 18:21:21 +00:00
'App\SomeOldClass' => 'App\SomeNewClass',
],
]
),
2018-04-08 11:51:26 +00:00
]);
}
2018-08-14 22:12:41 +00:00
/**
* @return string[]
*/
public function getNodeTypes(): array
2017-10-06 06:34:58 +00:00
{
return [
Name::class,
Property::class,
FunctionLike::class,
Expression::class,
ClassLike::class,
Namespace_::class,
];
2017-10-06 06:34:58 +00:00
}
2017-10-05 10:40:19 +00:00
/**
* @param Name|FunctionLike|Property $node
2017-10-05 10:40:19 +00:00
*/
2018-10-22 10:43:10 +00:00
public function refactor(Node $node): ?Node
2017-10-06 06:34:58 +00:00
{
2020-05-11 15:38:57 +00:00
return $this->classRenamer->renameNode($node, $this->oldToNewClasses);
}
2020-07-29 13:55:33 +00:00
public function configure(array $configuration): void
{
$this->oldToNewClasses = $configuration[self::OLD_TO_NEW_CLASSES] ?? [];
if ($this->oldToNewClasses !== []) {
$this->changeConfiguration->setOldToNewClasses($this->oldToNewClasses);
}
}
2017-10-05 10:40:19 +00:00
}