From 5a1e8ba9570c37fab071f07d2d4efe8cf98262eb Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 22 Oct 2018 20:12:32 +0200 Subject: [PATCH] cleanup StaticMethodNameReplacerRector --- .../StaticMethodNameReplacerRector.php | 203 ++++-------------- .../Correct/correct2.php.inc | 1 + .../Wrong/wrong2.php.inc | 1 + 3 files changed, 42 insertions(+), 163 deletions(-) diff --git a/src/Rector/MethodCall/StaticMethodNameReplacerRector.php b/src/Rector/MethodCall/StaticMethodNameReplacerRector.php index 412c169c23e..04feb855f3a 100644 --- a/src/Rector/MethodCall/StaticMethodNameReplacerRector.php +++ b/src/Rector/MethodCall/StaticMethodNameReplacerRector.php @@ -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; - } } diff --git a/tests/Rector/MethodCall/StaticMethodNameReplacerRector/Correct/correct2.php.inc b/tests/Rector/MethodCall/StaticMethodNameReplacerRector/Correct/correct2.php.inc index 9f1fd2e22fb..bc4bcf2b39b 100644 --- a/tests/Rector/MethodCall/StaticMethodNameReplacerRector/Correct/correct2.php.inc +++ b/tests/Rector/MethodCall/StaticMethodNameReplacerRector/Correct/correct2.php.inc @@ -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); diff --git a/tests/Rector/MethodCall/StaticMethodNameReplacerRector/Wrong/wrong2.php.inc b/tests/Rector/MethodCall/StaticMethodNameReplacerRector/Wrong/wrong2.php.inc index b08cdc23127..fdb31cb0fae 100644 --- a/tests/Rector/MethodCall/StaticMethodNameReplacerRector/Wrong/wrong2.php.inc +++ b/tests/Rector/MethodCall/StaticMethodNameReplacerRector/Wrong/wrong2.php.inc @@ -3,3 +3,4 @@ use Rector\Tests\Rector\MethodCall\MethodNameReplacerRector\Source\FormMacros; $result = FormMacros::renderFormBegin($someArgs); +$result = FormMACROS::renderFormBEGIN($someArgs);