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-12-26 21:42:49 +00:00
|
|
|
use Rector\NodeChanger\IdentifierRenamer;
|
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-11-01 11:46:45 +00:00
|
|
|
* @todo consider splitting to static call replacer or class rename,
|
|
|
|
* this api can lead users to bugs (already did)
|
|
|
|
*
|
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-12-25 19:14:18 +00:00
|
|
|
/**
|
2017-12-26 21:42:49 +00:00
|
|
|
* @var IdentifierRenamer
|
2017-12-25 19:14:18 +00:00
|
|
|
*/
|
2017-12-26 21:42:49 +00:00
|
|
|
private $identifierRenamer;
|
2017-12-26 18:31:39 +00:00
|
|
|
|
2017-10-06 11:57:21 +00:00
|
|
|
/**
|
2017-12-09 20:58:33 +00:00
|
|
|
* @param string[][] $perClassOldToNewMethods
|
2017-10-06 11:57:21 +00:00
|
|
|
*/
|
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,
|
2017-12-25 19:14:18 +00:00
|
|
|
MethodNameAnalyzer $methodNameAnalyzer,
|
2017-12-26 21:42:49 +00:00
|
|
|
IdentifierRenamer $identifierRenamer
|
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-12-26 21:42:49 +00:00
|
|
|
$this->identifierRenamer = $identifierRenamer;
|
2017-10-06 11:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isCandidate(Node $node): bool
|
|
|
|
{
|
2017-11-06 12:58:51 +00:00
|
|
|
$this->activeTypes = [];
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-09 03:25:15 +00:00
|
|
|
return $this->isMethodName($node, $this->getClasses());
|
2017-10-06 11:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-29 00:31:27 +00:00
|
|
|
if ($node instanceof Identifier) {
|
|
|
|
return $this->resolveIdentifier($node);
|
|
|
|
}
|
|
|
|
|
2017-12-25 19:14:18 +00:00
|
|
|
$oldToNewMethods = $this->matchOldToNewMethods();
|
|
|
|
|
2017-12-10 00:13:25 +00:00
|
|
|
/** @var Identifier $identifierNode */
|
|
|
|
$identifierNode = $node->name;
|
|
|
|
|
|
|
|
$methodName = $identifierNode->toString();
|
2017-10-20 16:59:24 +00:00
|
|
|
if (! isset($oldToNewMethods[$methodName])) {
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
2017-12-10 00:13:25 +00:00
|
|
|
if ($node instanceof StaticCall && $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-12-26 21:42:49 +00:00
|
|
|
$this->identifierRenamer->renameNode($node, $oldToNewMethods[$methodName]);
|
2017-12-25 19:37:09 +00:00
|
|
|
|
|
|
|
return $node;
|
2017-10-06 11:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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[]
|
|
|
|
*/
|
2017-11-14 20:43:57 +00:00
|
|
|
private function matchOldToNewMethods(): array
|
2017-10-25 15:56:06 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2018-02-20 23:37:57 +00:00
|
|
|
// already covered by previous methods
|
|
|
|
if (! $node instanceof Identifier) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);
|
|
|
|
if ($parentNode instanceof MethodCall || $parentNode instanceof StaticCall) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-29 00:31:27 +00:00
|
|
|
if (! $this->methodNameAnalyzer->isOverrideOfTypes($node, $types)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-06 11:48:43 +00:00
|
|
|
/** @var Identifier $node */
|
2017-10-29 00:31:27 +00:00
|
|
|
$parentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
|
|
|
|
|
|
|
|
/** @var Identifier $node */
|
2018-01-18 19:34:52 +00:00
|
|
|
if (! isset($this->perClassOldToNewMethods[$parentClassName][$node->name])) {
|
2017-10-29 00:31:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->activeTypes = [$parentClassName];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function resolveIdentifier(Identifier $node): Node
|
|
|
|
{
|
2017-11-14 20:43:57 +00:00
|
|
|
$oldToNewMethods = $this->matchOldToNewMethods();
|
2017-10-29 00:31:27 +00:00
|
|
|
|
|
|
|
$methodName = $node->name;
|
|
|
|
if (! isset($oldToNewMethods[$methodName])) {
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
$node->name = $oldToNewMethods[$methodName];
|
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $oldToNewMethods
|
|
|
|
*/
|
2017-12-10 00:13:25 +00:00
|
|
|
private function resolveClassRename(StaticCall $staticCallNode, array $oldToNewMethods, string $methodName): Node
|
2017-10-29 00:31:27 +00:00
|
|
|
{
|
|
|
|
[$newClass, $newMethod] = $oldToNewMethods[$methodName];
|
|
|
|
|
2017-12-10 00:13:25 +00:00
|
|
|
$staticCallNode->class = new Name($newClass);
|
2017-12-26 21:42:49 +00:00
|
|
|
$this->identifierRenamer->renameNode($staticCallNode, $newMethod);
|
2017-10-29 00:31:27 +00:00
|
|
|
|
2017-12-10 00:13:25 +00:00
|
|
|
return $staticCallNode;
|
2017-10-29 00:31:27 +00:00
|
|
|
}
|
2017-10-06 11:57:21 +00:00
|
|
|
}
|