diff --git a/rules/CodeQuality/Rector/ClassMethod/DateTimeToDateTimeInterfaceRector.php b/rules/CodeQuality/Rector/ClassMethod/DateTimeToDateTimeInterfaceRector.php index 5390d9cabcc..9db07b6ed32 100644 --- a/rules/CodeQuality/Rector/ClassMethod/DateTimeToDateTimeInterfaceRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/DateTimeToDateTimeInterfaceRector.php @@ -97,10 +97,7 @@ public function refactor(Node $node): ?Node { 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); diff --git a/rules/EarlyReturn/Rector/If_/ChangeOrIfReturnToEarlyReturnRector.php b/rules/EarlyReturn/Rector/If_/ChangeOrIfReturnToEarlyReturnRector.php index 24404a87841..a9edd1cf90a 100644 --- a/rules/EarlyReturn/Rector/If_/ChangeOrIfReturnToEarlyReturnRector.php +++ b/rules/EarlyReturn/Rector/If_/ChangeOrIfReturnToEarlyReturnRector.php @@ -6,6 +6,7 @@ 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 @@ public function refactor(Node $node): ?Node return null; } - if ($this->isInstanceofCondOnly($node->cond)) { + if ($this->isInstanceofCondOnlyOrHasBooleanAnd($node->cond)) { return null; } @@ -166,16 +167,20 @@ private function createIf(Expr $expr, Return_ $return): If_ ); } - 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_) { diff --git a/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php b/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php index aac957ea090..63afdaf5c8f 100644 --- a/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php +++ b/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php @@ -121,12 +121,6 @@ public function refactor(Node $node): ?Node 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; diff --git a/tests/Issues/IssueEarlyReturnAndOr/Fixture/fixture.php.inc b/tests/Issues/IssueEarlyReturnAndOr/Fixture/fixture.php.inc deleted file mode 100644 index 33d799f2e29..00000000000 --- a/tests/Issues/IssueEarlyReturnAndOr/Fixture/fixture.php.inc +++ /dev/null @@ -1,41 +0,0 @@ - ------ - diff --git a/tests/Issues/IssueEarlyReturnAndOr/Fixture/skip_next_or.php.inc b/tests/Issues/IssueEarlyReturnAndOr/Fixture/skip_next_or.php.inc new file mode 100644 index 00000000000..c44143492fd --- /dev/null +++ b/tests/Issues/IssueEarlyReturnAndOr/Fixture/skip_next_or.php.inc @@ -0,0 +1,19 @@ + diff --git a/tests/Issues/IssueEarlyReturnAndOrDatetime/Fixture/and_next_or.php.inc b/tests/Issues/IssueEarlyReturnAndOrDatetime/Fixture/and_next_or.php.inc deleted file mode 100644 index a4830d5fe18..00000000000 --- a/tests/Issues/IssueEarlyReturnAndOrDatetime/Fixture/and_next_or.php.inc +++ /dev/null @@ -1,59 +0,0 @@ - $a) { - return null; - } - - $d = 1; - - return; - } -} - -?> ------ - $a) { - return null; - } - - $d = 1; - - return; - } -} - -?> diff --git a/tests/Issues/IssueEarlyReturnAndOrDatetime/Fixture/skip_and_next_or.php.inc b/tests/Issues/IssueEarlyReturnAndOrDatetime/Fixture/skip_and_next_or.php.inc new file mode 100644 index 00000000000..8c829e9912f --- /dev/null +++ b/tests/Issues/IssueEarlyReturnAndOrDatetime/Fixture/skip_and_next_or.php.inc @@ -0,0 +1,28 @@ + $a) { + return null; + } + + $d = 1; + + return; + } +} + +?> diff --git a/tests/Issues/IssueReturnBeforeElseIf/Fixture/complex_if_cond_or.php.inc b/tests/Issues/IssueReturnBeforeElseIf/Fixture/complex_if_cond_or.php.inc new file mode 100644 index 00000000000..2852e78dcbf --- /dev/null +++ b/tests/Issues/IssueReturnBeforeElseIf/Fixture/complex_if_cond_or.php.inc @@ -0,0 +1,42 @@ + +----- + \ No newline at end of file diff --git a/tests/Issues/IssueReturnBeforeElseIf/Fixture/complex_if_cond_or_without_elseif.php.inc b/tests/Issues/IssueReturnBeforeElseIf/Fixture/complex_if_cond_or_without_elseif.php.inc index ebc16f0e5db..423b6ffe982 100644 --- a/tests/Issues/IssueReturnBeforeElseIf/Fixture/complex_if_cond_or_without_elseif.php.inc +++ b/tests/Issues/IssueReturnBeforeElseIf/Fixture/complex_if_cond_or_without_elseif.php.inc @@ -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'; diff --git a/tests/Issues/IssueReturnBeforeElseIf/Fixture/skip_complex_if_cond_or.php.inc b/tests/Issues/IssueReturnBeforeElseIf/Fixture/skip_complex_if_cond_or.php.inc deleted file mode 100644 index 33e76d89f76..00000000000 --- a/tests/Issues/IssueReturnBeforeElseIf/Fixture/skip_complex_if_cond_or.php.inc +++ /dev/null @@ -1,20 +0,0 @@ -