rector-downgrade-php icon indicating copy to clipboard operation
rector-downgrade-php copied to clipboard

match with UnhandledMatchError

Open AlexP11223 opened this issue 1 year ago • 8 comments

Looks like code like this

        try {
            match ($age) {
                1, 2 => foo(),
                3, 4 => bar(),
            };
        } catch (\UnhandledMatchError $e) {
            var_dump($e);
        }

is currently transpiled into

        try {
            switch ($age) {
                case 1:
                case 2:
                    foo();
                    break;
                case 3:
                case 4:
                    bar();
                    break;
            }
        } catch (\UnhandledMatchError $e) {
            var_dump($e);
        }

which is not the same behavior, unhandled matches are just ignored.

I think an easy fix could be just to somehow add a polyfil for UnhandledMatchError and add a default in switch throwing it when there is no default in match.

AlexP11223 avatar Jul 05 '24 09:07 AlexP11223