diff --git a/config/level/symfony/symfony34.yml b/config/level/symfony/symfony34.yml index 4f2284bd500..515312a2e70 100644 --- a/config/level/symfony/symfony34.yml +++ b/config/level/symfony/symfony34.yml @@ -7,3 +7,11 @@ services: ' !php/object:': ' !php/object ' ' !php/const:': ' !php/const ' ' !str': ' !!str' + + Rector\Rector\Dynamic\ArgumentRemoverRector: + $argumentChangesByMethodAndType: + - + class: 'Symfony\Component\Yaml\Yaml' + method: 'parse' + position: 2 + value: 'Symfony\Component\Yaml\Yaml::PARSE_KEYS_AS_STRINGS' diff --git a/src/Configuration/Rector/ArgumentRemoverRecipe.php b/src/Configuration/Rector/ArgumentRemoverRecipe.php index 11707908197..dad06155b63 100644 --- a/src/Configuration/Rector/ArgumentRemoverRecipe.php +++ b/src/Configuration/Rector/ArgumentRemoverRecipe.php @@ -4,4 +4,19 @@ namespace Rector\Configuration\Rector; final class ArgumentRemoverRecipe extends AbstractArgumentRecipe { + /** + * @var null|string + */ + private $value; + + public function __construct(string $class, string $method, int $position, ?string $value) + { + parent::__construct($class, $method, $position); + $this->value = $value; + } + + public function getValue(): ?string + { + return $this->value; + } } diff --git a/src/Configuration/Rector/ArgumentRemoverRecipeFactory.php b/src/Configuration/Rector/ArgumentRemoverRecipeFactory.php index 49d1c4d0f0d..00f03da8c85 100644 --- a/src/Configuration/Rector/ArgumentRemoverRecipeFactory.php +++ b/src/Configuration/Rector/ArgumentRemoverRecipeFactory.php @@ -11,6 +11,6 @@ final class ArgumentRemoverRecipeFactory extends AbstractArgumentRecipeFactory { $this->validateArrayData($data); - return new ArgumentRemoverRecipe($data['class'], $data['method'], $data['position']); + return new ArgumentRemoverRecipe($data['class'], $data['method'], $data['position'], $data['value'] ?? null); } } diff --git a/src/Rector/Dynamic/ArgumentRemoverRector.php b/src/Rector/Dynamic/ArgumentRemoverRector.php index 7fa02604cb8..2b1b623cb37 100644 --- a/src/Rector/Dynamic/ArgumentRemoverRector.php +++ b/src/Rector/Dynamic/ArgumentRemoverRector.php @@ -3,11 +3,14 @@ namespace Rector\Rector\Dynamic; use PhpParser\Node; +use PhpParser\Node\Arg; +use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Stmt\ClassMethod; use Rector\Configuration\Rector\ArgumentRemoverRecipe; use Rector\Configuration\Rector\ArgumentRemoverRecipeFactory; +use Rector\Node\Attribute; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; @@ -106,16 +109,38 @@ CODE_SAMPLE } /** - * @param mixed[] $argumentNodes + * @param Arg[] $argumentNodes * @return mixed[] */ private function processArgumentNodes(array $argumentNodes): array { foreach ($this->activeArgumentRemoverRecipes as $activeArgumentRemoverRecipe) { $position = $activeArgumentRemoverRecipe->getPosition(); - unset($argumentNodes[$position]); + + if ($activeArgumentRemoverRecipe->getValue() === null) { + unset($argumentNodes[$position]); + } elseif ($this->isArgumentValueMatch($argumentNodes[$position], $activeArgumentRemoverRecipe)) { + unset($argumentNodes[$position]); + } } return $argumentNodes; } + + private function isArgumentValueMatch(Arg $argNode, ArgumentRemoverRecipe $argumentRemoverRecipe): bool + { + $valueNode = $argNode->value; + + if ($valueNode instanceof ClassConstFetch) { + $valueNodeAsString = $valueNode->class->getAttribute(Attribute::RESOLVED_NAME)->toString() + . '::' + . $valueNode->name->toString(); + + if ($valueNodeAsString === $argumentRemoverRecipe->getValue()) { + return true; + } + } + + return false; + } } diff --git a/tests/Rector/Dynamic/ArgumentRemoverRector/ArgumentRemoverRectorTest.php b/tests/Rector/Dynamic/ArgumentRemoverRector/ArgumentRemoverRectorTest.php index 8368f26b250..472456462b4 100644 --- a/tests/Rector/Dynamic/ArgumentRemoverRector/ArgumentRemoverRectorTest.php +++ b/tests/Rector/Dynamic/ArgumentRemoverRector/ArgumentRemoverRectorTest.php @@ -17,7 +17,8 @@ final class ArgumentRemoverRectorTest extends AbstractRectorTestCase public function provideWrongToFixedFiles(): Iterator { - yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc']; +// 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/Dynamic/ArgumentRemoverRector/Correct/correct2.php.inc b/tests/Rector/Dynamic/ArgumentRemoverRector/Correct/correct2.php.inc new file mode 100644 index 00000000000..fcdf0269c14 --- /dev/null +++ b/tests/Rector/Dynamic/ArgumentRemoverRector/Correct/correct2.php.inc @@ -0,0 +1,13 @@ +