rector/src/Rector/Dynamic/MethodNameReplacerRector.php

201 lines
4.8 KiB
PHP
Raw Normal View History

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;
use PhpParser\Node\Identifier;
2017-10-06 11:57:21 +00:00
use PhpParser\Node\Name;
use Rector\Node\Attribute;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
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
* ]
*
* or (typically for static calls):
*
* class => [
* oldMethod => [
* newClass, newMethod
* ]
* ]
*
2017-10-06 11:57:21 +00:00
* @var string[][]
*/
private $perClassOldToNewMethods = [];
/**
* @var string[]
*/
private $activeTypes = [];
/**
* @var MethodCallAnalyzer
*/
private $methodCallAnalyzer;
2017-10-20 16:25:47 +00:00
2017-10-20 16:23:59 +00:00
/**
* @var StaticMethodCallAnalyzer
*/
private $staticMethodCallAnalyzer;
/**
* @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,
StaticMethodCallAnalyzer $staticMethodCallAnalyzer,
MethodNameAnalyzer $methodNameAnalyzer
2017-10-20 16:23:59 +00:00
) {
2017-10-06 11:57:21 +00:00
$this->perClassOldToNewMethods = $perClassOldToNewMethods;
$this->methodCallAnalyzer = $methodCallAnalyzer;
2017-10-20 16:23:59 +00:00
$this->staticMethodCallAnalyzer = $staticMethodCallAnalyzer;
$this->methodNameAnalyzer = $methodNameAnalyzer;
2017-10-06 11:57:21 +00:00
}
public function isCandidate(Node $node): bool
{
$this->activeTypes = null;
2017-10-06 11:57:21 +00:00
$matchedTypes = $this->methodCallAnalyzer->matchTypes($node, $this->getClasses());
if ($matchedTypes) {
$this->activeTypes = $matchedTypes;
2017-10-06 11:57:21 +00:00
return true;
}
$matchedTypes = $this->staticMethodCallAnalyzer->matchTypes($node, $this->getClasses());
if ($matchedTypes) {
$this->activeTypes = $matchedTypes;
2017-10-06 11:57:21 +00:00
return true;
}
if ($this->isMethodName($node, $this->getClasses())) {
return true;
}
2017-10-06 11:57:21 +00:00
return false;
}
/**
* @param Identifier|StaticCall|MethodCall $node
2017-10-06 11:57:21 +00:00
*/
public function refactor(Node $node): ?Node
{
$oldToNewMethods = $this->matchOldToNewMethos();
2017-10-20 15:57:24 +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;
}
if ($this->isClassRename($oldToNewMethods)) {
return $this->resolveClassRename($node, $oldToNewMethods, $methodName);
2017-10-20 16:59:24 +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);
}
/**
* @param mixed[] $oldToNewMethods
*/
private function isClassRename(array $oldToNewMethods): bool
{
$firstMethodConfiguration = current($oldToNewMethods);
return is_array($firstMethodConfiguration);
}
/**
* @return string[]
*/
private function matchOldToNewMethos(): array
{
foreach ($this->activeTypes as $activeType) {
if ($this->perClassOldToNewMethods[$activeType]) {
return $this->perClassOldToNewMethods[$activeType];
}
}
return [];
}
/**
* @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
}