3

I have to work on some legacy code. In a part of this code, there are two default cases in a switch:

switch (strtoupper($format)) {
    case '4A0': {$format = array(4767.87,6740.79); break;}
    // etc.
    case 'A3': {$format = array(841.89,1190.55); break;}
    case 'A4': default: {$format = array(595.28,841.89); break;}
    case 'A5': {$format = array(419.53,595.28); break;}
    // etc.
    case 'ROYAL': {$format=array(433.70,663.30 );  break;}
    default: $format = false;
}

It should have work before but it can't anymore now (since PHP7 multiple default cases will raise a E_COMPILE_ERROR error, BTW it's a good news!). I want to clean this code (I don't understand why some people added two default in a switch, I think it's weird and dirty) and remove one of the default, but I'm not sure to understand the purpose of that code, so I don't know which one I have to kill.

Assuming I don't want to break anything, should I remove the first default or the second default?

rap-2-h
  • 30,204
  • 37
  • 167
  • 263
  • 3
    Write a unit test for it, get it running in the PHP version it works in, then switch to PHP7 and you can refactor at will, safe in the knowledge that you have a test case ensuring the outcomes are the same. – bcmcfc Sep 28 '16 at 13:50
  • 1
    Just remove all but the last. It's most likely a left over introduced because of that horribly unreadable code style. – PeeHaa Sep 28 '16 at 14:08
  • 1
    @FélixGagnon-Grenier When I wrote "thanks to PHP7", there is no sarcasm. This code is not mine, and I never saw two default case before, so it's a unknown problem for me now, that's why I ask. – rap-2-h Sep 28 '16 at 15:03
  • 1
    Wow. Down-voted twice, don't understand why... – rap-2-h Sep 28 '16 at 15:04
  • 1
    @rap-2-h oh sorry, I misunderstood the intent :) as for the dv I would hazard it'd have to do with the quality of the code, or because people think you should think about that on your own. – Félix Adriyel Gagnon-Grenier Sep 28 '16 at 15:05
  • 1
    @FélixGagnon-Grenier I think you are not the only one who misunderstood, I think it's why I have been downvoted for a "ok" question ;) – rap-2-h Sep 28 '16 at 15:06

0 Answers0