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;
|
|
|
|
use PhpParser\Node\Name;
|
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;
|
|
|
|
use Rector\Core\RectorDefinition\ConfiguredCodeSample;
|
|
|
|
use Rector\Core\RectorDefinition\RectorDefinition;
|
2018-08-10 19:06:40 +00:00
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2020-08-18 15:57:30 +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
|
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-07-29 13:55:33 +00:00
|
|
|
public const OLD_TO_NEW_METHODS_BY_CLASSES = '$oldToNewMethodByClasses';
|
2018-08-10 19:06:40 +00:00
|
|
|
|
|
|
|
/**
|
2020-07-29 13:55:33 +00:00
|
|
|
* @var string[][]|string[][][]
|
2018-08-10 19:06:40 +00:00
|
|
|
*/
|
2020-07-29 13:55:33 +00:00
|
|
|
private $oldToNewMethodByClasses = [];
|
2018-08-10 19:06:40 +00:00
|
|
|
|
|
|
|
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();',
|
|
|
|
[
|
2020-07-30 15:46:36 +00:00
|
|
|
self::OLD_TO_NEW_METHODS_BY_CLASSES => [
|
|
|
|
'SomeClass' => [
|
|
|
|
'oldMethod' => ['AnotherExampleClass', 'newStaticMethod'],
|
|
|
|
],
|
2018-10-22 18:12:32 +00:00
|
|
|
],
|
|
|
|
]
|
|
|
|
),
|
2018-08-10 19:06:40 +00:00
|
|
|
new ConfiguredCodeSample(
|
|
|
|
'SomeClass::oldStaticMethod();',
|
|
|
|
'SomeClass::newStaticMethod();',
|
|
|
|
[
|
2020-07-30 15:46:36 +00:00
|
|
|
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-10 19:06:40 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-08-14 22:12:41 +00:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
{
|
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-08-10 19:06:40 +00:00
|
|
|
|
2018-10-22 18:12:32 +00:00
|
|
|
foreach ($oldToNewMethods as $oldMethod => $newMethod) {
|
2020-02-21 14:54:51 +00:00
|
|
|
if (! $this->isName($node->name, $oldMethod)) {
|
2018-10-22 18:12:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-08-10 19:06:40 +00:00
|
|
|
|
2018-10-22 18:12:32 +00:00
|
|
|
return $this->rename($node, $newMethod);
|
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
|
|
|
}
|
|
|
|
|
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-08-10 19:06:40 +00:00
|
|
|
/**
|
2018-10-22 18:12:32 +00:00
|
|
|
* @param string|string[] $newMethod
|
2018-08-10 19:06:40 +00:00
|
|
|
*/
|
2019-02-22 17:25:31 +00:00
|
|
|
private function rename(StaticCall $staticCall, $newMethod): StaticCall
|
2018-08-10 19:06:40 +00:00
|
|
|
{
|
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);
|
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
|
|
|
}
|
|
|
|
}
|