[PHP] Add scope limitation to ArgumentAdderRector for 3party non-existing params [closes #1662]

This commit is contained in:
Tomas Votruba 2019-07-02 19:53:44 +02:00
parent 2b7a0096ba
commit 3aa8e4952d
5 changed files with 117 additions and 1 deletions

View File

@ -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:

View File

@ -131,8 +131,15 @@ CODE_SAMPLE
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 @@ CODE_SAMPLE
// 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;
}
}

View File

@ -5,6 +5,7 @@ namespace Rector\Tests\Rector\Argument\ArgumentAdderRector;
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 @@ final class ArgumentAdderRectorTest extends AbstractRectorTestCase
__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 @@ final class ArgumentAdderRectorTest extends AbstractRectorTestCase
],
],
],
// scoped
SomeParentClient::class => [
'submit' => [
2 => [
'name' => 'serverParameters',
'default_value' => [],
'type' => 'array',
// scope!
'scope' => ['parent_call', 'class_method'],
],
],
],
],
],
];

View File

@ -0,0 +1,43 @@
<?php
namespace Rector\Tests\Rector\Argument\ArgumentAdderRector\Fixture;
use Rector\Tests\Rector\Argument\ArgumentAdderRector\Source\SomeParentClient;
class Scoped extends SomeParentClient
{
public function submit(\DomCrawlerForm $form, array $values = [])
{
return parent::submit($form, $values);
}
public function selfie($form)
{
$self = new self();
$self->submit($form);
}
}
?>
-----
<?php
namespace Rector\Tests\Rector\Argument\ArgumentAdderRector\Fixture;
use Rector\Tests\Rector\Argument\ArgumentAdderRector\Source\SomeParentClient;
class Scoped extends SomeParentClient
{
public function submit(\DomCrawlerForm $form, array $values = [], array $serverParameters = [])
{
return parent::submit($form, $values, $serverParameters);
}
public function selfie($form)
{
$self = new self();
$self->submit($form);
}
}
?>

View File

@ -0,0 +1,10 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Argument\ArgumentAdderRector\Source;
class SomeParentClient
{
public function submit(\DomCrawlerForm $form, array $values = [])
{
}
}