diff --git a/rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php b/rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php index 6550af97f72..65273bcf805 100644 --- a/rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php +++ b/rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php @@ -42,6 +42,8 @@ final class ArgumentAdderRector extends AbstractRector implements ConfigurableRe */ private array $addedArguments = []; + private bool $haveArgumentsChanged = false; + public function __construct( private ArgumentAddingScope $argumentAddingScope ) { @@ -62,17 +64,7 @@ public function getRuleDefinition(): RuleDefinition <<<'CODE_SAMPLE' $someObject = new SomeExampleClass; $someObject->someMethod(); -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -$someObject = new SomeExampleClass; -$someObject->someMethod(true); -CODE_SAMPLE - , - $exampleConfiguration - ), - new ConfiguredCodeSample( - <<<'CODE_SAMPLE' + class MyCustomClass extends SomeExampleClass { public function someMethod() @@ -82,6 +74,9 @@ public function someMethod() CODE_SAMPLE , <<<'CODE_SAMPLE' +$someObject = new SomeExampleClass; +$someObject->someMethod(true); + class MyCustomClass extends SomeExampleClass { public function someMethod($value = true) @@ -107,8 +102,10 @@ public function getNodeTypes(): array /** * @param MethodCall|StaticCall|ClassMethod $node */ - public function refactor(Node $node): MethodCall | StaticCall | ClassMethod + public function refactor(Node $node): MethodCall | StaticCall | ClassMethod | null { + $this->haveArgumentsChanged = false; + foreach ($this->addedArguments as $addedArgument) { if (! $this->isObjectTypeMatch($node, $addedArgument->getObjectType())) { continue; @@ -121,7 +118,11 @@ public function refactor(Node $node): MethodCall | StaticCall | ClassMethod $this->processPositionWithDefaultValues($node, $addedArgument); } - return $node; + if ($this->haveArgumentsChanged) { + return $node; + } + + return null; } /** @@ -180,6 +181,7 @@ private function processPositionWithDefaultValues( } $node->args[$position] = $arg; + $this->haveArgumentsChanged = true; } } @@ -203,25 +205,18 @@ private function shouldSkipParameter( return $this->isName($node->params[$position], $argumentName); } - // already added? - if (! isset($node->args[$position])) { - // is correct scope? - return ! $this->argumentAddingScope->isInCorrectScope($node, $argumentAdder); - } - if (! $this->isName($node->args[$position], $argumentName)) { - // is correct scope? - return ! $this->argumentAddingScope->isInCorrectScope($node, $argumentAdder); + if (isset($node->args[$position])) { + return true; } - return true; + + // is correct scope? + return ! $this->argumentAddingScope->isInCorrectScope($node, $argumentAdder); } - /** - * @param mixed $defaultValue - */ private function addClassMethodParam( ClassMethod $classMethod, ArgumentAdder $argumentAdder, - $defaultValue, + mixed $defaultValue, ?string $type, int $position ): void { @@ -236,6 +231,7 @@ private function addClassMethodParam( } $classMethod->params[$position] = $param; + $this->haveArgumentsChanged = true; } private function processStaticCall(StaticCall $staticCall, int $position, ArgumentAdder $argumentAdder): void @@ -254,5 +250,6 @@ private function processStaticCall(StaticCall $staticCall, int $position, Argume } $staticCall->args[$position] = new Arg(new Variable($argumentName)); + $this->haveArgumentsChanged = true; } }