From 738d5ed3e7a983b4edb0d522d2a60087d3c1e8ba Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 21 Oct 2018 13:32:42 +0200 Subject: [PATCH] Fix inlining ofr changed FuncCall args [closes #624] --- src/Rector/Function_/FunctionReplaceRector.php | 13 +++---------- .../Correct/correct2.php.inc | 15 +++++++++++++++ .../FunctionReplaceRectorTest.php | 1 + .../FunctionReplaceRector/Wrong/wrong2.php.inc | 15 +++++++++++++++ .../Function_/FunctionReplaceRector/config.yml | 3 ++- 5 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 tests/Rector/Function_/FunctionReplaceRector/Correct/correct2.php.inc create mode 100644 tests/Rector/Function_/FunctionReplaceRector/Wrong/wrong2.php.inc diff --git a/src/Rector/Function_/FunctionReplaceRector.php b/src/Rector/Function_/FunctionReplaceRector.php index 7a7a06ed7a6..bb4d83d9128 100644 --- a/src/Rector/Function_/FunctionReplaceRector.php +++ b/src/Rector/Function_/FunctionReplaceRector.php @@ -4,7 +4,6 @@ namespace Rector\Rector\Function_; use PhpParser\Node; use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\ConfiguredCodeSample; @@ -53,21 +52,15 @@ final class FunctionReplaceRector extends AbstractRector */ public function refactor(Node $node): ?Node { - // anonymous function - if (! $node->name instanceof Name) { - return null; - } - - $functionName = $node->name->toString(); + $functionName = $this->getName($node); if (! isset($this->oldFunctionToNewFunction[$functionName])) { return null; } $newFunctionName = $this->oldFunctionToNewFunction[$functionName]; - $functCallNode = new FuncCall(new FullyQualified($newFunctionName)); - $functCallNode->args = $node->args; + $node->name = new FullyQualified($newFunctionName); - return $functCallNode; + return $node; } } diff --git a/tests/Rector/Function_/FunctionReplaceRector/Correct/correct2.php.inc b/tests/Rector/Function_/FunctionReplaceRector/Correct/correct2.php.inc new file mode 100644 index 00000000000..348b5d44ba2 --- /dev/null +++ b/tests/Rector/Function_/FunctionReplaceRector/Correct/correct2.php.inc @@ -0,0 +1,15 @@ +setDescription(\Safe\sprintf( + 'Splits monorepo packages to standalone repositories as defined in "%s" section of "%s" config.', + 'parameters > directories_to_repositories', + 'monorepo-builder.yml' + )); + } +} diff --git a/tests/Rector/Function_/FunctionReplaceRector/FunctionReplaceRectorTest.php b/tests/Rector/Function_/FunctionReplaceRector/FunctionReplaceRectorTest.php index 7a7086916d7..21dc6515a24 100644 --- a/tests/Rector/Function_/FunctionReplaceRector/FunctionReplaceRectorTest.php +++ b/tests/Rector/Function_/FunctionReplaceRector/FunctionReplaceRectorTest.php @@ -21,6 +21,7 @@ final class FunctionReplaceRectorTest extends AbstractRectorTestCase 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 diff --git a/tests/Rector/Function_/FunctionReplaceRector/Wrong/wrong2.php.inc b/tests/Rector/Function_/FunctionReplaceRector/Wrong/wrong2.php.inc new file mode 100644 index 00000000000..5945bf52ccd --- /dev/null +++ b/tests/Rector/Function_/FunctionReplaceRector/Wrong/wrong2.php.inc @@ -0,0 +1,15 @@ +setDescription(sprintf( + 'Splits monorepo packages to standalone repositories as defined in "%s" section of "%s" config.', + 'parameters > directories_to_repositories', + 'monorepo-builder.yml' + )); + } +} diff --git a/tests/Rector/Function_/FunctionReplaceRector/config.yml b/tests/Rector/Function_/FunctionReplaceRector/config.yml index 1c25ce5f3cd..612a3c9ebce 100644 --- a/tests/Rector/Function_/FunctionReplaceRector/config.yml +++ b/tests/Rector/Function_/FunctionReplaceRector/config.yml @@ -1,3 +1,4 @@ services: Rector\Rector\Function_\FunctionReplaceRector: - 'view': 'Laravel\Templating\render' + view: 'Laravel\Templating\render' + sprintf: 'Safe\sprintf'