diff --git a/build/target-repository/docs/rector_rules_overview.md b/build/target-repository/docs/rector_rules_overview.md index df44cea20ce..ba12dc54cd1 100644 --- a/build/target-repository/docs/rector_rules_overview.md +++ b/build/target-repository/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 362 Rules Overview +# 361 Rules Overview
@@ -12,7 +12,7 @@ - [DeadCode](#deadcode) (42) -- [EarlyReturn](#earlyreturn) (10) +- [EarlyReturn](#earlyreturn) (9) - [Naming](#naming) (6) @@ -3377,29 +3377,6 @@ Split if statement, when if condition always break execution flow
-### ReturnBinaryAndToEarlyReturnRector - -Changes Single return of && to early returns - -- class: [`Rector\EarlyReturn\Rector\Return_\ReturnBinaryAndToEarlyReturnRector`](../rules/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector.php) - -```diff - class SomeClass - { - public function accept() - { -- return $this->something() && $this->somethingelse(); -+ if (! $this->something()) { -+ return false; -+ } -+ -+ return (bool) $this->somethingelse(); - } - } -``` - -
- ### ReturnBinaryOrToEarlyReturnRector Changes Single return of || to early returns diff --git a/config/set/early-return.php b/config/set/early-return.php index 1a970a07227..976b6047d38 100644 --- a/config/set/early-return.php +++ b/config/set/early-return.php @@ -10,7 +10,6 @@ use Rector\EarlyReturn\Rector\If_\ChangeOrIfContinueToMultiContinueRector; use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector; use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector; -use Rector\EarlyReturn\Rector\Return_\ReturnBinaryAndToEarlyReturnRector; use Rector\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector; use Rector\EarlyReturn\Rector\StmtsAwareInterface\ReturnEarlyIfVariableRector; @@ -21,7 +20,6 @@ ChangeIfElseValueAssignToEarlyReturnRector::class, ChangeNestedIfsToEarlyReturnRector::class, RemoveAlwaysElseRector::class, - ReturnBinaryAndToEarlyReturnRector::class, ChangeOrIfContinueToMultiContinueRector::class, PreparedValueToEarlyReturnRector::class, ReturnBinaryOrToEarlyReturnRector::class, diff --git a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/comment.php.inc b/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/comment.php.inc deleted file mode 100644 index 7f2bd67291d..00000000000 --- a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/comment.php.inc +++ /dev/null @@ -1,44 +0,0 @@ -something() && $this->somethingElse(); - } - - // another next comment - return 1; - } -} - -?> ------ -something()) { - return false; - } - return (bool) $this->somethingElse(); - } - - // another next comment - return 1; - } -} - -?> diff --git a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/dont_change_already_casted.php.inc b/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/dont_change_already_casted.php.inc deleted file mode 100644 index e3fef64aa54..00000000000 --- a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/dont_change_already_casted.php.inc +++ /dev/null @@ -1,30 +0,0 @@ -something() && (bool) $this->somethingelse(); - } -} - -?> ------ -something()) { - return false; - } - return (bool) $this->somethingelse(); - } -} - -?> diff --git a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/dont_change_already_return_typed.php.inc b/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/dont_change_already_return_typed.php.inc deleted file mode 100644 index 498c5bf25bf..00000000000 --- a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/dont_change_already_return_typed.php.inc +++ /dev/null @@ -1,50 +0,0 @@ -something() && $this->somethingElse(); - } - - private function something(): bool - { - return true; - } - - private function somethingElse(): bool - { - return true; - } -} - -?> ------ -something()) { - return false; - } - return $this->somethingElse(); - } - - private function something(): bool - { - return true; - } - - private function somethingElse(): bool - { - return true; - } -} - -?> diff --git a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/fixture.php.inc b/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/fixture.php.inc deleted file mode 100644 index 9872f9da134..00000000000 --- a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,30 +0,0 @@ -something() && $this->somethingElse(); - } -} - -?> ------ -something()) { - return false; - } - return (bool) $this->somethingElse(); - } -} - -?> diff --git a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/identical.php.inc b/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/identical.php.inc deleted file mode 100644 index a9ff86a4c62..00000000000 --- a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/identical.php.inc +++ /dev/null @@ -1,30 +0,0 @@ -something() === 1 && !$this->somethingElse(); - } -} - -?> ------ -something() !== 1) { - return false; - } - return !$this->somethingElse(); - } -} - -?> diff --git a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/last_return_bool.php.inc b/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/last_return_bool.php.inc deleted file mode 100644 index dd0c9c3e7ee..00000000000 --- a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/last_return_bool.php.inc +++ /dev/null @@ -1,53 +0,0 @@ -something() && $this->getSomethingElse(); - } - - public function accept2() - { - return $this->something() && $this->somethingelse() === 'something else'; - } - - private function getSomethingElse(): bool - { - return true; - } -} - -?> ------ -something()) { - return false; - } - return $this->getSomethingElse(); - } - - public function accept2() - { - if (!$this->something()) { - return false; - } - return $this->somethingelse() === 'something else'; - } - - private function getSomethingElse(): bool - { - return true; - } -} - -?> diff --git a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/multiple_binary_and.php.inc b/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/multiple_binary_and.php.inc deleted file mode 100644 index d50a49c8bf6..00000000000 --- a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/multiple_binary_and.php.inc +++ /dev/null @@ -1,36 +0,0 @@ -something() && $this->somethingelse() && $this->anotherelse() && $this->last(); - } -} - -?> ------ -something()) { - return false; - } - if (!$this->somethingelse()) { - return false; - } - if (!$this->anotherelse()) { - return false; - } - return (bool) $this->last(); - } -} - -?> diff --git a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/multiple_binary_and_start_with_method_call.php.inc b/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/multiple_binary_and_start_with_method_call.php.inc deleted file mode 100644 index a5aecabef22..00000000000 --- a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/multiple_binary_and_start_with_method_call.php.inc +++ /dev/null @@ -1,37 +0,0 @@ -execute() && $a && $b; - } - - private function execute() {} -} - -?> ------ -execute()) { - return false; - } - if (!$a) { - return false; - } - return (bool) $b; - } - - private function execute() {} -} - -?> diff --git a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/not_object_call_in_last.php.inc b/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/not_object_call_in_last.php.inc deleted file mode 100644 index faddd806d46..00000000000 --- a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/not_object_call_in_last.php.inc +++ /dev/null @@ -1,30 +0,0 @@ -something() && true; - } -} - -?> ------ -something()) { - return false; - } - return true; - } -} - -?> diff --git a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/skip_not_object_call.php.inc b/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/skip_not_object_call.php.inc deleted file mode 100644 index cef6a678b8f..00000000000 --- a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/skip_not_object_call.php.inc +++ /dev/null @@ -1,13 +0,0 @@ -something || $this->somethingelse; - } -} - -?> diff --git a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/skip_or_in_next.php.inc b/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/skip_or_in_next.php.inc deleted file mode 100644 index 9203dcf9d29..00000000000 --- a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/skip_or_in_next.php.inc +++ /dev/null @@ -1,13 +0,0 @@ -something() && $this->somethingelse() || $this->anotherelse() && $this->last(); - } -} - -?> diff --git a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/some_not_identical.php.inc b/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/some_not_identical.php.inc deleted file mode 100644 index 54b16bd9fc8..00000000000 --- a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/some_not_identical.php.inc +++ /dev/null @@ -1,30 +0,0 @@ -something() !== 1 && !$this->somethingelse(); - } -} - -?> ------ -something() === 1) { - return false; - } - return !$this->somethingelse(); - } -} - -?> diff --git a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/truthy_negation.php.inc b/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/truthy_negation.php.inc deleted file mode 100644 index 705d41edd6b..00000000000 --- a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/Fixture/truthy_negation.php.inc +++ /dev/null @@ -1,30 +0,0 @@ -something() && !$this->somethingelse(); - } -} - -?> ------ -something()) { - return false; - } - return !$this->somethingelse(); - } -} - -?> diff --git a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/ReturnBinaryAndToEarlyReturnRectorTest.php b/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/ReturnBinaryAndToEarlyReturnRectorTest.php deleted file mode 100644 index 3f481313036..00000000000 --- a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/ReturnBinaryAndToEarlyReturnRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/config/configured_rule.php b/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/config/configured_rule.php deleted file mode 100644 index 134dbc211a6..00000000000 --- a/rules-tests/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -rule(ReturnBinaryAndToEarlyReturnRector::class); -}; diff --git a/rules/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector.php b/rules/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector.php deleted file mode 100644 index 53a16bc5faa..00000000000 --- a/rules/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector.php +++ /dev/null @@ -1,135 +0,0 @@ -something() && $this->somethingelse(); - } -} -CODE_SAMPLE - - , - <<<'CODE_SAMPLE' -class SomeClass -{ - public function accept() - { - if (! $this->something()) { - return false; - } - - return (bool) $this->somethingelse(); - } -} -CODE_SAMPLE - ), - ]); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [Return_::class]; - } - - /** - * @param Return_ $node - * @return null|Node[] - */ - public function refactor(Node $node): ?array - { - if (! $node->expr instanceof BooleanAnd) { - return null; - } - - $left = $node->expr->left; - $ifNegations = $this->createMultipleIfsNegation($left, $node, []); - - // ensure ifs not removed by other rules - if ($ifNegations === []) { - return null; - } - - if (! $this->callAnalyzer->doesIfHasObjectCall($ifNegations)) { - return null; - } - - $this->mirrorComments($ifNegations[0], $node); - /** @var BooleanAnd $booleanAnd */ - $booleanAnd = $node->expr; - - $lastReturnExpr = $this->assignAndBinaryMap->getTruthyExpr($booleanAnd->right); - return array_merge($ifNegations, [new Return_($lastReturnExpr)]); - } - - /** - * @param If_[] $ifNegations - * @return If_[] - */ - private function createMultipleIfsNegation(Expr $expr, Return_ $return, array $ifNegations): array - { - while ($expr instanceof BooleanAnd) { - $ifNegations = array_merge($ifNegations, $this->collectLeftBooleanAndToIfs($expr, $return, $ifNegations)); - $ifNegations[] = $this->ifManipulator->createIfNegation( - $expr->right, - new Return_($this->nodeFactory->createFalse()) - ); - - $expr = $expr->right; - } - - return $ifNegations + [ - $this->ifManipulator->createIfNegation($expr, new Return_($this->nodeFactory->createFalse())), - ]; - } - - /** - * @param If_[] $ifNegations - * @return If_[] - */ - private function collectLeftBooleanAndToIfs(BooleanAnd $booleanAnd, Return_ $return, array $ifNegations): array - { - $left = $booleanAnd->left; - if (! $left instanceof BooleanAnd) { - return [$this->ifManipulator->createIfNegation($left, new Return_($this->nodeFactory->createFalse()))]; - } - - return $this->createMultipleIfsNegation($left, $return, $ifNegations); - } -} diff --git a/src/NodeManipulator/IfManipulator.php b/src/NodeManipulator/IfManipulator.php index 989aad853e9..050aa2cc9fb 100644 --- a/src/NodeManipulator/IfManipulator.php +++ b/src/NodeManipulator/IfManipulator.php @@ -17,7 +17,6 @@ use Rector\Core\PhpParser\Comparing\NodeComparator; use Rector\Core\PhpParser\Node\BetterNodeFinder; use Rector\Core\PhpParser\Node\Value\ValueResolver; -use Rector\EarlyReturn\NodeTransformer\ConditionInverter; final class IfManipulator { @@ -25,7 +24,6 @@ public function __construct( private readonly BetterNodeFinder $betterNodeFinder, private readonly StmtsManipulator $stmtsManipulator, private readonly ValueResolver $valueResolver, - private readonly ConditionInverter $conditionInverter, private readonly NodeComparator $nodeComparator ) { } @@ -188,14 +186,6 @@ public function isIfWithoutElseAndElseIfs(If_ $if): bool return $if->elseifs === []; } - public function createIfNegation(Expr $expr, Return_ $return): If_ - { - $expr = $this->conditionInverter->createInvertedCondition($expr); - return new If_($expr, [ - 'stmts' => [$return], - ]); - } - /** * @deprecated Create If_ directly, this is simple new with no added value */