Skip to content

[CodeQuality] Add SwitchTrueToMatchRector as replacement for deprecated SwitchTrueToIfRector#8113

Merged
TomasVotruba merged 1 commit into
mainfrom
switch-true-to-match-rector
Jun 29, 2026
Merged

[CodeQuality] Add SwitchTrueToMatchRector as replacement for deprecated SwitchTrueToIfRector#8113
TomasVotruba merged 1 commit into
mainfrom
switch-true-to-match-rector

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Jun 29, 2026

Copy link
Copy Markdown
Member

Replaces the deprecated SwitchTrueToIfRector with SwitchTrueToMatchRector.

Instead of expanding a compact switch (true) into many if statements, it converts it into a single match (true) expression.

 class SomeClass
 {
     public function run($value)
     {
-        switch (true) {
-            case $value === 0:
-                return 'no';
-            case $value === 1:
-                return 'yes';
-            default:
-                return 'maybe';
-        }
+        return match (true) {
+            $value === 0 => 'no',
+            $value === 1 => 'yes',
+            default => 'maybe',
+        };
     }
 }

Scope / safety

  • Only switch (true).
  • Every case must be a single return <expr>; (a match arm is one expression).
  • Requires a default case, and it must be the last one. This avoids a behavior change: a match (true) with no matching arm throws UnhandledMatchError, whereas switch (true) falls through.
  • Added to CodeQualityLevel.

Cases with break, assignments, or no/non-trailing default are skipped.

Replaces the rule deprecated in #8109.

@TomasVotruba TomasVotruba merged commit e12a222 into main Jun 29, 2026
65 checks passed
@TomasVotruba TomasVotruba deleted the switch-true-to-match-rector branch June 29, 2026 13:30
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