Skip to content

Commit

Permalink
Recreate to exact Expr node instead of printing when possible on AddP…
Browse files Browse the repository at this point in the history
…aramBasedOnParentClassMethodRector take 2 (#4842)

* Recreate to exact Expr node instead of printing when possible on AddParamBasedOnParentClassMethodRector take 2

* [ci-review] Rector Rectify

* double quote fixture

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Aug 24, 2023
1 parent 295156c commit 21f33de
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ExtendsParentDefaultEmptyArrayString extends ParentWithParamWithDefaultVal
public function emptyString($default = '') {
}

public function emptyString2($default = '') {
public function emptyString2($default = "") {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class ExtendsParentDefaultEmptyArrayString extends ParentWithParamWithDefaultVal

public function nonEmptyString() {
}

public function nonEmptyString2() {
}
}

?>
Expand All @@ -26,6 +29,9 @@ class ExtendsParentDefaultEmptyArrayString extends ParentWithParamWithDefaultVal

public function nonEmptyString($default = 'some value') {
}

public function nonEmptyString2($default = "some value") {
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public function nonEmptyString($default = 'some value') {
return $default;
}

public function nonEmptyString2($default = "some value") {
return $default;
}

public function intParam($default = 123) {
return $default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ function (Node $subNode) use ($parentClassMethodParam): bool {
private function resolveParamDefault(Expr $expr): Expr
{
// re-create to avoid TokenStream error
if ($expr instanceof String_ && $expr->value === '') {
return new String_($expr->value);
if ($expr instanceof String_) {
return new String_($expr->value, [AttributeKey::KIND => $expr->getAttribute(AttributeKey::KIND)]);
}

if ($expr instanceof LNumber) {
Expand All @@ -257,11 +257,11 @@ private function resolveParamDefault(Expr $expr): Expr
return new DNumber($expr->value);
}

$printParamDefault = $this->betterStandardPrinter->print($expr);
if ($printParamDefault === '[]') {
return new Array_([]);
if ($expr instanceof Array_ && $expr->items === []) {
return new Array_($expr->items, [AttributeKey::KIND => $expr->getAttribute(AttributeKey::KIND)]);
}

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

Expand Down

0 comments on commit 21f33de

Please sign in to comment.