From 9fdf6c9336eea0fc3b47ce9a0b24f73f01ffa2b1 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 19 Feb 2019 16:39:32 +0100 Subject: [PATCH] Add SwapFuncCallArgumentsRector --- .../database-migration/mysql-to-mysqli.yaml | 10 ++- .../FuncCall/SwapFuncCallArgumentsRector.php | 87 +++++++++++++++++++ .../Fixture/fixture.php.inc | 27 ++++++ .../SwapFuncCallArgumentsRectorTest.php | 29 +++++++ 4 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 packages/Php/src/Rector/FuncCall/SwapFuncCallArgumentsRector.php create mode 100644 packages/Php/tests/Rector/FuncCall/SwapFuncCallArgumentsRector/Fixture/fixture.php.inc create mode 100644 packages/Php/tests/Rector/FuncCall/SwapFuncCallArgumentsRector/SwapFuncCallArgumentsRectorTest.php diff --git a/config/level/database-migration/mysql-to-mysqli.yaml b/config/level/database-migration/mysql-to-mysqli.yaml index 9769486aaa6..afe05b09f24 100644 --- a/config/level/database-migration/mysql-to-mysqli.yaml +++ b/config/level/database-migration/mysql-to-mysqli.yaml @@ -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: diff --git a/packages/Php/src/Rector/FuncCall/SwapFuncCallArgumentsRector.php b/packages/Php/src/Rector/FuncCall/SwapFuncCallArgumentsRector.php new file mode 100644 index 00000000000..aecef2af97d --- /dev/null +++ b/packages/Php/src/Rector/FuncCall/SwapFuncCallArgumentsRector.php @@ -0,0 +1,87 @@ +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; + } +} diff --git a/packages/Php/tests/Rector/FuncCall/SwapFuncCallArgumentsRector/Fixture/fixture.php.inc b/packages/Php/tests/Rector/FuncCall/SwapFuncCallArgumentsRector/Fixture/fixture.php.inc new file mode 100644 index 00000000000..73d04bfbc62 --- /dev/null +++ b/packages/Php/tests/Rector/FuncCall/SwapFuncCallArgumentsRector/Fixture/fixture.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/packages/Php/tests/Rector/FuncCall/SwapFuncCallArgumentsRector/SwapFuncCallArgumentsRectorTest.php b/packages/Php/tests/Rector/FuncCall/SwapFuncCallArgumentsRector/SwapFuncCallArgumentsRectorTest.php new file mode 100644 index 00000000000..5c47939bfc1 --- /dev/null +++ b/packages/Php/tests/Rector/FuncCall/SwapFuncCallArgumentsRector/SwapFuncCallArgumentsRectorTest.php @@ -0,0 +1,29 @@ +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], + ]; + } +}