Make StaticCallToFunctionRector configuration consistent with type as key

This commit is contained in:
Tomas Votruba 2018-10-31 17:39:39 +01:00
parent a2d68ad756
commit 023522acf2
3 changed files with 26 additions and 22 deletions

View File

@ -16,8 +16,10 @@ services:
'GuzzleHttp\json_decode': ['GuzzleHttp\Utils', 'jsonDecode']
'GuzzleHttp\get_path': ['GuzzleHttp\Utils', 'getPath']
Rector\Rector\StaticCall\StaticCallToFunctionRector:
'GuzzleHttp\Utils::setPath': 'GuzzleHttp\set_path'
'GuzzleHttp\Pool::batch': 'GuzzleHttp\Pool\batch'
'GuzzleHttp\Utils':
setPath: 'GuzzleHttp\set_path'
'GuzzleHttp\Pool':
batch: 'GuzzleHttp\Pool\batch'
Rector\Guzzle\Rector\FuncCall\MessageAsArrayRector: ~
Rector\Rector\MethodCall\MethodNameReplacerRector:

View File

@ -13,16 +13,16 @@ use Rector\RectorDefinition\RectorDefinition;
final class StaticCallToFunctionRector extends AbstractRector
{
/**
* @var string[]
* @var string[][]
*/
private $staticCallToFunction = [];
private $staticCallToFunctionByType = [];
/**
* @param string[] $staticCallToFunction
* @param string[][] $staticCallToFunctionByType
*/
public function __construct(array $staticCallToFunction)
public function __construct(array $staticCallToFunctionByType)
{
$this->staticCallToFunction = $staticCallToFunction;
$this->staticCallToFunctionByType = $staticCallToFunctionByType;
}
public function getDefinition(): RectorDefinition
@ -33,7 +33,9 @@ final class StaticCallToFunctionRector extends AbstractRector
'new_function("args");',
[
'$staticCallToFunction' => [
'OldClass::oldMethod' => 'new_function',
'OldClass' => [
'oldMethod' => 'new_function',
],
],
]
),
@ -53,21 +55,20 @@ final class StaticCallToFunctionRector extends AbstractRector
*/
public function refactor(Node $node): ?Node
{
$staticCalls = array_keys($this->staticCallToFunction);
$activeStaticCall = null;
foreach ($staticCalls as $staticCall) {
[$class, $method] = explode('::', $staticCall);
if ($this->isType($node, $class) && $this->isName($node, $method)) {
$activeStaticCall = $staticCall;
foreach ($this->staticCallToFunctionByType as $type => $methodNamesToFunctions) {
if (! $this->isType($node, $type)) {
continue;
}
foreach ($methodNamesToFunctions as $methodName => $function) {
if (! $this->isName($node, $methodName)) {
continue;
}
return new FuncCall(new FullyQualified($function), $node->args);
}
}
if (! isset($this->staticCallToFunction[$activeStaticCall])) {
return null;
}
$newFunctionName = $this->staticCallToFunction[$activeStaticCall];
return new FuncCall(new FullyQualified($newFunctionName), $node->args);
return null;
}
}

View File

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