php truepath,php – 为什么switch(true)具有比if()elseif()更小的NPath复杂度?

  • Post author:
  • Post category:php


我有这个功能负责将文件的名称和MIME类型转换为更多的“人”(例如file.png,image / png到[Image,PNG])。我发现有趣的是,if()elseif()语句的组具有比switch(true)语句更高的NPath复杂度。

使用以下代码,PHP Mess Detector输出一个4410的NPath:

public function humanKind()

{

$typeRA = explode(“/”, strtolower($this->type));

$fileRA = explode(“.”, $this->name);

$fileType = strtoupper($fileRA[count($fileRA) – 1]);

switch($typeRA[0]) {

case “image”:

$humanType = “Image”;

break;

case “video”:

$humanType = “Video”;

break;

case “audio”:

$humanType = “Sound”;

break;

case “font”:

$humanType = “Font”;

break;

default:

$humanType = “File”;

}

switch ($this->type) {

case “application/msword”:

case “application/pdf”:

case “applicaiton/wordperfect”:

case “text/plain”:

case “text/rtf”:

case “image/vnd.photoshop”:

case “image/psd”:

case “image/vnd.adobe.photoshop”:

case “image/x-photoshop”:

case “application/xml”:

case “application/x-mspublisher”:

case “text/html”:

case “application/xhtml+xml”:

case “text/richtext”:

case “application/rtf”:

case “application/x-iwork-pages-sffpages”:

case “application/vnd.apple.pages”:

$humanType = “Document”;

break;

case “application/vnd.ms-excel”:

case “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”:

case “application/x-iwork-numbers-sffnumbers”:

case “application/vnd.apple.numbers”:

$humanType = “Spreadsheet”;

break;

case “application/vnd.ms-powerpoint”:

case “application/vnd.openxmlformats-officedocument.presentationml.presentation”:

case “application/vnd.openxmlformats-officedocument.presentationml.slideshow”:

case “application/x-iwork-keynote-sffkey”:

case “application/vnd.apple.keynote”:

$humanType = “Slideshow”;

break;

case “application/zip”:

case “application/x-zip-compressed”:

case “application/x-compressed”:

case “application/x-compress”:

case “application/x-rar-compressed”:

case “applicaiton/x-7z-compressed”:

case “application/x-ace-compressed”:

$humanType = “Archive”;

break;

case “text/x-vcard”:

case “text/x-ms-contact”:

$humanType = “Contact”;

break;

case “text/x-php”:

case “application/x-dosexec”:

case “application/x-xpinstall”:

case “application/x-opera-extension”:

case “application/x-chrome-extension”:

case “application/x-perl”:

case “application/x-shockwave-flash”:

case “application/java-archive”:

$humanType = “Program”;

break;

case “application/vnd.ms-fontobject”:

case “application/font-woff”:

case “application/x-font-truetype”:

case “application/x-font-opentype”:

case “application/x-font-ttf”:

case “application/font-sfnt”:

$humanType = “Font”;

break;

}

// Special Cases

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”;

}

if (strlen($fileType) > 4) {

$fileType = “”;

}

return array($humanType, $fileType);

如果我们然后替换特殊情况,如果elseif与以下:

// Special Cases

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;

}

PHP Mess Detector报告了1960年的NPath复杂性。

为什么是这样?什么使switch(true)比我所看到的仅仅是一个相同的控制结构那么复杂?