Add StaticCallToFunctionRector [closes #555]

This commit is contained in:
Tomas Votruba 2018-08-11 12:22:29 +02:00
parent 547646dee0
commit 39d3d37b3a
6 changed files with 151 additions and 0 deletions

View File

@ -0,0 +1,83 @@
<?php declare(strict_types=1);
namespace Rector\Rector\StaticCall;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name\FullyQualified;
use Rector\NodeAnalyzer\StaticMethodCallAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class StaticCallToFunctionRector extends AbstractRector
{
/**
* @var string[][]
*/
private $staticCallToFunction = [];
/**
* @var StaticMethodCallAnalyzer
*/
private $staticMethodCallAnalyzer;
/**
* @var string|null
*/
private $activeStaticCall;
/**
* @param string[] $staticCallToFunction
*/
public function __construct(array $staticCallToFunction, StaticMethodCallAnalyzer $staticMethodCallAnalyzer)
{
$this->staticCallToFunction = $staticCallToFunction;
$this->staticMethodCallAnalyzer = $staticMethodCallAnalyzer;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Turns static call to function call.', [
new ConfiguredCodeSample(
'OldClass::oldMethod("args");',
'new_functoin("args");',
[
'$staticCallToFunction' => [
'OldClass::oldMethod' => 'new_function',
],
]
),
]);
}
public function isCandidate(Node $node): bool
{
if (! $node instanceof StaticCall) {
return false;
}
$staticCalls = array_keys($this->staticCallToFunction);
foreach ($staticCalls as $staticCall) {
[$class, $method] = explode('::', $staticCall);
if ($this->staticMethodCallAnalyzer->isTypeAndMethod($node, $class, $method)) {
$this->activeStaticCall = $staticCall;
return true;
}
}
return false;
}
/**
* @param StaticCall $node
*/
public function refactor(Node $node): ?Node
{
$newFunctionName = $this->staticCallToFunction[$this->activeStaticCall];
return new FuncCall(new FullyQualified($newFunctionName), $node->args);
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace Rector\Tests\Rector\StaticCall\StaticCallToFunctionRector\Wrong;
use Rector\Tests\Rector\StaticCall\StaticCallToFunctionRector\Source\SomeOldStaticClass;
class SomeClass
{
public function someMethod()
{
\view('template', []);
}
}

View File

@ -0,0 +1,8 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\StaticCall\StaticCallToFunctionRector\Source;
final class SomeOldStaticClass
{
}

View File

@ -0,0 +1,30 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\StaticCall\StaticCallToFunctionRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
/**
* @see \Rector\Rector\StaticCall\StaticCallToFunctionRector
*/
final class StaticCallToFunctionRectorTest 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,13 @@
<?php
namespace Rector\Tests\Rector\StaticCall\StaticCallToFunctionRector\Wrong;
use Rector\Tests\Rector\StaticCall\StaticCallToFunctionRector\Source\SomeOldStaticClass;
class SomeClass
{
public function someMethod()
{
SomeOldStaticClass::render('template', []);
}
}

View File

@ -0,0 +1,4 @@
services:
Rector\Rector\StaticCall\StaticCallToFunctionRector:
$staticCallToFunction:
Rector\Tests\Rector\StaticCall\StaticCallToFunctionRector\Source\SomeOldStaticClass::render: 'view'