Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector\Fixture;

class AndInNext
{
public function accept()
{
return $this->something() || $this->somethingelse() && $this->anotherelse() || $this->last();
}
}

?>
-----
<?php

namespace Rector\Tests\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector\Fixture;

class AndInNext
{
public function accept()
{
if ($this->something()) {
return true;
}
if ($this->somethingelse() && $this->anotherelse()) {
return true;
}
return (bool) $this->last();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Tests\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector\Fixture;

class AndNestedDeep
{
public function hasStale(): bool
{
return $this->donation === null
|| ($this->commission > 0 && $this->transaction === null)
|| $this->somethingElse()
|| $this->anotherThing();
}
}

?>
-----
<?php

namespace Rector\Tests\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector\Fixture;

class AndNestedDeep
{
public function hasStale(): bool
{
if ($this->donation === null) {
return true;
}
if ($this->commission > 0 && $this->transaction === null) {
return true;
}
if ($this->somethingElse()) {
return true;
}
return (bool) $this->anotherThing();
}
}

?>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
Expand Down Expand Up @@ -89,12 +88,15 @@ public function refactor(Node $node): ?Node

$booleanOr = $stmt->expr;

// the right side becomes the final return, the left operands become early returns
$left = $booleanOr->left;
$ifs = $this->createMultipleIfs($left, $stmt, []);
$leftOperands = $left instanceof BooleanOr ? $this->flattenBooleanOr($left) : [$left];

// ensure ifs not removed by other rules
if ($ifs === []) {
continue;
$ifs = [];
foreach ($leftOperands as $leftOperand) {
$ifs[] = new If_($leftOperand, [
'stmts' => [new Return_($this->nodeFactory->createTrue())],
]);
}

if (! $this->callAnalyzer->doesIfHasObjectCall($ifs)) {
Expand All @@ -119,56 +121,17 @@ public function refactor(Node $node): ?Node
}

/**
* @param If_[] $ifs
* @return If_[]
*/
private function createMultipleIfs(Expr $expr, Return_ $return, array $ifs): array
{
while ($expr instanceof BooleanOr) {
$ifs = [...$ifs, ...$this->collectLeftBooleanOrToIfs($expr, $return, $ifs)];
$ifs[] = new If_($expr->right, [
'stmts' => [new Return_($this->nodeFactory->createTrue())],
]);

$expr = $expr->right;
if ($expr instanceof BooleanAnd) {
return [];
}

if (! $expr instanceof BooleanOr) {
continue;
}

return [];
}

$lastIf = new If_($expr, [
'stmts' => [new Return_($this->nodeFactory->createTrue())],
]);

// if empty, fallback to last if
if ($ifs === []) {
return [$lastIf];
}

return $ifs;
}

/**
* @param If_[] $ifs
* @return If_[]
* Flatten a left-associative "||" chain into its operands, in source order.
*
* @return Expr[]
*/
private function collectLeftBooleanOrToIfs(BooleanOr $booleanOr, Return_ $return, array $ifs): array
private function flattenBooleanOr(BooleanOr $booleanOr): array
{
$left = $booleanOr->left;
if (! $left instanceof BooleanOr) {
$returnTrueIf = new If_($left, [
'stmts' => [new Return_($this->nodeFactory->createTrue())],
]);

return [$returnTrueIf];
}
$operands = $left instanceof BooleanOr ? $this->flattenBooleanOr($left) : [$left];
$operands[] = $booleanOr->right;

return $this->createMultipleIfs($left, $return, $ifs);
return $operands;
}
}
Loading