mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 06:18:07 +01:00
Merge pull request #569 from rectorphp/function-to-static-call
Add FunctionToStaticCallRector
This commit is contained in:
commit
547646dee0
5
config/level/guzzle/guzzle50.yml
Normal file
5
config/level/guzzle/guzzle50.yml
Normal file
@ -0,0 +1,5 @@
|
||||
services:
|
||||
Rector\Rector\Function_\FunctionToMethodCallRector:
|
||||
$functionToMethodCall:
|
||||
'GuzzleHttp\json_decode': ['GuzzleHttp\Utils', 'jsonDecode']
|
||||
'GuzzleHttp\get_path': ['GuzzleHttp\Utils': 'getPath']
|
80
src/Rector/Function_/FunctionToStaticCallRector.php
Normal file
80
src/Rector/Function_/FunctionToStaticCallRector.php
Normal 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;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\FunctionToStaticCallRector;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function someMethod()
|
||||
{
|
||||
\SomeStaticClass::render('template', []);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\FunctionToStaticCallRector;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function someMethod()
|
||||
{
|
||||
\AnotherStaticClass::render('template', []);
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Function_\FunctionToStaticCallRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
/**
|
||||
* @see \Rector\Rector\Function_\FunctionToStaticCallRector
|
||||
*/
|
||||
final class FunctionToStaticCallRectorTest 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'];
|
||||
yield [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'];
|
||||
}
|
||||
|
||||
protected function provideConfig(): string
|
||||
{
|
||||
return __DIR__ . '/config.yml';
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\FunctionToStaticCallRector;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function someMethod()
|
||||
{
|
||||
\view('template', []);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\FunctionToStaticCallRector;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function someMethod()
|
||||
{
|
||||
\SomeNamespaced\view('template', []);
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
services:
|
||||
Rector\Rector\Function_\FunctionToStaticCallRector:
|
||||
$functionToMethodCall:
|
||||
'view': ['SomeStaticClass', 'render']
|
||||
'SomeNamespaced\view': ['AnotherStaticClass', 'render']
|
Loading…
x
Reference in New Issue
Block a user