mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 06:18:07 +01:00
Add StaticCallToFunctionRector [closes #555]
This commit is contained in:
parent
547646dee0
commit
39d3d37b3a
83
src/Rector/StaticCall/StaticCallToFunctionRector.php
Normal file
83
src/Rector/StaticCall/StaticCallToFunctionRector.php
Normal 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);
|
||||
}
|
||||
}
|
@ -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', []);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\StaticCall\StaticCallToFunctionRector\Source;
|
||||
|
||||
final class SomeOldStaticClass
|
||||
{
|
||||
|
||||
}
|
@ -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';
|
||||
}
|
||||
}
|
@ -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', []);
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
services:
|
||||
Rector\Rector\StaticCall\StaticCallToFunctionRector:
|
||||
$staticCallToFunction:
|
||||
Rector\Tests\Rector\StaticCall\StaticCallToFunctionRector\Source\SomeOldStaticClass::render: 'view'
|
Loading…
x
Reference in New Issue
Block a user