Repository navigation
Default is compulsory in switch statements
This release adds a new rule making "default" statement compulsory in "switch" statements. See #29
Rationale:
Just imagine you have a function that does some processing on an article, based on its status. Its status can be "active", "pending" or "inactive"
What you should NOT do:
function doStuff($status) {
switch ($status) {
case "active":
// Do some stuff
break;
case "pending":
// Do some stuff
break;
case "inactive":
// Do some stuff
break;
}
}But in the future, what if a new status is added, like "archived"? How long will it take before you notice?
So your code should look like this:
function doStuff($status) {
switch ($status) {
case "active":
// Do some stuff
break;
case "pending":
// Do some stuff
break;
case "inactive":
// Do some stuff
break;
default:
// This is good! In case an unexpected status is sent, you will notice
throw InvalidStatusException(sprintf("Unknown Status '%s'", $status));
}
}By adding a default statement that throws an exception, you are sure to notice if something goes wrong in the future.