Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Arguments] Make ArgumentAdderRector notify user only on changed args #780

Merged
merged 1 commit into from
Aug 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}