Skip to content

Commit

Permalink
Skip argument if value equals default value (#4368)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefantalen committed Jun 27, 2023
1 parent c93176f commit b408d9e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Arguments\Rector\ClassMethod\ArgumentAdderRector\Fixture;

use Rector\Tests\Arguments\Rector\ClassMethod\ArgumentAdderRector\Source\SomeClass;

class SkipAddWithDefaultValue
{
public function skip(): void
{
$someClass = new SomeClass();
$someClass->someMethod();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@

class SomeClass
{
public function someMethod($default = 1): void
{
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@
),
new ArgumentAdder(SomeClass::class, 'withoutTypeOrDefaultValue', 0, 'arguments', [], $arrayType),
new ArgumentAdder(SomeMultiArg::class, 'run', 2, 'c', 4),
new ArgumentAdder(SomeClass::class, 'someMethod', 0, 'default', 1),
]);
};
11 changes: 11 additions & 0 deletions rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,17 @@ private function shouldSkipParameter(
return true;
}

// Check if default value is the same
$classMethod = $this->astResolver->resolveClassMethodFromCall($node);
if (($classMethod instanceof ClassMethod) &&
isset($classMethod->params[$position]) &&
! $this->changedArgumentsDetector->isDefaultValueChanged(
$classMethod->params[$position],
$argumentAdder->getArgumentDefaultValue()
)) {
return true;
}

// is correct scope?
return ! $this->argumentAddingScope->isInCorrectScope($node, $argumentAdder);
}
Expand Down

0 comments on commit b408d9e

Please sign in to comment.