[EarlyReturn] Split nested && operand into its own early return in ReturnBinaryOrToEarlyReturnRector#8156
Open
TomasVotruba wants to merge 2 commits into
Open
[EarlyReturn] Split nested && operand into its own early return in ReturnBinaryOrToEarlyReturnRector#8156TomasVotruba wants to merge 2 commits into
TomasVotruba wants to merge 2 commits into
Conversation
…turnBinaryOrToEarlyReturnRector
9591092 to
786d227
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes rectorphp/rector#9802
ReturnBinaryOrToEarlyReturnRectorsilently dropped operands when a returned||chain contained a parenthesized&&in a nested-left position.Problem
The rule used an empty array
[]as an abort signal when it met a&&operand. But when the&&sat deep in the left branch of a longer||chain, that[]was spread back into the collectedifs during recursion — indistinguishable from "nothing collected here" — so collection resumed and the earlier operands were discarded:public function hasStale(): bool { - return $this->donation === null - || ($this->commission > 0 && $this->transaction === null) - || $this->somethingElse() - || $this->anotherThing(); + if ($this->somethingElse()) { + return true; + } + return $this->anotherThing(); }The first two operands (
$this->donation === nulland the&&group) vanished — the method no longer meant the same thing.Fix
A
&&operand is a perfectly valid early-return condition, so instead of skipping it is now kept whole inside its ownif. The convoluted recursivecreateMultipleIfs/collectLeftBooleanOrToIfspair (and its ambiguous[]abort) is replaced by a simple flatten of the left-associative||chain into ordered operands — each becomes one early return, the last becomes the final return.public function hasStale(): bool { - return $this->donation === null - || ($this->commission > 0 && $this->transaction === null) - || $this->somethingElse() - || $this->anotherThing(); + if ($this->donation === null) { + return true; + } + if ($this->commission > 0 && $this->transaction === null) { + return true; + } + if ($this->somethingElse()) { + return true; + } + return (bool) $this->anotherThing(); }Tests
and_nested_deep.php.inc(the reported case).skip_and_in_next.php.incis nowand_in_next.php.inc— a&&in the middle of a chain now transforms instead of being skipped.