From 3aa8e4952ddad0fa669a9bebc0a7d65dadfa5599 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 2 Jul 2019 19:53:44 +0200 Subject: [PATCH] [PHP] Add scope limitation to ArgumentAdderRector for 3party non-existing params [closes #1662] --- config/set/symfony/symfony42.yaml | 11 ++++- src/Rector/Argument/ArgumentAdderRector.php | 39 +++++++++++++++++ .../ArgumentAdderRectorTest.php | 15 +++++++ .../Fixture/scoped.php.inc | 43 +++++++++++++++++++ .../Source/SomeParentClient.php | 10 +++++ 5 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 tests/Rector/Argument/ArgumentAdderRector/Fixture/scoped.php.inc create mode 100644 tests/Rector/Argument/ArgumentAdderRector/Source/SomeParentClient.php diff --git a/config/set/symfony/symfony42.yaml b/config/set/symfony/symfony42.yaml index 51cf5667e817..067b0bd032c8 100644 --- a/config/set/symfony/symfony42.yaml +++ b/config/set/symfony/symfony42.yaml @@ -25,6 +25,7 @@ services: 2: name: 'serverParameters' default_value: [] + scope: ['method_call'] # https://github.com/symfony/symfony/commit/f634afdb6f573e4af8d89aaa605e0c7d4058676d Symfony\Component\DomCrawler\Crawler: @@ -32,41 +33,49 @@ services: 0: # $selector default_value: null + scope: ['method_call'] Symfony\Component\Finder\Finder: sortByName: 0: # $useNaturalSort - default_vlaue: false + default_value: false + scope: ['method_call'] Symfony\Bridge\Monolog\Processor\DebugProcessor: getLogs: 0: # $request default_value: null + scope: ['method_call'] countErrors: 0: # $request default_value: null + scope: ['method_call'] Symfony\Bridge\Monolog\Logger: getLogs: 0: # $request default_value: null + scope: ['method_call'] countErrors: 0: # $request default_value: null + scope: ['method_call'] Symfony\Component\Serializer\Normalizer: handleCircularReference: 1: # $format default_value: null + scope: ['method_call'] 2: # $context default_value: [] + scope: ['method_call'] Rector\Rector\MethodCall\RenameMethodRector: Symfony\Component\Cache\CacheItem: diff --git a/src/Rector/Argument/ArgumentAdderRector.php b/src/Rector/Argument/ArgumentAdderRector.php index 232829a4bc07..3158aeb86ec6 100644 --- a/src/Rector/Argument/ArgumentAdderRector.php +++ b/src/Rector/Argument/ArgumentAdderRector.php @@ -131,8 +131,15 @@ private function processPositionWithDefaultValues(Node $node, array $positionWit continue; } + // is correct scope? + if (! $this->isInCorrectScope($node, $parameterConfiguration)) { + continue; + } + if ($node instanceof ClassMethod) { $this->addClassMethodParam($node, $name, $defaultValue, $type, $position); + } elseif ($node instanceof StaticCall && $this->isName($node->class, 'parent')) { + $node->args[$position] = new Arg(new Variable($name)); } else { $arg = new Arg(BuilderHelpers::normalizeValue($defaultValue)); $node->args[$position] = $arg; @@ -171,4 +178,36 @@ private function shouldSkipParameter(Node $node, int $position, string $name): b // already added? return isset($node->args[$position]) && $this->isName($node->args[$position], $name); } + + /** + * @param ClassMethod|MethodCall|StaticCall $node + * @param mixed[] $parameterConfiguration + */ + private function isInCorrectScope(Node $node, array $parameterConfiguration): bool + { + if (! isset($parameterConfiguration['scope'])) { + return true; + } + + /** @var string[] $scope */ + $scope = $parameterConfiguration['scope']; + + if ($node instanceof ClassMethod) { + return in_array('class_method', $scope, true); + } + + if ($node instanceof StaticCall) { + if ($this->isName($node->class, 'parent')) { + return in_array('parent_call', $scope, true); + } + + return in_array('method_call', $scope, true); + } + + if ($node instanceof MethodCall) { + return in_array('method_call', $scope, true); + } + + return false; + } } diff --git a/tests/Rector/Argument/ArgumentAdderRector/ArgumentAdderRectorTest.php b/tests/Rector/Argument/ArgumentAdderRector/ArgumentAdderRectorTest.php index 9dd283a77eca..d64ad54e1869 100644 --- a/tests/Rector/Argument/ArgumentAdderRector/ArgumentAdderRectorTest.php +++ b/tests/Rector/Argument/ArgumentAdderRector/ArgumentAdderRectorTest.php @@ -5,6 +5,7 @@ use Rector\Rector\Argument\ArgumentAdderRector; use Rector\Testing\PHPUnit\AbstractRectorTestCase; use Rector\Tests\Rector\Argument\ArgumentAdderRector\Source\SomeContainerBuilder; +use Rector\Tests\Rector\Argument\ArgumentAdderRector\Source\SomeParentClient; final class ArgumentAdderRectorTest extends AbstractRectorTestCase { @@ -14,6 +15,7 @@ public function test(): void __DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/fixture2.php.inc', __DIR__ . '/Fixture/fixture3.php.inc', + __DIR__ . '/Fixture/scoped.php.inc', __DIR__ . '/Fixture/already_added.php.inc', ]); } @@ -41,6 +43,19 @@ protected function getRectorsWithConfiguration(): array ], ], ], + + // scoped + SomeParentClient::class => [ + 'submit' => [ + 2 => [ + 'name' => 'serverParameters', + 'default_value' => [], + 'type' => 'array', + // scope! + 'scope' => ['parent_call', 'class_method'], + ], + ], + ], ], ], ]; diff --git a/tests/Rector/Argument/ArgumentAdderRector/Fixture/scoped.php.inc b/tests/Rector/Argument/ArgumentAdderRector/Fixture/scoped.php.inc new file mode 100644 index 000000000000..54ff2eebf26d --- /dev/null +++ b/tests/Rector/Argument/ArgumentAdderRector/Fixture/scoped.php.inc @@ -0,0 +1,43 @@ +submit($form); + } +} + +?> +----- +submit($form); + } +} + +?> diff --git a/tests/Rector/Argument/ArgumentAdderRector/Source/SomeParentClient.php b/tests/Rector/Argument/ArgumentAdderRector/Source/SomeParentClient.php new file mode 100644 index 000000000000..518957dd9483 --- /dev/null +++ b/tests/Rector/Argument/ArgumentAdderRector/Source/SomeParentClient.php @@ -0,0 +1,10 @@ +