mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-21 18:42:24 +00:00
[EarlyReturn] Skip left or right is BooleanAnd on ChangeOrIfReturnToEarlyReturnRector (#799)
* [EarlyReturn] Handle left or right is BooleanAnd on ChangeOrIfReturnToEarlyReturnRector * skip * remove tweak in DateTimeToDateTimeInterfaceRector * naming * remove tweak in RemoveAlwaysElseRector * rename fixture
This commit is contained in:
parent
c42b1969ce
commit
2e18cab758
|
@ -97,10 +97,7 @@ CODE_SAMPLE
|
|||
{
|
||||
if ($node instanceof ClassMethod) {
|
||||
$this->refactorClassMethod($node);
|
||||
|
||||
// @see https://github.com/rectorphp/rector-src/pull/794
|
||||
// avoid duplicated ifs and returns when combined with ChangeOrIfReturnToEarlyReturnRector and ChangeAndIfToEarlyReturnRector
|
||||
return null;
|
||||
return $node;
|
||||
}
|
||||
|
||||
return $this->refactorProperty($node);
|
||||
|
|
|
@ -6,6 +6,7 @@ namespace Rector\EarlyReturn\Rector\If_;
|
|||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
|
||||
use PhpParser\Node\Expr\Instanceof_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
|
@ -86,7 +87,7 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
if ($this->isInstanceofCondOnly($node->cond)) {
|
||||
if ($this->isInstanceofCondOnlyOrHasBooleanAnd($node->cond)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -166,16 +167,20 @@ CODE_SAMPLE
|
|||
);
|
||||
}
|
||||
|
||||
private function isInstanceofCondOnly(BooleanOr $booleanOr): bool
|
||||
private function isInstanceofCondOnlyOrHasBooleanAnd(BooleanOr $booleanOr): bool
|
||||
{
|
||||
$currentNode = $booleanOr;
|
||||
|
||||
if ($currentNode->left instanceof BooleanAnd || $currentNode->right instanceof BooleanAnd) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($currentNode->left instanceof BooleanOr) {
|
||||
return $this->isInstanceofCondOnly($currentNode->left);
|
||||
return $this->isInstanceofCondOnlyOrHasBooleanAnd($currentNode->left);
|
||||
}
|
||||
|
||||
if ($currentNode->right instanceof BooleanOr) {
|
||||
return $this->isInstanceofCondOnly($currentNode->right);
|
||||
return $this->isInstanceofCondOnlyOrHasBooleanAnd($currentNode->right);
|
||||
}
|
||||
|
||||
if (! $currentNode->right instanceof Instanceof_) {
|
||||
|
|
|
@ -121,12 +121,6 @@ CODE_SAMPLE
|
|||
|
||||
private function shouldSkip(If_ $if): bool
|
||||
{
|
||||
// to avoid repetitive If_ creation when used along with ChangeOrIfReturnToEarlyReturnRector
|
||||
// @see https://github.com/rectorphp/rector-src/pull/651
|
||||
if ($if->cond instanceof BooleanOr && $if->elseifs !== []) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// to avoid repetitive flipped elseif above return when used along with ChangeAndIfReturnToEarlyReturnRector
|
||||
// @see https://github.com/rectorphp/rector-src/pull/654
|
||||
return $if->cond instanceof BooleanAnd && count($if->elseifs) > 1;
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Issues\IssueEarlyReturnAndOr\Fixture;
|
||||
|
||||
class ComplexAndOr
|
||||
{
|
||||
public function run($a, $b, $c, $d)
|
||||
{
|
||||
if (($a && false === $b) || ! $c) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $d;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Issues\IssueEarlyReturnAndOr\Fixture;
|
||||
|
||||
class ComplexAndOr
|
||||
{
|
||||
public function run($a, $b, $c, $d)
|
||||
{
|
||||
if ($a && false === $b) {
|
||||
return '';
|
||||
}
|
||||
if (! $c) {
|
||||
return '';
|
||||
}
|
||||
return $d;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Issues\IssueEarlyReturnAndOr\Fixture;
|
||||
|
||||
class SkipAndNextOr
|
||||
{
|
||||
public function run($a, $b, $c, $d)
|
||||
{
|
||||
if (($a && false === $b) || ! $c) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $d;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,59 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Issues\IssueEarlyReturnAndOrDatetime\Fixture;
|
||||
|
||||
class AndNextOr
|
||||
{
|
||||
public function run($a, $b, $c, $d)
|
||||
{
|
||||
if ($a && $b || $c) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$a = 1;
|
||||
$b = 2;
|
||||
|
||||
if ($b > $a) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$d = 1;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Issues\IssueEarlyReturnAndOrDatetime\Fixture;
|
||||
|
||||
class AndNextOr
|
||||
{
|
||||
public function run($a, $b, $c, $d)
|
||||
{
|
||||
if ($a && $b) {
|
||||
return null;
|
||||
}
|
||||
if ($c) {
|
||||
return null;
|
||||
}
|
||||
$a = 1;
|
||||
$b = 2;
|
||||
|
||||
if ($b > $a) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$d = 1;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Issues\IssueEarlyReturnAndOrDatetime\Fixture;
|
||||
|
||||
class SkipAndNextOr
|
||||
{
|
||||
public function run($a, $b, $c, $d)
|
||||
{
|
||||
if ($a && $b || $c) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$a = 1;
|
||||
$b = 2;
|
||||
|
||||
if ($b > $a) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$d = 1;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Issues\IssueReturnBeforeElseIf\Fixture;
|
||||
|
||||
class ComplexIfCondOr
|
||||
{
|
||||
public function run($a, $b, $c, $d, $e)
|
||||
{
|
||||
if (($a && false === $b) || ! $c) {
|
||||
return '';
|
||||
} elseif ($d) {
|
||||
return '';
|
||||
}
|
||||
return $e;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Issues\IssueReturnBeforeElseIf\Fixture;
|
||||
|
||||
class ComplexIfCondOr
|
||||
{
|
||||
public function run($a, $b, $c, $d, $e)
|
||||
{
|
||||
if (($a && false === $b) || ! $c) {
|
||||
return '';
|
||||
}
|
||||
if ($d) {
|
||||
return '';
|
||||
}
|
||||
return $e;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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';
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Issues\IssueReturnBeforeElseIf\Fixture;
|
||||
|
||||
class SkipComplexIfCondOr
|
||||
{
|
||||
public function run($a, $b, $c, $d, $e)
|
||||
{
|
||||
if (($a && false === $b) || ! $c) {
|
||||
return '';
|
||||
} elseif ($d) {
|
||||
return '';
|
||||
}
|
||||
return $e;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user