diff --git a/src/Rector/Function_/FunctionToStaticCallRector.php b/src/Rector/Function_/FunctionToStaticCallRector.php new file mode 100644 index 00000000000..005d9921553 --- /dev/null +++ b/src/Rector/Function_/FunctionToStaticCallRector.php @@ -0,0 +1,80 @@ +functionToStaticCall = $functionToMethodCall; + } + + public function getDefinition(): RectorDefinition + { + return new RectorDefinition('Turns defined function call to static method call.', [ + new ConfiguredCodeSample( + 'view("...", []);', + 'SomeClass::render("...", []);', + [ + '$functionToStaticCall' => [ + 'view' => ['SomeStaticClass', 'render'], + ], + ] + ), + ]); + } + + /** + * future compatibility + */ + public function getNodeType(): string + { + return FuncCall::class; + } + + public function isCandidate(Node $node): bool + { + return true; + } + + 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->functionToStaticCall[$functionName])) { + return $node; + } + + [$className, $methodName] = $this->functionToStaticCall[$functionName]; + + $staticCallNode = new StaticCall(new FullyQualified($className), $methodName); + $staticCallNode->args = $node->args; + + return $staticCallNode; + } +} diff --git a/tests/Rector/Function_/FunctionToStaticCallRector/Correct/correct.php.inc b/tests/Rector/Function_/FunctionToStaticCallRector/Correct/correct.php.inc new file mode 100644 index 00000000000..a69751733a6 --- /dev/null +++ b/tests/Rector/Function_/FunctionToStaticCallRector/Correct/correct.php.inc @@ -0,0 +1,11 @@ +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_/FunctionToStaticCallRector/Wrong/wrong.php.inc b/tests/Rector/Function_/FunctionToStaticCallRector/Wrong/wrong.php.inc new file mode 100644 index 00000000000..388dd5a9380 --- /dev/null +++ b/tests/Rector/Function_/FunctionToStaticCallRector/Wrong/wrong.php.inc @@ -0,0 +1,11 @@ +