-
-
Notifications
You must be signed in to change notification settings - Fork 379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix WrapReturnRector #6798
Fix WrapReturnRector #6798
Conversation
@@ -86,8 +86,9 @@ public function refactor(Node $node): ?Node | |||
continue; | |||
} | |||
|
|||
$this->wrap($classMethod, $typeMethodWrap->isArrayWrap()); | |||
$hasChanged = true; | |||
if ($typeMethodWrap->isArrayWrap() && $this->wrap($classMethod)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of passing the isArrayWrap() value to the function, if it is not true we just don't call the function
@@ -107,22 +108,21 @@ public function configure(array $configuration): void | |||
$this->typeMethodWraps = $configuration; | |||
} | |||
|
|||
private function wrap(ClassMethod $classMethod, bool $isArrayWrap): ?ClassMethod | |||
private function wrap(ClassMethod $classMethod): bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of returning the ClassMethod (which was not used), we return a boolean indicating if the value has changed
$classMethod->stmts[$key] = $stmt; | ||
$hasChanged = false; | ||
foreach ($classMethod->stmts as $stmt) { | ||
if ($stmt instanceof Return_ && $stmt->expr instanceof Expr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simplify the if statement into a single statement
if ($stmt instanceof Return_ && $stmt->expr instanceof Expr | ||
&& ! $stmt->expr instanceof Array_) { | ||
$stmt->expr = new Array_([new ArrayItem($stmt->expr)]); | ||
$hasChanged = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only when we actually change the return expression we mark the node as changed
$stmt->expr = new Array_([new ArrayItem($stmt->expr)]); | ||
} | ||
|
||
$classMethod->stmts[$key] = $stmt; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not needed, this code just replaces a statement with itself
Thank you @carlos-granados |
Derived from #6794