Skip to content
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

Merged
merged 1 commit into from
Mar 21, 2025
Merged

Conversation

carlos-granados
Copy link
Contributor

Derived from #6794

@@ -86,8 +86,9 @@ public function refactor(Node $node): ?Node
continue;
}

$this->wrap($classMethod, $typeMethodWrap->isArrayWrap());
$hasChanged = true;
if ($typeMethodWrap->isArrayWrap() && $this->wrap($classMethod)) {
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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;
Copy link
Contributor Author

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;
Copy link
Contributor Author

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

@samsonasik samsonasik merged commit cf82f6a into rectorphp:main Mar 21, 2025
44 checks passed
@samsonasik
Copy link
Member

Thank you @carlos-granados

@carlos-granados carlos-granados deleted the fix-WrapReturnRector branch March 21, 2025 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants