From e917735190c8df99622ea72223f33e81db83e8c9 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 12 Aug 2018 13:06:07 +0200 Subject: [PATCH] Add FunctionReplacerRector [closes #571] --- .../Function_/FunctionReplaceRector.php | 82 +++++++++++++++++++ .../StaticCall/StaticCallToFunctionRector.php | 2 +- .../Correct/correct.php.inc | 9 ++ .../FunctionReplacerRectorTest.php | 30 +++++++ .../FunctionReplaceRector/Wrong/wrong.php.inc | 9 ++ .../FunctionReplaceRector/config.yml | 4 + 6 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 src/Rector/Function_/FunctionReplaceRector.php create mode 100644 tests/Rector/Function_/FunctionReplaceRector/Correct/correct.php.inc create mode 100644 tests/Rector/Function_/FunctionReplaceRector/FunctionReplacerRectorTest.php create mode 100644 tests/Rector/Function_/FunctionReplaceRector/Wrong/wrong.php.inc create mode 100644 tests/Rector/Function_/FunctionReplaceRector/config.yml diff --git a/src/Rector/Function_/FunctionReplaceRector.php b/src/Rector/Function_/FunctionReplaceRector.php new file mode 100644 index 00000000000..f52d29993a4 --- /dev/null +++ b/src/Rector/Function_/FunctionReplaceRector.php @@ -0,0 +1,82 @@ +oldFunctionToNewFunction = $oldFunctionToNewFunction; + } + + public function getDefinition(): RectorDefinition + { + return new RectorDefinition('Turns defined function call new one.', [ + new ConfiguredCodeSample( + 'view("...", []);', + 'Laravel\Templating\render("...", []);', + [ + '$functionToStaticCall' => [ + 'view' => 'Laravel\Templating\render', + ], + ] + ), + ]); + } + + /** + * future compatibility + */ + public function getNodeType(): string + { + return FuncCall::class; + } + + public function isCandidate(Node $node): bool + { + return true; + } + + /** + * @param FuncCall $node + */ + public function refactor(Node $node): ?Node + { + if (! $node instanceof FuncCall) { + return $node; + } + + // anonymous function + if (! $node->name instanceof Name) { + return $node; + } + + $functionName = $node->name->toString(); + if (! isset($this->oldFunctionToNewFunction[$functionName])) { + return $node; + } + + $newFunctionName = $this->oldFunctionToNewFunction[$functionName]; + + $functCallNode = new FuncCall(new FullyQualified($newFunctionName)); + $functCallNode->args = $node->args; + + return $functCallNode; + } +} diff --git a/src/Rector/StaticCall/StaticCallToFunctionRector.php b/src/Rector/StaticCall/StaticCallToFunctionRector.php index 8f830d34cb3..c79faba4579 100644 --- a/src/Rector/StaticCall/StaticCallToFunctionRector.php +++ b/src/Rector/StaticCall/StaticCallToFunctionRector.php @@ -42,7 +42,7 @@ final class StaticCallToFunctionRector extends AbstractRector return new RectorDefinition('Turns static call to function call.', [ new ConfiguredCodeSample( 'OldClass::oldMethod("args");', - 'new_functoin("args");', + 'new_function("args");', [ '$staticCallToFunction' => [ 'OldClass::oldMethod' => 'new_function', diff --git a/tests/Rector/Function_/FunctionReplaceRector/Correct/correct.php.inc b/tests/Rector/Function_/FunctionReplaceRector/Correct/correct.php.inc new file mode 100644 index 00000000000..d32550a4a32 --- /dev/null +++ b/tests/Rector/Function_/FunctionReplaceRector/Correct/correct.php.inc @@ -0,0 +1,9 @@ +doTestFileMatchesExpectedContent($wrong, $fixed); + } + + public function provideWrongToFixedFiles(): Iterator + { + yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc']; + } + + protected function provideConfig(): string + { + return __DIR__ . '/config.yml'; + } +} diff --git a/tests/Rector/Function_/FunctionReplaceRector/Wrong/wrong.php.inc b/tests/Rector/Function_/FunctionReplaceRector/Wrong/wrong.php.inc new file mode 100644 index 00000000000..a7445af76d6 --- /dev/null +++ b/tests/Rector/Function_/FunctionReplaceRector/Wrong/wrong.php.inc @@ -0,0 +1,9 @@ +