-
-
Notifications
You must be signed in to change notification settings - Fork 439
Fix WrapReturnRector #6798
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,8 +86,9 @@ public function refactor(Node $node): ?Node | |
| continue; | ||
| } | ||
|
|
||
| $this->wrap($classMethod, $typeMethodWrap->isArrayWrap()); | ||
| $hasChanged = true; | ||
| if ($typeMethodWrap->isArrayWrap() && $this->wrap($classMethod)) { | ||
| $hasChanged = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -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 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| { | ||
| if (! is_iterable($classMethod->stmts)) { | ||
| return null; | ||
| return false; | ||
| } | ||
|
|
||
| foreach ($classMethod->stmts as $key => $stmt) { | ||
| if ($stmt instanceof Return_ && $stmt->expr instanceof Expr) { | ||
| if ($isArrayWrap && ! $stmt->expr instanceof Array_) { | ||
| $stmt->expr = new Array_([new ArrayItem($stmt->expr)]); | ||
| } | ||
|
|
||
| $classMethod->stmts[$key] = $stmt; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not needed, this code just replaces a statement with itself |
||
| $hasChanged = false; | ||
| foreach ($classMethod->stmts as $stmt) { | ||
| if ($stmt instanceof Return_ && $stmt->expr instanceof Expr | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplify the if statement into a single statement |
||
| && ! $stmt->expr instanceof Array_) { | ||
| $stmt->expr = new Array_([new ArrayItem($stmt->expr)]); | ||
| $hasChanged = true; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| } | ||
| } | ||
|
|
||
| return $classMethod; | ||
| return $hasChanged; | ||
| } | ||
| } | ||
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