2017-10-06 11:57:21 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
2019-09-23 15:09:26 +00:00
|
|
|
namespace Rector\Renaming\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;
|
2018-12-10 14:45:35 +00:00
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
2017-10-29 00:31:27 +00:00
|
|
|
use PhpParser\Node\Identifier;
|
2018-12-10 14:45:35 +00:00
|
|
|
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
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2019-09-23 15:03:23 +00:00
|
|
|
* @see \Rector\Renaming\Tests\Rector\MethodCall\RenameMethodRector\RenameMethodRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2019-03-09 12:53:58 +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
|
|
|
|
2017-10-06 12:07:13 +00:00
|
|
|
/**
|
2018-12-11 17:22:54 +00:00
|
|
|
* @param string[][]|mixed[][][] $oldToNewMethodsByClass
|
2017-10-06 12:07:13 +00:00
|
|
|
*/
|
2019-04-26 13:32:24 +00:00
|
|
|
public function __construct(array $oldToNewMethodsByClass = [])
|
2018-10-22 17:17:17 +00:00
|
|
|
{
|
|
|
|
$this->oldToNewMethodsByClass = $oldToNewMethodsByClass;
|
2017-10-06 11:57:21 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 11:51:26 +00:00
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
2018-08-01 13:34:55 +00:00
|
|
|
return new RectorDefinition('Turns method names to new ones.', [
|
2018-08-01 19:52:44 +00:00
|
|
|
new ConfiguredCodeSample(
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2018-10-22 17:39:10 +00:00
|
|
|
$someObject = new SomeExampleClass;
|
2018-04-09 12:48:15 +00:00
|
|
|
$someObject->oldMethod();
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2018-04-08 11:51:26 +00:00
|
|
|
,
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2018-10-22 17:39:10 +00:00
|
|
|
$someObject = new SomeExampleClass;
|
2018-04-09 12:48:15 +00:00
|
|
|
$someObject->newMethod();
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2018-08-01 19:52:44 +00:00
|
|
|
,
|
|
|
|
[
|
2018-11-11 12:22:35 +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) {
|
2019-09-04 12:10:29 +00:00
|
|
|
if (! $this->isMethodStaticCallOrClassMethodObjectType($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) {
|
2019-08-25 10:37:56 +00:00
|
|
|
if (! $this->isName($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-10 14:45:35 +00:00
|
|
|
|
|
|
|
/**
|
2018-12-11 17:22:54 +00:00
|
|
|
* @param MethodCall|StaticCall|ClassMethod $node
|
|
|
|
* @param string|mixed[] $newMethod
|
2018-12-10 14:45:35 +00:00
|
|
|
*/
|
2018-12-11 17:22:54 +00:00
|
|
|
private function process(Node $node, $newMethod): ?Node
|
2018-12-10 14:45:35 +00:00
|
|
|
{
|
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-10 14:45:35 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 17:22:54 +00:00
|
|
|
return null;
|
2018-12-10 14:45:35 +00:00
|
|
|
}
|
2017-10-06 11:57:21 +00:00
|
|
|
}
|