Skip to content

[EarlyReturn] Deprecate ChangeNestedIfsToEarlyReturnRector - #8304

Merged
TomasVotruba merged 1 commit into
mainfrom
deprecate-change-nested-ifs-to-early-return
Aug 5, 2026
Merged

[EarlyReturn] Deprecate ChangeNestedIfsToEarlyReturnRector#8304
TomasVotruba merged 1 commit into
mainfrom
deprecate-change-nested-ifs-to-early-return

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

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 !== 5 to get back the original intent.

Follows #8302 and #8303.

ChangeNestedIfsToEarlyReturnRector now implements DeprecatedInterface and throws on use. It was the last rule of the early-return set; the set file and SetList::EARLY_RETURN stay in place (now empty) to avoid a BC break. Also drops the two IfManipulator methods that only this rule used.

@TomasVotruba
TomasVotruba enabled auto-merge (squash) August 5, 2026 23:08
@TomasVotruba
TomasVotruba merged commit b14e0ab into main Aug 5, 2026
64 checks passed
@TomasVotruba
TomasVotruba deleted the deprecate-change-nested-ifs-to-early-return branch August 5, 2026 23:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant