PHP Coding styles return; in switch/case

后端 未结 6 2020
栀梦
栀梦 2021-02-01 00:13

we\'re trying to implement new coding style guidelines for our team, the php codesniffer is printing an warning on switch case statements when no \"break\" is found like:

<
6条回答
  •  星月不相逢
    2021-02-01 00:59

    If your "php codesniffer is printing a warning" try to get another better codesniffer and don't forget to try to use the last PHP stable version. You can, of course, write a breakafter one return, but it doesn't make sense, because it will never be read at all. Your code is OK.

    Look at this:

    $fun = function(int $argument): string {
        switch ($argument) {
            case 1:
                return "one";
            case 2:
                return "two";
            default:
                return "more than two";
        }
    };
    $str = $fun(4); // return "more than two"
    

    In my opinion, this is simpler and better: fewer lines => less code to maintain :-)

提交回复
热议问题