Skip to content

Commit

Permalink
[Arguments] Make ArgumentAdderRector notify user only on changed args (
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 27, 2021
1 parent 7db36d3 commit d8b3be9
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ final class ArgumentAdderRector extends AbstractRector implements ConfigurableRe
*/
private array $addedArguments = [];

private bool $haveArgumentsChanged = false;

public function __construct(
private ArgumentAddingScope $argumentAddingScope
) {
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -180,6 +181,7 @@ private function processPositionWithDefaultValues(
}

$node->args[$position] = $arg;
$this->haveArgumentsChanged = true;
}
}

Expand All @@ -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 {
Expand All @@ -236,6 +231,7 @@ private function addClassMethodParam(
}

$classMethod->params[$position] = $param;
$this->haveArgumentsChanged = true;
}

private function processStaticCall(StaticCall $staticCall, int $position, ArgumentAdder $argumentAdder): void
Expand All @@ -254,5 +250,6 @@ private function processStaticCall(StaticCall $staticCall, int $position, Argume
}

$staticCall->args[$position] = new Arg(new Variable($argumentName));
$this->haveArgumentsChanged = true;
}
}

0 comments on commit d8b3be9

Please sign in to comment.