Switch statements in PHP link a situation (the cases) to code to be executed. Just like this :
switch ($a) { case 'a' : /* code here */ case 'b' : /* code here */ default : /* code here */ }
Duplicate code in switch statements
With the size of the switch, it is easy to miss that cases are defined twice. Here is an example in a version of PHPExcel library :
switch ($conditional->getStyle()->getFill()->getEndColor()->getRGB()) { case '000000': $colorIdxFg = 0x08; break; case 'FFFFFF': $colorIdxFg = 0x09; break; case 'FF0000': $colorIdxFg = 0x0A; break; case '00FF00': $colorIdxFg = 0x0B; break; case '0000FF': $colorIdxFg = 0x0C; break; case 'FFFF00': $colorIdxFg = 0x0D; break; case 'FF00FF': $colorIdxFg = 0x0E; break; case '00FFFF': $colorIdxFg = 0x0F; break; case '800000': $colorIdxFg = 0x10; break; case '008000': $colorIdxFg = 0x11; break; case '000080': $colorIdxFg = 0x12; break; case '808000': $colorIdxFg = 0x13; break; case '800080': $colorIdxFg = 0x14; break; case '008080': $colorIdxFg = 0x15; break; /* more cases */ case 'CCCCFF': $colorIdxFg = 0x1F; break; case '000080': $colorIdxFg = 0x20; break; case 'FF00FF': $colorIdxFg = 0x21; break; case 'FFFF00': $colorIdxFg = 0x22; break; case '00FFFF': $colorIdxFg = 0x23; break; case '800080': $colorIdxFg = 0x24; break; case '800000': $colorIdxFg = 0x25; break; case '008080': $colorIdxFg = 0x26; break; /* more cases */
Two duplicates are in bold, and you can find another five of them. Here, all second case will be ignored and may be dropped, at least from a programming point of view. From a user point of view, may be those 0x20 and 0x21 will be missing at some point.
Sometimes, it is quite obvious, as this code from jQuery-File-Upload :
switch ($type) { case 'gif': case 'png': imagecolortransparent($new_img, imagecolorallocate($new_img, 0, 0, 0)); case 'png': imagealphablending($new_img, false); imagesavealpha($new_img, true); break; }
Here, the ‘png’ case is twice and will need some refactoring. And sometimes, it is a forgotten copy/paste, as it is in Simplepie :
switch ($this->consume()) { case "\x09": case "\x0A": case "\x0B": case "\x0B": case "\x0C": case "\x20": case "\x3C": case "\x26": case false: /* more code */ }
What code is prone to duplicate cases ?
Legacy statements, long switch and switch with grouped cases. The short ones (1-2 cases) tends to be checked on the fly while coding, so they are pretty safe.
Is it worth checking duplicate cases?
Duplicate cases are pure dead code (only one of them is actually used), or a bug (some of the case is misspelled, and a situation is not taken into account). Finding duplicate case is always rewarded.
Switches statements are not too many. Ratio varies from 1 to 2 every thousand of lines of code. This is not a heavy task.
How to check
Finding the switch statements is a search operation. Then, the short ones, with one to 5-7 cases are easy to check visually. Beyond this size and when the cases are too widely separated, it is best extracting the case statement with the IDE, sorting and extracting the duplicates.
This is also automatically done with Exakat static auditing tool.