Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect behavior of ChangeSwitchToMatchRector in if/else block #7065

Closed
sylbru opened this issue Mar 21, 2022 · 0 comments · Fixed by rectorphp/rector-src#1952
Closed

Incorrect behavior of ChangeSwitchToMatchRector in if/else block #7065

sylbru opened this issue Mar 21, 2022 · 0 comments · Fixed by rectorphp/rector-src#1952
Labels

Comments

@sylbru
Copy link

sylbru commented Mar 21, 2022

Bug Report

Subject Details
Rector version last dev-master
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.org/demo/b0d8696f-23ae-44b1-afb3-9c5ce9443bdf

<?php

function f(int $param, bool $flag): string
{
    if (!$flag) {
        $value = "A";
    } else {
        switch ($param) {
            case 1:
                $value = "B";
                break;
            case 2:
                $value = "C";
                break;
            default:
                $value = "D";
                break;
        }
    }
    
    return $value;
}

Responsible rules

  • ChangeSwitchToMatchRector

Expected Behavior

The rule should not touch anything outside the initial switch statement. Here it’s removing the $value = "A"; instruction in the other branch of the if/else block. This means that if I call this function with (99, false), it will have an undefined return value, where it should have returned "A" before running Rector.

By the way, but this is an unrelated issue, I think it’s dangerous to advertise this as a part of a simple automatic upgrade for PHP 8, as the switch->match conversion will likely introduce some bugs because of the strict comparison being used in match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant