Add FunctionToStaticCallRector [closes #556]

This commit is contained in:
Tomas Votruba 2018-08-11 11:05:46 +02:00
parent cca0fc4f98
commit 1db0b88115
5 changed files with 136 additions and 0 deletions

View File

@ -0,0 +1,80 @@
<?php declare(strict_types=1);
namespace Rector\Rector\Function_;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class FunctionToStaticCallRector extends AbstractRector
{
/**
* @var string[]
*/
private $functionToStaticCall = [];
/**
* @param string[] $functionToMethodCall
*/
public function __construct(array $functionToMethodCall)
{
$this->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;
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace Rector\Tests\FunctionToStaticCallRector;
class SomeClass
{
public function someMethod()
{
\SomeStaticClass::render('template', []);
}
}

View File

@ -0,0 +1,30 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Function_\FunctionToMethodCallRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
/**
* @see \Rector\Rector\Function_\FunctionToStaticCallRector
*/
final class FunctionToMethodCallRectorTest 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';
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace Rector\Tests\FunctionToStaticCallRector;
class SomeClass
{
public function someMethod()
{
\view('template', []);
}
}

View File

@ -0,0 +1,4 @@
services:
Rector\Rector\Function_\FunctionToStaticCallRector:
$functionToMethodCall:
'view': ['SomeStaticClass', 'render']