[EarlyReturn] Deprecate ChangeNestedIfsToEarlyReturnRector - #8304
Merged
Conversation
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.
Inverting nested ifs into early returns flips the logic flow, which makes the code harder to read and understand. Whether it is an improvement depends on the context, so it should not be automated.
class SomeClass { public function run() { - if ($value === 5) { - if ($value2 === 10) { - return 'yes'; - } + if ($value !== 5) { + return 'no'; + } + + if ($value2 === 10) { + return 'yes'; } return 'no'; } }The 2nd form is not clearly better - the
return 'no'is now duplicated and the reader has to mentally re-invert$value !== 5to get back the original intent.Follows #8302 and #8303.
ChangeNestedIfsToEarlyReturnRectornow implementsDeprecatedInterfaceand throws on use. It was the last rule of theearly-returnset; the set file andSetList::EARLY_RETURNstay in place (now empty) to avoid a BC break. Also drops the twoIfManipulatormethods that only this rule used.