[EarlyReturn] Skip ChangeOrIfReturnToEarlyReturnRector on next node is null or return void (#796)

This commit is contained in:
Abdul Malik Ikhsan 2021-08-31 19:20:06 +07:00 committed by GitHub
parent 9247ba932a
commit 654d4a2a2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 8 deletions

View File

@ -12,6 +12,7 @@ use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -89,6 +90,15 @@ CODE_SAMPLE
return null;
}
$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE);
if ($nextNode === null) {
return null;
}
if ($nextNode instanceof Return_ && $nextNode->expr === null) {
return null;
}
/** @var Return_ $return */
$return = $node->stmts[0];
$ifs = $this->createMultipleIfs($node->cond, $return, []);

View File

@ -31,12 +31,10 @@ class AndNextOrReturnVoid
*/
public function run($a, $b, $c, $d)
{
if ($a && $b) {
return null;
}
if ($c) {
if ($a && $b || $c) {
return null;
}
return;
}
}

View File

@ -28,10 +28,7 @@ class ComplexIfCondOrWithoutElseIf
{
public function run($a, $b, $c)
{
if ($a && false === $b) {
return 'a';
}
if (! $c) {
if (($a && false === $b) || ! $c) {
return 'a';
}
return 'b';