Skip to content

RemoveAlwaysElseRector is not fully refactoring code #9504

@u01jmg3

Description

@u01jmg3

Bug Report

Subject Details
Rector version 2.2.8

RemoveAlwaysElseRector is not formatting code correctly or completing what I would expect it to refactor.

Minimal PHP Code Causing Issue

https://getrector.com/demo/6b25de4e-f818-4e12-95c4-62620da5445d

-        if ($value == 0) {
-            return $value;
-        } elseif ($formatForEmail) {
-            $value .= '|' . 1;
-
-            return $time_context . '  //  ' . $direction . $value . ' minute(s)';
-        } else {
+        if ($value == 0) {
+            return $value;
+        }
+        if ($formatForEmail) {
+            $value .= '|' . 1;
+            return $time_context . '  //  ' . $direction . $value . ' minute(s)';
+        }
+        else {
             return $value;
         }

Expected Behaviour

Highlights

  • Final else branch can be refactored further
  • A newline should be inserted between if conditions
  • The newline present in the original code should be maintained after the refactor (i.e. the one that comes after $value .= '|' . 1;)
-        if ($value == 0) {
-            return $value;
-        } elseif ($formatForEmail) {
-            $value .= '|' . 1;
-
-            return $time_context . '  //  ' . $direction . $value . ' minute(s)';
-        } else {
-             return $value;
-        }
+        if ($value == 0) {
+            return $value;
+        }
+
+        if ($formatForEmail) {
+            $value .= '|' . 1;
+
+            return $time_context . '  //  ' . $direction . $value . ' minute(s)';
+        }
+
+        return $value;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions