Should I be avoiding having a long list of serialized IF statements in my code? Sometimes it seems unavoidable, but I wonder if that's just my inexperience.
For example, if you're processing an image the user has uploaded -- and you wish to give accurate feedback on any errors -- you might have something like:
if($file["size"] == 0) {
throw new Exception("ERROR: File was empty");
}
if (($file["type"] != "image/gif")
|| ($file["type"] != "image/jpeg")
|| ($file["type"] != "image/pjpeg")
|| ($file["type"] != "image/png")) {
throw new Exception("ERROR: Image must be either GIF, PNG or JPEG!");
}
if ($file["size"] > 2000000) {
throw new Exception("ERROR: Image must be than less 2MB!");
}
if ($file["error"] > 0) {
throw new Exception("UNKNOWN ERROR: ".$file['error']);
}
$imgDetails = getimagesize($file["tmp_name"]);
if($imgDetails['channels'] != 3){
throw new Exception("ERROR: Image must be RGB.)";
}
if($imgDetails['0'] < 50 && $imgDetails['1'] < 50) {
throw new Exception("ERROR: Image must be larger then 50 x 50.)";
}
Etc. etc. etc. Until eventually the file passes all the tests and is processed.
Is this "bad practice"?