Skip to content

[Php80] Skip int cast of string subject in ChangeSwitchToMatchRector - #9056

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-switch-to-match-int-cast
Nov 24, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
fix-switch-to-match-int-cast

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

switch compares loosely, match strictly. For a string subject with int case values, the rule cast the subject to int:

function test(string $code): int
{
    switch ($code) {
        case 777:
            return 1;
        default:
            return 4;
    }
}

became

return match ((int) $code) {
    777 => 1,
    default => 4,
};

On PHP 8, '777abc' == 777 is false (non-numeric string compares as string), but (int) '777abc' === 777 is true. Same for '777.5', or 'abc' vs a 0 case.

The fix skips a switch with a string subject and any int case value - there is no cast that keeps the loose semantics. The (string) cast for an int subject is unchanged. The cast_int_for_string fixture becomes skip_cast_int_for_string.

@TomasVotruba
TomasVotruba merged commit baa462e into main Nov 24, 2026
51 checks passed
@TomasVotruba
TomasVotruba deleted the fix-switch-to-match-int-cast branch November 24, 2026 16:57
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