2017-10-06 11:57:21 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\Rector\Dynamic;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
2017-10-29 00:31:27 +00:00
|
|
|
use PhpParser\Node\Identifier;
|
2017-10-06 11:57:21 +00:00
|
|
|
use PhpParser\Node\Name;
|
2017-10-29 00:31:27 +00:00
|
|
|
use Rector\Node\Attribute;
|
2017-10-20 16:16:52 +00:00
|
|
|
use Rector\NodeAnalyzer\MethodCallAnalyzer;
|
2017-10-29 00:31:27 +00:00
|
|
|
use Rector\NodeAnalyzer\MethodNameAnalyzer;
|
2017-10-20 16:23:59 +00:00
|
|
|
use Rector\NodeAnalyzer\StaticMethodCallAnalyzer;
|
2017-10-06 11:57:21 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
|
|
|
|
final class MethodNameReplacerRector extends AbstractRector
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* class => [
|
|
|
|
* oldMethod => newMethod
|
|
|
|
* ]
|
|
|
|
*
|
2017-10-20 15:54:20 +00:00
|
|
|
* or (typically for static calls):
|
|
|
|
*
|
|
|
|
* class => [
|
|
|
|
* oldMethod => [
|
|
|
|
* newClass, newMethod
|
|
|
|
* ]
|
|
|
|
* ]
|
|
|
|
*
|
2017-10-06 11:57:21 +00:00
|
|
|
* @var string[][]
|
|
|
|
*/
|
|
|
|
private $perClassOldToNewMethods = [];
|
|
|
|
|
2017-10-06 12:07:13 +00:00
|
|
|
/**
|
2017-10-25 15:56:06 +00:00
|
|
|
* @var string[]
|
2017-10-06 12:07:13 +00:00
|
|
|
*/
|
2017-10-25 15:56:06 +00:00
|
|
|
private $activeTypes = [];
|
2017-10-06 12:07:13 +00:00
|
|
|
|
2017-10-20 16:16:52 +00:00
|
|
|
/**
|
|
|
|
* @var MethodCallAnalyzer
|
|
|
|
*/
|
|
|
|
private $methodCallAnalyzer;
|
2017-10-20 16:25:47 +00:00
|
|
|
|
2017-10-20 16:23:59 +00:00
|
|
|
/**
|
|
|
|
* @var StaticMethodCallAnalyzer
|
|
|
|
*/
|
|
|
|
private $staticMethodCallAnalyzer;
|
2017-10-20 16:16:52 +00:00
|
|
|
|
2017-10-29 00:31:27 +00:00
|
|
|
/**
|
|
|
|
* @var MethodNameAnalyzer
|
|
|
|
*/
|
|
|
|
private $methodNameAnalyzer;
|
|
|
|
|
2017-10-06 11:57:21 +00:00
|
|
|
/**
|
|
|
|
* @param string[][]
|
|
|
|
*/
|
2017-10-20 16:23:59 +00:00
|
|
|
public function __construct(
|
|
|
|
array $perClassOldToNewMethods,
|
|
|
|
MethodCallAnalyzer $methodCallAnalyzer,
|
2017-10-29 00:31:27 +00:00
|
|
|
StaticMethodCallAnalyzer $staticMethodCallAnalyzer,
|
|
|
|
MethodNameAnalyzer $methodNameAnalyzer
|
2017-10-20 16:23:59 +00:00
|
|
|
) {
|
2017-10-06 11:57:21 +00:00
|
|
|
$this->perClassOldToNewMethods = $perClassOldToNewMethods;
|
2017-10-20 16:16:52 +00:00
|
|
|
$this->methodCallAnalyzer = $methodCallAnalyzer;
|
2017-10-20 16:23:59 +00:00
|
|
|
$this->staticMethodCallAnalyzer = $staticMethodCallAnalyzer;
|
2017-10-29 00:31:27 +00:00
|
|
|
$this->methodNameAnalyzer = $methodNameAnalyzer;
|
2017-10-06 11:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isCandidate(Node $node): bool
|
|
|
|
{
|
2017-10-25 15:56:06 +00:00
|
|
|
$this->activeTypes = null;
|
2017-10-06 11:57:21 +00:00
|
|
|
|
2017-10-25 15:56:06 +00:00
|
|
|
$matchedTypes = $this->methodCallAnalyzer->matchTypes($node, $this->getClasses());
|
|
|
|
if ($matchedTypes) {
|
|
|
|
$this->activeTypes = $matchedTypes;
|
2017-10-20 16:16:52 +00:00
|
|
|
|
2017-10-06 11:57:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-25 15:56:06 +00:00
|
|
|
$matchedTypes = $this->staticMethodCallAnalyzer->matchTypes($node, $this->getClasses());
|
|
|
|
if ($matchedTypes) {
|
|
|
|
$this->activeTypes = $matchedTypes;
|
2017-10-20 16:16:52 +00:00
|
|
|
|
2017-10-06 11:57:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-29 00:31:27 +00:00
|
|
|
if ($this->isMethodName($node, $this->getClasses())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-06 11:57:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-29 00:31:27 +00:00
|
|
|
* @param Identifier|StaticCall|MethodCall $node
|
2017-10-06 11:57:21 +00:00
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
2017-10-25 15:56:06 +00:00
|
|
|
$oldToNewMethods = $this->matchOldToNewMethos();
|
2017-10-20 15:57:24 +00:00
|
|
|
|
2017-10-29 00:31:27 +00:00
|
|
|
if ($node instanceof Identifier) {
|
|
|
|
return $this->resolveIdentifier($node);
|
|
|
|
}
|
|
|
|
|
|
|
|
$methodName = $node->name->name;
|
2017-10-20 16:59:24 +00:00
|
|
|
if (! isset($oldToNewMethods[$methodName])) {
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
2017-10-20 15:54:20 +00:00
|
|
|
if ($this->isClassRename($oldToNewMethods)) {
|
2017-10-29 00:31:27 +00:00
|
|
|
return $this->resolveClassRename($node, $oldToNewMethods, $methodName);
|
2017-10-20 16:59:24 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 00:31:27 +00:00
|
|
|
$node->name->name = $oldToNewMethods[$methodName];
|
2017-10-06 11:57:21 +00:00
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
private function getClasses(): array
|
|
|
|
{
|
|
|
|
return array_keys($this->perClassOldToNewMethods);
|
|
|
|
}
|
2017-10-20 15:54:20 +00:00
|
|
|
|
2017-10-20 16:16:52 +00:00
|
|
|
/**
|
|
|
|
* @param mixed[] $oldToNewMethods
|
|
|
|
*/
|
2017-10-20 15:54:20 +00:00
|
|
|
private function isClassRename(array $oldToNewMethods): bool
|
|
|
|
{
|
|
|
|
$firstMethodConfiguration = current($oldToNewMethods);
|
|
|
|
|
|
|
|
return is_array($firstMethodConfiguration);
|
|
|
|
}
|
2017-10-25 15:56:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
private function matchOldToNewMethos(): array
|
|
|
|
{
|
|
|
|
foreach ($this->activeTypes as $activeType) {
|
|
|
|
if ($this->perClassOldToNewMethods[$activeType]) {
|
|
|
|
return $this->perClassOldToNewMethods[$activeType];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
2017-10-29 00:31:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $types
|
|
|
|
*/
|
|
|
|
private function isMethodName(Node $node, array $types): bool
|
|
|
|
{
|
|
|
|
if (! $this->methodNameAnalyzer->isOverrideOfTypes($node, $types)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
|
|
|
|
|
|
|
|
/** @var Identifier $node */
|
|
|
|
if (! isset($this->perClassOldToNewMethods[$parentClassName][$node->toString()])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->activeTypes = [$parentClassName];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function resolveIdentifier(Identifier $node): Node
|
|
|
|
{
|
|
|
|
$oldToNewMethods = $this->matchOldToNewMethos();
|
|
|
|
|
|
|
|
$methodName = $node->name;
|
|
|
|
if (! isset($oldToNewMethods[$methodName])) {
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
$node->name = $oldToNewMethods[$methodName];
|
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param StaticCall|MethodCall $node
|
|
|
|
* @param string[] $oldToNewMethods
|
|
|
|
*/
|
|
|
|
private function resolveClassRename(Node $node, array $oldToNewMethods, string $methodName): Node
|
|
|
|
{
|
|
|
|
[$newClass, $newMethod] = $oldToNewMethods[$methodName];
|
|
|
|
|
|
|
|
$node->class = new Name($newClass);
|
|
|
|
$node->name->name = $newMethod;
|
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
2017-10-06 11:57:21 +00:00
|
|
|
}
|