Skip to content

Commit

Permalink
[Php80] Remove unnecessary re-create default value param from ConstFe…
Browse files Browse the repository at this point in the history
…tch on AddParamBasedOnParentClassMethodRector, set origNode = null instead (#5112)
  • Loading branch information
samsonasik committed Oct 4, 2023
1 parent 4e56b7c commit b491f42
Showing 1 changed file with 10 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@
use PhpParser\Node;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\MethodReflection;
use Rector\Core\PhpParser\AstResolver;
Expand Down Expand Up @@ -246,29 +241,17 @@ function (Node $subNode) use ($parentClassMethodParam): bool {

private function resolveParamDefault(Expr $expr): Expr
{
// re-create to avoid TokenStream error
if ($expr instanceof String_) {
return new String_($expr->value, [
AttributeKey::KIND => $expr->getAttribute(AttributeKey::KIND),
]);
}

if ($expr instanceof LNumber) {
return new LNumber($expr->value);
}

if ($expr instanceof DNumber) {
return new DNumber($expr->value);
}

if ($expr instanceof Array_ && $expr->items === []) {
return new Array_($expr->items, [
AttributeKey::KIND => $expr->getAttribute(AttributeKey::KIND),
]);
}
// 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;
}
);

$printParamDefault = $this->betterStandardPrinter->print($expr);
return new ConstFetch(new Name($printParamDefault));
return $expr;
}

private function resolveParamType(Param $param): null|Identifier|Name|ComplexType
Expand Down

0 comments on commit b491f42

Please sign in to comment.