ChangeAndIfToEarlyReturnRector: fix test expectation in if_in_foreach fixture (#4711)

* fix test expectation in if_in_foreach fixture

* fixed method name which was misleading

* fix variable name

* adjust impl
This commit is contained in:
Markus Staab 2020-11-27 14:59:56 +01:00 committed by GitHub
parent f718d90cd2
commit fe47bc2cc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 4 deletions

View File

@ -122,9 +122,9 @@ CODE_SAMPLE
$this->addNodesAfterNode($ifs, $node);
$this->addNodeAfterNode($ifReturn, $node);
$ifParentReturn = $this->getIfParentReturn($node);
if ($ifParentReturn !== null) {
$this->removeNode($ifParentReturn);
$ifNextReturn = $this->getIfNextReturn($node);
if ($ifNextReturn !== null && !$this->isIfInLoop($node)) {
$this->removeNode($ifNextReturn);
}
$this->removeNode($node);
@ -221,7 +221,7 @@ CODE_SAMPLE
$ifs[0]->setAttribute(AttributeKey::COMMENTS, $nodeComments);
}
private function getIfParentReturn(If_ $if): ?Return_
private function getIfNextReturn(If_ $if): ?Return_
{
$nextNode = $if->getAttribute(AttributeKey::NEXT_NODE);
if (! $nextNode instanceof Return_) {
@ -279,4 +279,11 @@ CODE_SAMPLE
}
return $nextNode instanceof Return_;
}
private function isIfInLoop(If_ $if): bool
{
$parentLoop = $this->betterNodeFinder->findFirstParentInstanceOf($if, [Stmt\Foreach_::class, Stmt\For_::class, Stmt\While_::class]);
return $parentLoop !== null;
}
}

View File

@ -42,6 +42,7 @@ class IfInForeachClass
return;
}
$this->processNameOrIdentifier($node);
return;
}
return;