Skip to content

Commit

Permalink
[EarlyReturn] Skip ChangeOrIfReturnToEarlyReturnRector on next node i…
Browse files Browse the repository at this point in the history
…s null or return void (#796)
  • Loading branch information
samsonasik committed Aug 31, 2021
1 parent 9247ba9 commit 654d4a2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;

Expand Down Expand Up @@ -89,6 +90,15 @@ public function refactor(Node $node): ?Node
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, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 654d4a2

Please sign in to comment.