mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 06:18:07 +01:00
Add FunctionReplacerRector [closes #571]
This commit is contained in:
parent
a29829faf9
commit
e917735190
82
src/Rector/Function_/FunctionReplaceRector.php
Normal file
82
src/Rector/Function_/FunctionReplaceRector.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Function_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\ConfiguredCodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
final class FunctionReplaceRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $oldFunctionToNewFunction = [];
|
||||
|
||||
/**
|
||||
* @param string[] $oldFunctionToNewFunction
|
||||
*/
|
||||
public function __construct(array $oldFunctionToNewFunction)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
@ -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',
|
||||
|
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function someMethod()
|
||||
{
|
||||
\Laravel\Templating\render('template', []);
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Function_\FunctionReplacerRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
/**
|
||||
* @see \Rector\Rector\Function_\FunctionReplaceRector
|
||||
*/
|
||||
final class FunctionReplacerRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideWrongToFixedFiles()
|
||||
*/
|
||||
public function test(string $wrong, string $fixed): void
|
||||
{
|
||||
$this->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';
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function someMethod()
|
||||
{
|
||||
view('template', []);
|
||||
}
|
||||
}
|
4
tests/Rector/Function_/FunctionReplaceRector/config.yml
Normal file
4
tests/Rector/Function_/FunctionReplaceRector/config.yml
Normal file
@ -0,0 +1,4 @@
|
||||
services:
|
||||
Rector\Rector\Function_\FunctionReplaceRector:
|
||||
$oldFunctionToNewFunction:
|
||||
'view': 'Laravel\Templating\render'
|
Loading…
x
Reference in New Issue
Block a user