rector/src/Rector/MethodCall/RenameMethodRector.php

113 lines
2.9 KiB
PHP
Raw Normal View History

2017-10-06 11:57:21 +00:00
<?php declare(strict_types=1);
namespace Rector\Rector\MethodCall;
2017-10-06 11:57:21 +00:00
2018-12-11 17:22:54 +00:00
use PhpParser\BuilderHelpers;
2017-10-06 11:57:21 +00:00
use PhpParser\Node;
2018-12-11 17:22:54 +00:00
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
2017-10-06 11:57:21 +00:00
use Rector\Rector\AbstractRector;
2018-08-01 19:52:44 +00:00
use Rector\RectorDefinition\ConfiguredCodeSample;
2018-04-08 11:51:26 +00:00
use Rector\RectorDefinition\RectorDefinition;
2017-10-06 11:57:21 +00:00
final class RenameMethodRector extends AbstractRector
2017-10-06 11:57:21 +00:00
{
/**
* class => [
* oldMethod => newMethod
* ]
*
2018-12-11 17:22:54 +00:00
* @var string[][]|mixed[][][]
2017-10-06 11:57:21 +00:00
*/
2018-10-22 17:17:17 +00:00
private $oldToNewMethodsByClass = [];
2017-10-06 11:57:21 +00:00
/**
2018-12-11 17:22:54 +00:00
* @param string[][]|mixed[][][] $oldToNewMethodsByClass
*/
2018-10-22 17:17:17 +00:00
public function __construct(array $oldToNewMethodsByClass)
{
$this->oldToNewMethodsByClass = $oldToNewMethodsByClass;
2017-10-06 11:57:21 +00:00
}
2018-04-08 11:51:26 +00:00
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Turns method names to new ones.', [
2018-08-01 19:52:44 +00:00
new ConfiguredCodeSample(
2018-04-08 11:51:26 +00:00
<<<'CODE_SAMPLE'
2018-10-22 17:39:10 +00:00
$someObject = new SomeExampleClass;
2018-04-09 12:48:15 +00:00
$someObject->oldMethod();
2018-04-08 11:51:26 +00:00
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
2018-10-22 17:39:10 +00:00
$someObject = new SomeExampleClass;
2018-04-09 12:48:15 +00:00
$someObject->newMethod();
2018-04-08 11:51:26 +00:00
CODE_SAMPLE
2018-08-01 19:52:44 +00:00
,
[
'SomeExampleClass' => [
'oldMethod' => 'newMethod',
2018-08-01 19:52:44 +00:00
],
]
),
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 11:57:21 +00:00
{
2018-12-11 17:22:54 +00:00
return [MethodCall::class, StaticCall::class, ClassMethod::class];
2017-10-06 11:57:21 +00:00
}
/**
2018-12-11 17:22:54 +00:00
* @param MethodCall|StaticCall|ClassMethod $node
2017-10-06 11:57:21 +00:00
*/
public function refactor(Node $node): ?Node
{
2018-10-22 17:17:17 +00:00
foreach ($this->oldToNewMethodsByClass as $type => $oldToNewMethods) {
2018-12-11 17:22:54 +00:00
if (! $this->isType($node, $type)) {
2018-10-22 17:17:17 +00:00
continue;
}
2018-08-14 22:12:41 +00:00
2018-10-22 17:17:17 +00:00
foreach ($oldToNewMethods as $oldMethod => $newMethod) {
2018-10-22 17:36:12 +00:00
if (! $this->isNameInsensitive($node, $oldMethod)) {
2018-10-22 17:17:17 +00:00
continue;
}
2018-08-14 22:12:41 +00:00
2019-01-14 18:21:23 +00:00
$newNode = $this->process($node, $newMethod);
if ($newNode !== null) {
return $newNode;
2018-12-11 17:22:54 +00:00
}
2018-10-22 17:17:17 +00:00
}
2018-08-14 22:12:41 +00:00
}
2018-12-11 17:22:54 +00:00
return null;
2018-08-14 22:12:41 +00:00
}
/**
2018-12-11 17:22:54 +00:00
* @param MethodCall|StaticCall|ClassMethod $node
* @param string|mixed[] $newMethod
*/
2018-12-11 17:22:54 +00:00
private function process(Node $node, $newMethod): ?Node
{
2018-12-11 17:22:54 +00:00
if (is_string($newMethod)) {
$node->name = new Identifier($newMethod);
return $node;
}
// special case for array dim fetch
if (! $node instanceof ClassMethod) {
$node->name = new Identifier($newMethod['name']);
return new ArrayDimFetch($node, BuilderHelpers::normalizeValue($newMethod['array_key']));
}
2018-12-11 17:22:54 +00:00
return null;
}
2017-10-06 11:57:21 +00:00
}