2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-08-10 19:06:40 +00:00
|
|
|
|
2020-08-18 15:57:30 +00:00
|
|
|
namespace Rector\Renaming\Rector\StaticCall;
|
2018-08-10 19:06:40 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
|
|
|
use PhpParser\Node\Identifier;
|
2020-09-25 12:52:39 +00:00
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
2020-07-29 13:55:33 +00:00
|
|
|
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-09-12 21:19:08 +00:00
|
|
|
use Rector\Renaming\ValueObject\RenameStaticMethod;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2021-05-02 10:46:55 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2018-08-10 19:06:40 +00:00
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Renaming\Rector\StaticCall\RenameStaticMethodRector\RenameStaticMethodRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2020-07-29 13:55:33 +00:00
|
|
|
final class RenameStaticMethodRector extends AbstractRector implements ConfigurableRectorInterface
|
2018-08-10 19:06:40 +00:00
|
|
|
{
|
|
|
|
/**
|
2020-07-29 13:55:33 +00:00
|
|
|
* @var string
|
2018-08-10 19:06:40 +00:00
|
|
|
*/
|
2020-08-23 09:39:09 +00:00
|
|
|
public const OLD_TO_NEW_METHODS_BY_CLASSES = 'old_to_new_method_by_classes';
|
2018-08-10 19:06:40 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-23 09:39:09 +00:00
|
|
|
* @var string
|
2018-08-10 19:06:40 +00:00
|
|
|
*/
|
2020-08-23 09:39:09 +00:00
|
|
|
private const SOME_CLASS = 'SomeClass';
|
|
|
|
|
|
|
|
/**
|
2020-09-12 21:19:08 +00:00
|
|
|
* @var RenameStaticMethod[]
|
2020-08-23 09:39:09 +00:00
|
|
|
*/
|
|
|
|
private $staticMethodRenames = [];
|
2018-08-10 19:06:40 +00:00
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2018-08-10 19:06:40 +00:00
|
|
|
{
|
2020-12-12 12:08:34 +00:00
|
|
|
$renameClassConfiguration = [
|
|
|
|
self::OLD_TO_NEW_METHODS_BY_CLASSES => [
|
|
|
|
new RenameStaticMethod(self::SOME_CLASS, 'oldMethod', 'AnotherExampleClass', 'newStaticMethod'),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$renameMethodConfiguration = [
|
|
|
|
self::OLD_TO_NEW_METHODS_BY_CLASSES => [
|
|
|
|
new RenameStaticMethod(self::SOME_CLASS, 'oldMethod', self::SOME_CLASS, 'newStaticMethod'),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition('Turns method names to new ones.', [
|
2018-10-22 18:12:32 +00:00
|
|
|
new ConfiguredCodeSample(
|
|
|
|
'SomeClass::oldStaticMethod();',
|
|
|
|
'AnotherExampleClass::newStaticMethod();',
|
2020-12-12 12:08:34 +00:00
|
|
|
$renameClassConfiguration
|
2018-10-22 18:12:32 +00:00
|
|
|
),
|
2018-08-10 19:06:40 +00:00
|
|
|
new ConfiguredCodeSample(
|
|
|
|
'SomeClass::oldStaticMethod();',
|
|
|
|
'SomeClass::newStaticMethod();',
|
2020-12-12 12:08:34 +00:00
|
|
|
$renameMethodConfiguration
|
2018-08-10 19:06:40 +00:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-08-14 22:12:41 +00:00
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-08-14 22:12:41 +00:00
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
2018-08-10 19:06:40 +00:00
|
|
|
{
|
2018-10-22 18:12:32 +00:00
|
|
|
return [StaticCall::class];
|
2018-08-10 19:06:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-22 18:12:32 +00:00
|
|
|
* @param StaticCall $node
|
2018-08-10 19:06:40 +00:00
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
2020-08-23 09:39:09 +00:00
|
|
|
foreach ($this->staticMethodRenames as $staticMethodRename) {
|
2021-02-27 02:13:22 +00:00
|
|
|
if (! $this->isObjectType($node->class, $staticMethodRename->getOldObjectType())) {
|
2018-10-22 18:12:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-08-10 19:06:40 +00:00
|
|
|
|
2020-08-23 09:39:09 +00:00
|
|
|
if (! $this->isName($node->name, $staticMethodRename->getOldMethod())) {
|
|
|
|
continue;
|
2018-08-10 19:06:40 +00:00
|
|
|
}
|
2020-08-23 09:39:09 +00:00
|
|
|
|
|
|
|
return $this->rename($node, $staticMethodRename);
|
2018-08-10 19:06:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-22 18:12:32 +00:00
|
|
|
return null;
|
2018-08-10 19:06:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 10:46:55 +00:00
|
|
|
/**
|
|
|
|
* @param array<string, RenameStaticMethod[]> $configuration
|
|
|
|
*/
|
2020-07-29 13:55:33 +00:00
|
|
|
public function configure(array $configuration): void
|
|
|
|
{
|
2021-05-02 10:46:55 +00:00
|
|
|
$oldToNewMethodsByClasses = $configuration[self::OLD_TO_NEW_METHODS_BY_CLASSES];
|
|
|
|
Assert::allIsInstanceOf($oldToNewMethodsByClasses, RenameStaticMethod::class);
|
|
|
|
$this->staticMethodRenames = $oldToNewMethodsByClasses;
|
2020-07-29 13:55:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 21:19:08 +00:00
|
|
|
private function rename(StaticCall $staticCall, RenameStaticMethod $renameStaticMethod): StaticCall
|
2018-08-10 19:06:40 +00:00
|
|
|
{
|
2020-09-12 21:19:08 +00:00
|
|
|
$staticCall->name = new Identifier($renameStaticMethod->getNewMethod());
|
2020-08-23 09:39:09 +00:00
|
|
|
|
2020-09-12 21:19:08 +00:00
|
|
|
if ($renameStaticMethod->hasClassChanged()) {
|
2020-09-25 12:52:39 +00:00
|
|
|
$staticCall->class = new FullyQualified($renameStaticMethod->getNewClass());
|
2018-08-10 19:06:40 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 17:25:31 +00:00
|
|
|
return $staticCall;
|
2018-08-10 19:06:40 +00:00
|
|
|
}
|
|
|
|
}
|