Add SwapFuncCallArgumentsRector

This commit is contained in:
Tomas Votruba 2019-02-19 16:39:32 +01:00
parent 683cb08846
commit 9fdf6c9336
4 changed files with 151 additions and 2 deletions

View File

@ -6,6 +6,14 @@ services:
Rector\MysqlToMysqli\Rector\FuncCall\MysqlFuncCallToMysqliRector: ~
Rector\MysqlToMysqli\Rector\FuncCall\MysqlPConnectToMysqliConnectRector: ~
# first swap arguments, then rename
Rector\Php\Rector\FuncCall\SwapFuncCallArgumentsRector:
mysql_real_escape_string: [1, 0]
mysql_select_db: [1, 0]
mysql_set_charset: [1, 0]
mysql_query: [1, 0]
# @todo
Rector\Rector\Function_\FunctionReplaceRector:
mysql_affected_rows: 'mysqli_affected_rows'
mysql_close: 'mysqli_close'
@ -41,8 +49,6 @@ services:
mysql_num_fields: 'mysqli_field_count'
mysql_connect: 'mysqli_connect'
# Rector\Rector\Function_\SwapArgumentsRector:
# http://php.net/manual/en/mysql.constants.php ↓
# http://php.net/manual/en/mysqli.constants.php
Rector\Php\Rector\ConstFetch\ConstantReplacerRector:

View File

@ -0,0 +1,87 @@
<?php declare(strict_types=1);
namespace Rector\Php\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class SwapFuncCallArgumentsRector extends AbstractRector
{
/**
* @var int[][]
*/
private $newArgumentPositionsByFunctionName = [];
/**
* @param int[][] $newArgumentPositionsByFunctionName
*/
public function __construct(array $newArgumentPositionsByFunctionName)
{
$this->newArgumentPositionsByFunctionName = $newArgumentPositionsByFunctionName;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Swap arguments in function calls', [
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run($one, $two)
{
return some_function($one, $two);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run($one, $two)
{
return some_function($two, $one);
}
}
CODE_SAMPLE
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
foreach ($this->newArgumentPositionsByFunctionName as $functionName => $newArgumentPositions) {
if (! $this->isName($node, $functionName)) {
continue;
}
$newArguments = [];
foreach ($newArgumentPositions as $oldPosition => $newPosition) {
if (! isset($node->args[$oldPosition]) || ! isset($node->args[$newPosition])) {
continue;
}
$newArguments[$newPosition] = $node->args[$oldPosition];
}
foreach ($newArguments as $newPosition => $argument) {
$node->args[$newPosition] = $argument;
}
}
return $node;
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Rector\Php\Tests\Rector\FuncCall\SwapFuncCallArgumentsRector\Fixture;
final class SomeClass
{
public function run($one, $two)
{
return some_function($one, $two);
}
}
?>
-----
<?php
namespace Rector\Php\Tests\Rector\FuncCall\SwapFuncCallArgumentsRector\Fixture;
final class SomeClass
{
public function run($one, $two)
{
return some_function($two, $one);
}
}
?>

View File

@ -0,0 +1,29 @@
<?php declare(strict_types=1);
namespace Rector\Php\Tests\Rector\FuncCall\SwapFuncCallArgumentsRector;
use Rector\Php\Rector\FuncCall\SwapFuncCallArgumentsRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class SwapFuncCallArgumentsRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
}
protected function getRectorClass(): string
{
return SwapFuncCallArgumentsRector::class;
}
/**
* @return mixed[]|null
*/
protected function getRectorConfiguration(): ?array
{
return [
'some_function' => [1, 0],
];
}
}