cleanup StaticMethodNameReplacerRector

This commit is contained in:
Tomas Votruba 2018-10-22 20:12:32 +02:00
parent 69ab227c63
commit 5a1e8ba957
3 changed files with 42 additions and 163 deletions

View File

@ -6,9 +6,6 @@ use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use Rector\Builder\IdentifierRenamer;
use Rector\NodeAnalyzer\MethodNameAnalyzer;
use Rector\NodeTypeResolver\Node\Attribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -16,48 +13,39 @@ use Rector\RectorDefinition\RectorDefinition;
final class StaticMethodNameReplacerRector extends AbstractRector
{
/**
* @var string[][]
* @var string[][]|string[][][]
*/
private $perClassOldToNewMethods = [];
private $oldToNewMethodByClasses = [];
/**
* @var string[]
* @param string[][]|string[][][] $oldToNewMethodByClasses
*/
private $activeTypes = [];
/**
* @var MethodNameAnalyzer
*/
private $methodNameAnalyzer;
/**
* @var IdentifierRenamer
*/
private $identifierRenamer;
/**
* @param string[][] $perClassOldToNewMethods
*/
public function __construct(
array $perClassOldToNewMethods,
MethodNameAnalyzer $methodNameAnalyzer,
IdentifierRenamer $identifierRenamer
) {
$this->perClassOldToNewMethods = $perClassOldToNewMethods;
$this->methodNameAnalyzer = $methodNameAnalyzer;
$this->identifierRenamer = $identifierRenamer;
public function __construct(array $oldToNewMethodByClasses)
{
$this->oldToNewMethodByClasses = $oldToNewMethodByClasses;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Turns method names to new ones.', [
new ConfiguredCodeSample(
'SomeClass::oldStaticMethod();',
'AnotherExampleClass::newStaticMethod();',
[
'$oldToNewMethodByClasses' => [
'SomeClass' => [
'oldMethod' => ['AnotherExampleClass', 'newStaticMethod'],
],
],
]
),
new ConfiguredCodeSample(
'SomeClass::oldStaticMethod();',
'SomeClass::newStaticMethod();',
[
'$perClassOldToNewMethods' => [
'$oldToNewMethodByClasses' => [
'SomeClass' => [
'oldMethod' => ['SomeClass', 'newMethod'],
'oldMethod' => 'newStaticMethod',
],
],
]
@ -70,155 +58,44 @@ final class StaticMethodNameReplacerRector extends AbstractRector
*/
public function getNodeTypes(): array
{
return [Identifier::class, StaticCall::class];
return [StaticCall::class];
}
/**
* @param Identifier|StaticCall $node
* @param StaticCall $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof Identifier) {
return $this->processIdentifierNode($node);
}
foreach ($this->oldToNewMethodByClasses as $type => $oldToNewMethods) {
if (! $this->isType($node, $type)) {
continue;
}
if ($node instanceof StaticCall) {
return $this->processStaticCallNode($node);
foreach ($oldToNewMethods as $oldMethod => $newMethod) {
if (! $this->isNameInsensitive($node, $oldMethod)) {
continue;
}
return $this->rename($node, $newMethod);
}
}
return null;
}
/**
* @return string[]
* @param string|string[] $newMethod
*/
private function getClasses(): array
private function rename(StaticCall $staticCallNode, $newMethod): StaticCall
{
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 matchOldToNewMethods(): array
{
foreach ($this->activeTypes as $activeType) {
if (isset($this->perClassOldToNewMethods[$activeType])) {
return $this->perClassOldToNewMethods[$activeType];
}
if (is_array($newMethod)) {
[$newClass, $newMethod] = $newMethod;
$staticCallNode->class = new Name($newClass);
$staticCallNode->name = new Identifier($newMethod);
} else {
$staticCallNode->name = new Identifier($newMethod);
}
return [];
}
/**
* @param string[] $types
*/
private function isMethodName(Node $node, array $types): bool
{
// already covered by previous methods
if (! $node instanceof Identifier) {
return false;
}
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);
if ($parentNode instanceof StaticCall) {
return false;
}
if (! $this->methodNameAnalyzer->isOverrideOfTypes($node, $types)) {
return false;
}
/** @var Identifier $node */
$parentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
/** @var Identifier $node */
if (! isset($this->perClassOldToNewMethods[$parentClassName][$node->name])) {
return false;
}
$this->activeTypes = [$parentClassName];
return true;
}
private function resolveIdentifier(Identifier $node): ?Identifier
{
$oldToNewMethods = $this->matchOldToNewMethods();
$methodName = $node->name;
if (! isset($oldToNewMethods[$methodName])) {
return null;
}
$node->name = $oldToNewMethods[$methodName];
return $node;
}
/**
* @param string[] $oldToNewMethods
*/
private function resolveClassRename(StaticCall $staticCallNode, array $oldToNewMethods, string $methodName): Node
{
[$newClass, $newMethod] = $oldToNewMethods[$methodName];
$staticCallNode->class = new Name($newClass);
$this->identifierRenamer->renameNode($staticCallNode, $newMethod);
return $staticCallNode;
}
private function processIdentifierNode(Identifier $node): ?Identifier
{
$this->activeTypes = [];
$matchedTypes = $this->matchTypes($node, $this->getClasses());
if ($matchedTypes) {
$this->activeTypes = $matchedTypes;
}
if (! $this->isMethodName($node, $this->getClasses())) {
return null;
}
return $this->resolveIdentifier($node);
}
private function processStaticCallNode(StaticCall $node): ?Node
{
$this->activeTypes = [];
$matchedTypes = $this->matchTypes($node, $this->getClasses());
if ($matchedTypes) {
$this->activeTypes = $matchedTypes;
}
$oldToNewMethods = $this->matchOldToNewMethods();
/** @var Identifier $identifierNode */
$identifierNode = $node->name;
$methodName = $identifierNode->toString();
if (! isset($oldToNewMethods[$methodName])) {
return null;
}
if ($this->isClassRename($oldToNewMethods)) {
return $this->resolveClassRename($node, $oldToNewMethods, $methodName);
}
$this->identifierRenamer->renameNode($node, $oldToNewMethods[$methodName]);
return $node;
}
}

View File

@ -3,3 +3,4 @@
use Rector\Tests\Rector\MethodCall\MethodNameReplacerRector\Source\FormMacros;
$result = Nette\Bridges\FormsLatte\Runtime::renderFormBegin($someArgs);
$result = Nette\Bridges\FormsLatte\Runtime::renderFormBegin($someArgs);

View File

@ -3,3 +3,4 @@
use Rector\Tests\Rector\MethodCall\MethodNameReplacerRector\Source\FormMacros;
$result = FormMacros::renderFormBegin($someArgs);
$result = FormMACROS::renderFormBEGIN($someArgs);