I was analyzing my code with PHP Mess Detector when PHPMD reported some of my codes has high NPath Complexity. One example would be:
function compareDates($date1, $date2){
if($date->year < $date2->year){
return -1;
}
if($date->year > $date2->year){
return 1;
}
if($date->month < $date2->month){
return -1;
}
if($date->month > $date2->month){
return 1;
}
if($date->day < $date2->day){
return -1;
}
if($date->day > $date2->day){
return 1;
}
// etc.. same for hour, minute, second.
return 0;
}
The result would be that this function has very high NPath complexity. Is there a generic way of coding to reduce such control structures and NPath complexity?
Source Code: http://code.google.com/p/phpraise/source/browse/trunk/phpraise/core/datetime/RaiseDateTime.php#546