Skip to content

Commit

Permalink
[NodeFactory] Add new method: createReprintedExpr() to create re-prin…
Browse files Browse the repository at this point in the history
…ted Expr that can be used to pull default Expr from Param to be re-used (#5114)

* [NodeFactory] Add new method: createReprintedExpr() to create re-printed Expr that can be used to pull default Expr from Param to be re-used

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Oct 4, 2023
1 parent a24035c commit fe2c85c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 45 deletions.
18 changes: 1 addition & 17 deletions rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\AstResolver;
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 Down Expand Up @@ -206,25 +205,10 @@ private function fillGapBetweenWithDefaultValue(MethodCall | StaticCall $node, i
throw new ShouldNotHappenException('Previous position does not have default value');
}

$node->args[$index] = new Arg($this->resolveParamDefault($param->default));
$node->args[$index] = new Arg($this->nodeFactory->createReprintedExpr($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
12 changes: 1 addition & 11 deletions rules/Php72/NodeFactory/AnonymousFunctionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,17 +301,7 @@ private function resolveParamDefaultExpr(
return null;
}

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

return $paramDefaultExpr;
return $this->nodeFactory->createReprintedExpr($paramDefaultExpr);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ function (Node $subNode) use ($parentClassMethodParam): bool {
$paramDefault = $parentClassMethodParam->default;

if ($paramDefault instanceof Expr) {
$paramDefault = $this->resolveParamDefault($paramDefault);
$paramDefault = $this->nodeFactory->createReprintedExpr($paramDefault);
}

$paramName = $this->nodeNameResolver->getName($parentClassMethodParam);
Expand All @@ -239,21 +239,6 @@ function (Node $subNode) use ($parentClassMethodParam): bool {
return $node;
}

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 resolveParamType(Param $param): null|Identifier|Name|ComplexType
{
if ($param->type === null) {
Expand Down
20 changes: 19 additions & 1 deletion src/PhpParser/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
use Rector\Core\Exception\NotImplementedYetException;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeDecorator\PropertyTypeDecorator;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\PostRector\ValueObject\PropertyMetadata;
use Rector\StaticTypeMapper\StaticTypeMapper;
Expand All @@ -61,7 +63,8 @@ public function __construct(
private readonly BuilderFactory $builderFactory,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly StaticTypeMapper $staticTypeMapper,
private readonly PropertyTypeDecorator $propertyTypeDecorator
private readonly PropertyTypeDecorator $propertyTypeDecorator,
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser
) {
}

Expand Down Expand Up @@ -432,4 +435,19 @@ private function createMethodCaller(

return $exprOrVariableName;
}

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

return $expr;
}
}

0 comments on commit fe2c85c

Please sign in to comment.