Since NPath complexity measures the number of unit tests required to get complete coverage of your code there should be no difference between your 2 "Special Cases" implementations.
But there is some difference in the calculation. Let's step through the 2 "Special Cases" implementations and calculate the NPath Complexity manually:
NPath Complexity with if .. elseif ..
if ($humanType == "Archive" && $fileType == "APK") { // Android App
$humanType = "App";
}
elseif ($humanType == "Archive" && $fileType == "XPS") {
$humanType = "Document";
}
elseif ($this->type == "application/xml" && $fileType == "CONTACT") {
$humanType = "Contact";
}
elseif ($this->type == "application/octet-stream" && $fileType == "JNT") {
$humanType = "Document";
}
This statement results in NPath Complexity of 9: 1 point for if .. else
, 1 point for every if(expr)
and 1 point for every &&
operator. (1 + 4 + 4 = 9)
NPath Complexity with switch(true)
switch(true) {
case ($humanType == "Archive" && $fileType == "APK"): // Android App
$humanType = "App";
break;
case ($humanType == "Archive" && $fileType == "XPS"):
$humanType = "Document";
break;
case ($this->type == "application/xml" && $fileType == "CONTACT"):
$humanType = "Contact";
break;
case ($this->type == "application/octet-stream" && $fileType == "JNT"):
$humanType = "Document";
break;
}
And this statement results in NPath Complexity of only 4: 0 points for switch(true)
because it contains no &&
or ||
operators and 1 point for every case
label. (0 + 4 = 4)
NPath Complexity of your humanKind
function
NPath values are calculated for each statement and than the values are multiplied. The NPath Complexity of your function without the "Special Cases" statement is 490. Multiplied with the NPath value for the if .. else if ..
statement of 9 you get a NPath Complexity of 4410. And multiplied with the NPath value for the switch(true)
statement of 4 you get a complexity of only 1960. That's all!
And now we know: NPath Complexity does not measure expression complexity of case
labels in switch
statements!