Skip to content

Commit

Permalink
[Arguments] Remove unnecessary re-create default value param from Con…
Browse files Browse the repository at this point in the history
…stFetch on ArgumentAdderRector, set origNode = null instead (#5113)
  • Loading branch information
samsonasik authored Oct 4, 2023
1 parent b491f42 commit a24035c
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
Expand All @@ -25,8 +24,8 @@
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
Expand All @@ -49,7 +48,6 @@ public function __construct(
private readonly ArgumentAddingScope $argumentAddingScope,
private readonly ChangedArgumentsDetector $changedArgumentsDetector,
private readonly AstResolver $astResolver,
private readonly BetterStandardPrinter $betterStandardPrinter,
private readonly StaticTypeMapper $staticTypeMapper
) {
}
Expand Down Expand Up @@ -208,11 +206,25 @@ private function fillGapBetweenWithDefaultValue(MethodCall | StaticCall $node, i
throw new ShouldNotHappenException('Previous position does not have default value');
}

$default = $this->betterStandardPrinter->print($param->default);
$node->args[$index] = new Arg(new ConstFetch(new Name($default)));
$node->args[$index] = new Arg($this->resolveParamDefault($param->default));
}
}

private function resolveParamDefault(Expr $expr): Expr
{
// reset original node, to allow the printer to re-use the expr
$expr->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$this->traverseNodesWithCallable(
$expr,
static function (Node $node): Node {
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
return $node;
}
);

return $expr;
}

private function shouldSkipParameter(
ClassMethod | MethodCall | StaticCall $node,
ArgumentAdder $argumentAdder
Expand Down

0 comments on commit a24035c

Please sign in to comment.