Add ShortenElseIfRector#2094
Conversation
c393833 to
0196d1e
Compare
TomasVotruba
left a comment
There was a problem hiding this comment.
Very nice job!
I've added few comments
| return null; | ||
| } | ||
|
|
||
| $if = $this->refactor($if) ?? $if; |
There was a problem hiding this comment.
This is pretty difficult to understand recursion.
Could you think of more readable syntax?
There was a problem hiding this comment.
Removed the coalesc and made it more verbose,
hopefully that makes it clearer
Do you have any better ideas in mind how to clarify this?
There was a problem hiding this comment.
I'm not sure why have you chosen this approach, so I don't even understand the idea to describe it
I always try to avoid calling the self or another Rector in a Rector, but rather decopule a method with meaning captured in it's name
There was a problem hiding this comment.
Ok the reason behind the recursion is that we get rid of the original nested If_ node, but this rector runs only on If_ nodes.
This means after refactoring the top-level If_ we won't detect any more else/if condition that we could refactor inside that if statement, because now it is an Elseif_ Node.
Consider the example from the recursive fixture:
public function run()
{
if ($this->cond1) {
$this->doSomething();
} else {
if ($this->cond2) {
$this->doSomething();
} else {
if ($this->cond3) {
$this->doSomething();
}
}
}
}In that case we would move the if ($this->cond2) statement up and combine it to the elseif ($this->cond2). By this refactoring our rector doesn't run for the nested if ($this->cond3), it would get refactored if we run the rector a second time.
Therefore we have to run the refactoring on the inner if ($this->cond3) before applying the refactoring on the outer if ($this->cond3).
I hope this clarifies the need for recursion, i'll try to express this inside the code
There was a problem hiding this comment.
I see
I hope this clarifies the need for recursion, i'll try to express this inside the code
That would be perfect, so this comment is not needed to understand the mechanism
| */ | ||
| public function refactor(Node $node): ?Node | ||
| { | ||
| return $this->shortenElseIf($node); |
rectorphp/rector-src@8e51a6c [Php74] Skip array type on trait on RestoreDefaultNullToNullableTypePropertyRector (#2094)
Fixes #1945