rector/rules/renaming/src/Rector/StaticCall/RenameStaticMethodRector.php

110 lines
3.1 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
namespace Rector\Renaming\Rector\StaticCall;
use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
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;
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Renaming\Tests\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
{
/**
2020-07-29 13:55:33 +00:00
* @var string
*/
2020-07-29 13:55:33 +00:00
public const OLD_TO_NEW_METHODS_BY_CLASSES = '$oldToNewMethodByClasses';
/**
2020-07-29 13:55:33 +00:00
* @var string[][]|string[][][]
*/
2020-07-29 13:55:33 +00:00
private $oldToNewMethodByClasses = [];
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Turns method names to new ones.', [
2018-10-22 18:12:32 +00:00
new ConfiguredCodeSample(
'SomeClass::oldStaticMethod();',
'AnotherExampleClass::newStaticMethod();',
[
self::OLD_TO_NEW_METHODS_BY_CLASSES => [
'SomeClass' => [
'oldMethod' => ['AnotherExampleClass', 'newStaticMethod'],
],
2018-10-22 18:12:32 +00:00
],
]
),
new ConfiguredCodeSample(
'SomeClass::oldStaticMethod();',
'SomeClass::newStaticMethod();',
[
self::OLD_TO_NEW_METHODS_BY_CLASSES => [
2018-10-15 02:37:09 +00:00
'SomeClass' => [
2018-10-22 18:12:32 +00:00
'oldMethod' => 'newStaticMethod',
],
],
]
),
]);
}
2018-08-14 22:12:41 +00:00
/**
* @return string[]
*/
public function getNodeTypes(): array
{
2018-10-22 18:12:32 +00:00
return [StaticCall::class];
}
/**
2018-10-22 18:12:32 +00:00
* @param StaticCall $node
*/
public function refactor(Node $node): ?Node
{
2018-10-22 18:12:32 +00:00
foreach ($this->oldToNewMethodByClasses as $type => $oldToNewMethods) {
2019-09-04 12:10:29 +00:00
if (! $this->isObjectType($node->class, $type)) {
2018-10-22 18:12:32 +00:00
continue;
}
2018-10-22 18:12:32 +00:00
foreach ($oldToNewMethods as $oldMethod => $newMethod) {
if (! $this->isName($node->name, $oldMethod)) {
2018-10-22 18:12:32 +00:00
continue;
}
2018-10-22 18:12:32 +00:00
return $this->rename($node, $newMethod);
}
}
2018-10-22 18:12:32 +00:00
return null;
}
2020-07-29 13:55:33 +00:00
public function configure(array $configuration): void
{
$this->oldToNewMethodByClasses = $configuration[self::OLD_TO_NEW_METHODS_BY_CLASSES] ?? [];
}
/**
2018-10-22 18:12:32 +00:00
* @param string|string[] $newMethod
*/
2019-02-22 17:25:31 +00:00
private function rename(StaticCall $staticCall, $newMethod): StaticCall
{
2018-10-22 18:12:32 +00:00
if (is_array($newMethod)) {
[$newClass, $newMethod] = $newMethod;
2019-02-22 17:25:31 +00:00
$staticCall->class = new Name($newClass);
$staticCall->name = new Identifier($newMethod);
2018-10-22 18:12:32 +00:00
} else {
2019-02-22 17:25:31 +00:00
$staticCall->name = new Identifier($newMethod);
}
2019-02-22 17:25:31 +00:00
return $staticCall;
}
}