Add remove by name to ArgumentRemoverRector

This commit is contained in:
Tomas Votruba 2019-03-08 23:53:12 +00:00
parent 28b7a6c0cf
commit 963c7bb4c0
4 changed files with 60 additions and 9 deletions

View File

@ -103,6 +103,11 @@ CODE_SAMPLE
}
if ($match) {
if (isset($match['name'])) {
$this->removeByName($node, $position, $match['name']);
return;
}
// only argument specific value can be removed
if ($node instanceof ClassMethod || ! isset($node->args[$position])) {
return;
@ -123,4 +128,28 @@ CODE_SAMPLE
return in_array($nodeValue, $values, true);
}
/**
* @param ClassMethod|StaticCall|MethodCall $node
*/
private function removeByName(Node $node, int $position, string $name): void
{
if ($node instanceof MethodCall || $node instanceof StaticCall) {
if (isset($node->args[$position])) {
if ($this->isName($node->args[$position], $name)) {
unset($node->args[$position]);
}
return;
}
}
if (isset($node->params[$position])) {
if ($this->isName($node->params[$position], $name)) {
unset($node->params[$position]);
}
return;
}
}
}

View File

@ -31,7 +31,7 @@ final class ArgumentAdderRectorTest extends AbstractRectorTestCase
return [
ContainerBuilder::class => [
'compile' => [
[
0 => [
'name' => 'isCompiled',
'default_value' => false,
],

View File

@ -4,6 +4,7 @@ namespace Rector\Tests\Rector\Argument\ArgumentRemoverRector;
use Rector\Rector\Argument\ArgumentRemoverRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Tests\Rector\Argument\ArgumentRemoverRector\Fixture\RemoveInMiddle;
use Rector\Tests\Rector\Argument\ArgumentRemoverRector\Source\Persister;
use Symfony\Component\Yaml\Yaml;
@ -31,18 +32,20 @@ final class ArgumentRemoverRectorTest extends AbstractRectorTestCase
return [
Persister::class => [
'getSelectJoinColumnSQL' => [
4 => null
]
4 => null,
],
],
Yaml::class => [
'parse' => [
1 => ['Symfony\Component\Yaml\Yaml::PARSE_KEYS_AS_STRINGS', 'hey', 55, 5.5],
],
],
RemoveInMiddle::class => [
'run' => [
1 => [
'Symfony\Component\Yaml\Yaml::PARSE_KEYS_AS_STRINGS',
'hey',
55,
5.5,
]
]
'name' => 'second',
],
],
],
];
}

View File

@ -4,5 +4,24 @@ namespace Rector\Tests\Rector\Argument\ArgumentRemoverRector\Fixture;
final class RemoveInMiddle
{
public function run($first, $second, $third)
{
}
}
?>
-----
<?php
namespace Rector\Tests\Rector\Argument\ArgumentRemoverRector\Fixture;
final class RemoveInMiddle
{
public function run($first, $third)
{
}
}
?>