diff --git a/src/Rector/Contrib/Nette/Utils/HtmlAddMethodRector.php b/src/Rector/Contrib/Nette/Utils/HtmlAddMethodRector.php deleted file mode 100644 index 286fc972166..00000000000 --- a/src/Rector/Contrib/Nette/Utils/HtmlAddMethodRector.php +++ /dev/null @@ -1,20 +0,0 @@ - [ - 'add' => 'addHtml', - ], - ]; - } -} diff --git a/src/Rector/Dynamic/MethodNameReplacerRector.php b/src/Rector/Dynamic/MethodNameReplacerRector.php new file mode 100644 index 00000000000..37c2281d536 --- /dev/null +++ b/src/Rector/Dynamic/MethodNameReplacerRector.php @@ -0,0 +1,136 @@ + [ + * oldMethod => newMethod + * ] + * + * @var string[][] + */ + private $perClassOldToNewMethods = []; + + /** + * @param string[][] + */ + public function __construct(array $perClassOldToNewMethods) + { + $this->perClassOldToNewMethods = $perClassOldToNewMethods; + } + + /** + * @var string|null + */ + private $activeType; + + public function isCandidate(Node $node): bool + { + $this->activeType = null; + + if ($this->isOnTypeCall($node)) { + return true; + } + + if ($this->isStaticCallOnType($node)) { + return true; + } + + return false; + } + + /** + * @param StaticCall|MethodCall $node + */ + public function refactor(Node $node): ?Node + { + $oldToNewMethods = $this->perClassOldToNewMethods[$this->activeType]; + + foreach ($oldToNewMethods as $oldMethod => $newMethod) { + $methodName = $node->name->name; + if ($methodName !== $oldMethod) { + continue; + } + + $node->name->name = $newMethod; + } + + return $node; + } + + private function isOnTypeCall(Node $node): bool + { + if (! $node instanceof MethodCall) { + return false; + } + + if (! $node->var instanceof Variable) { + return false; + } + + /** @var string|null $type */ + $type = $node->var->getAttribute(Attribute::TYPE); + if ($type === null) { + return false; + } + + if (! $this->isTypeRelevant($type)) { + return false; + } + + $this->activeType = $type; + + return true; + } + + private function isStaticCallOnType(Node $node): bool + { + if (! $node instanceof StaticCall) { + return false; + } + + if (! $node->name instanceof Identifier) { + return false; + } + + if (! $node->class instanceof Name) { + return false; + } + + $type = $node->class->toString(); + + if (! $this->isTypeRelevant($type)) { + return false; + } + + $this->activeType = $type; + + return true; + } + + private function isTypeRelevant(string $type): bool + { + $classes = $this->getClasses(); + + return in_array($type, $classes, true); + } + + /** + * @return string[] + */ + private function getClasses(): array + { + return array_keys($this->perClassOldToNewMethods); + } +} diff --git a/src/config/level/nette/nette24.yml b/src/config/level/nette/nette24.yml index 999c0576d8a..4f62a32101a 100644 --- a/src/config/level/nette/nette24.yml +++ b/src/config/level/nette/nette24.yml @@ -1,9 +1,12 @@ rectors: - - Rector\Rector\Contrib\Nette\Form\FormNegativeRulesRector - - Rector\Rector\Contrib\Nette\Form\FormCallbackRector - - Rector\Rector\Contrib\Nette\Form\FormSetRequiredRector - - Rector\Rector\Contrib\Nette\Utils\HtmlAddMethodRector - - Rector\Rector\Contrib\Nette\Utils\NetteObjectToSmartTraitRector - - Rector\Rector\Contrib\Nette\PhpGenerator\PhpGeneratorDocumentMethodRector - - Rector\Rector\Contrib\Nette\Utils\MagicMethodRector - - Rector\Rector\Contrib\Nette\DI\SetEntityToStatementRector + Rector\Rector\Contrib\Nette\Form\FormNegativeRulesRector: ~ + Rector\Rector\Contrib\Nette\Form\FormCallbackRector: ~ + Rector\Rector\Contrib\Nette\Form\FormSetRequiredRector: ~ + Rector\Rector\Contrib\Nette\Utils\NetteObjectToSmartTraitRector: ~ + Rector\Rector\Contrib\Nette\PhpGenerator\PhpGeneratorDocumentMethodRector: ~ + Rector\Rector\Contrib\Nette\Utils\MagicMethodRector: ~ + Rector\Rector\Contrib\Nette\DI\SetEntityToStatementRector: ~ + + Rector\Rector\Dynamic\MethodNameReplacerRector: + 'Nette\Utils\Html': + 'add': 'addHtml' diff --git a/tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Test.php b/tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Test.php deleted file mode 100644 index a3b409e6ee4..00000000000 --- a/tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Test.php +++ /dev/null @@ -1,41 +0,0 @@ -doTestFileMatchesExpectedContent( - __DIR__ . '/Wrong/wrong.php.inc', - __DIR__ . '/Correct/correct.php.inc' - ); - $this->doTestFileMatchesExpectedContent( - __DIR__ . '/Wrong/wrong2.php.inc', - __DIR__ . '/Correct/correct2.php.inc' - ); - $this->doTestFileMatchesExpectedContent( - __DIR__ . '/Wrong/wrong3.php.inc', - __DIR__ . '/Correct/correct3.php.inc' - ); - $this->doTestFileMatchesExpectedContent( - __DIR__ . '/Wrong/wrong4.php.inc', - __DIR__ . '/Correct/correct4.php.inc' - ); - $this->doTestFileMatchesExpectedContent( - __DIR__ . '/Wrong/SomeClass.php', - __DIR__ . '/Correct/SomeClass.php' - ); - } - - /** - * @return string[] - */ - protected function getRectorClasses(): array - { - return [HtmlAddMethodRector::class]; - } -} diff --git a/tests/Rector/Dynamic/MethodNameReplacerRector/Test.php b/tests/Rector/Dynamic/MethodNameReplacerRector/Test.php new file mode 100644 index 00000000000..052a4a9a24e --- /dev/null +++ b/tests/Rector/Dynamic/MethodNameReplacerRector/Test.php @@ -0,0 +1,41 @@ +doTestFileMatchesExpectedContent( + __DIR__ . '/wrong/wrong.php.inc', + __DIR__ . '/correct/correct.php.inc' + ); + $this->doTestFileMatchesExpectedContent( + __DIR__ . '/wrong/wrong2.php.inc', + __DIR__ . '/correct/correct2.php.inc' + ); + $this->doTestFileMatchesExpectedContent( + __DIR__ . '/wrong/wrong3.php.inc', + __DIR__ . '/correct/correct3.php.inc' + ); + $this->doTestFileMatchesExpectedContent( + __DIR__ . '/wrong/wrong4.php.inc', + __DIR__ . '/correct/correct4.php.inc' + ); + $this->doTestFileMatchesExpectedContent( + __DIR__ . '/wrong/SomeClass.php', + __DIR__ . '/correct/SomeClass.php' + ); + } + + /** + * @return string[] + */ + protected function getRectorClasses(): array + { + return [MethodNameReplacerRector::class]; + } +} diff --git a/tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Correct/SomeClass.php b/tests/Rector/Dynamic/MethodNameReplacerRector/correct/SomeClass.php similarity index 100% rename from tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Correct/SomeClass.php rename to tests/Rector/Dynamic/MethodNameReplacerRector/correct/SomeClass.php diff --git a/tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Correct/correct.php.inc b/tests/Rector/Dynamic/MethodNameReplacerRector/correct/correct.php.inc similarity index 100% rename from tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Correct/correct.php.inc rename to tests/Rector/Dynamic/MethodNameReplacerRector/correct/correct.php.inc diff --git a/tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Correct/correct2.php.inc b/tests/Rector/Dynamic/MethodNameReplacerRector/correct/correct2.php.inc similarity index 100% rename from tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Correct/correct2.php.inc rename to tests/Rector/Dynamic/MethodNameReplacerRector/correct/correct2.php.inc diff --git a/tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Correct/correct3.php.inc b/tests/Rector/Dynamic/MethodNameReplacerRector/correct/correct3.php.inc similarity index 100% rename from tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Correct/correct3.php.inc rename to tests/Rector/Dynamic/MethodNameReplacerRector/correct/correct3.php.inc diff --git a/tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Correct/correct4.php.inc b/tests/Rector/Dynamic/MethodNameReplacerRector/correct/correct4.php.inc similarity index 100% rename from tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Correct/correct4.php.inc rename to tests/Rector/Dynamic/MethodNameReplacerRector/correct/correct4.php.inc diff --git a/tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Wrong/SomeClass.php b/tests/Rector/Dynamic/MethodNameReplacerRector/wrong/SomeClass.php similarity index 100% rename from tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Wrong/SomeClass.php rename to tests/Rector/Dynamic/MethodNameReplacerRector/wrong/SomeClass.php diff --git a/tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Wrong/wrong.php.inc b/tests/Rector/Dynamic/MethodNameReplacerRector/wrong/wrong.php.inc similarity index 100% rename from tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Wrong/wrong.php.inc rename to tests/Rector/Dynamic/MethodNameReplacerRector/wrong/wrong.php.inc diff --git a/tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Wrong/wrong2.php.inc b/tests/Rector/Dynamic/MethodNameReplacerRector/wrong/wrong2.php.inc similarity index 100% rename from tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Wrong/wrong2.php.inc rename to tests/Rector/Dynamic/MethodNameReplacerRector/wrong/wrong2.php.inc diff --git a/tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Wrong/wrong3.php.inc b/tests/Rector/Dynamic/MethodNameReplacerRector/wrong/wrong3.php.inc similarity index 100% rename from tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Wrong/wrong3.php.inc rename to tests/Rector/Dynamic/MethodNameReplacerRector/wrong/wrong3.php.inc diff --git a/tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Wrong/wrong4.php.inc b/tests/Rector/Dynamic/MethodNameReplacerRector/wrong/wrong4.php.inc similarity index 100% rename from tests/Rector/Contrib/Nette/Utils/HtmlAddMethodRector/Wrong/wrong4.php.inc rename to tests/Rector/Dynamic/MethodNameReplacerRector/wrong/wrong4.php.inc