PHP统计目录下的文件总数及代码行数(去除注释及空行)
<?php教程 /** * @author xiaoxiao <x_824@sina.com> 2011-1-12 * @link http://xiaoyaoxia.cnblogs.com/ * @license * 统计目录下的文件行数及总文件数··去除注释 */ $obj = new caculatefiles(); //如果设置为false,这不会显示每个文件的信息,否则显示 $obj->setshowflag(false); //会跳过所有all开头的文件 $obj->setfileskip(array('all')); $obj->run("d:phpappphp_tests"); //所有文件,(默认格式为.php) $obj->setfileskip(array()); $obj->run("d:phpappphp"); $obj->setshowflag(true); //跳过所有i和a开头的文件,(比如接口和抽象类开头) $obj->setfileskip(array('i', 'a')); $obj->run("d:phpappphp"); /** * 执行目录中文件的统计(包括文件数及总行数 * * 1、跳过文件的时候: * 匹配的规则只是从文件名上着手,匹配的规则也仅限在开头。 * 2、跳过文件中的注释行: * 匹配的规则只是从注释段落的头部匹配,如果出现// 及 *及 #及/*开头的行及空行会被跳过。所以类似/*这种多汗注释,每行的开头都必须加上*号,否则无法匹配到这种的注释。 * 3、目录过滤: * 匹配的规则是从目录名的全名匹配 */ class caculatefiles { /** * 统计的后缀 */ private $ext = ".php"; /** * 是否显示每个文件的统计数 */ private $showeveryfile = true; /** * 文件的的跳过规则 */ private $fileskip = array(); /** * 统计的跳过行规则 */ private $lineskip = array("*", "/*", "//", "#"); /** * 统计跳过的目录规则 */ private $dirskip = array(".", "..", '.svn'); public function __construct($ext = '', $dir = '', $showeveryfile = true, $dirskip = array(), $lineskip = array(), $fileskip = array()) { $this->setext($ext); $this->setdirskip($dirskip); $this->setfileskip($fileskip); $this->setlineskip($lineskip); $this->setshowflag($showeveryfile); $this->run($dir); } public function setext($ext) { trim($ext) && $this->ext = strtolower(trim($ext)); } public function setshowflag($flag = true) { $this->showeveryfile = $flag; } public function setdirskip($dirskip) { $dirskip && is_array($dirskip) && $this->dirskip = $dirskip; } public function setfileskip($fileskip) { $this->fileskip = $fileskip; } public function setlineskip($lineskip) { $lineskip && is_array($lineskip) && $this->lineskip = array_merge($this->lineskip, $lineskip); } /** * 执行统计 * @param string $dir 统计的目录 */ public function run($dir = '') { if ($dir == '') return; if (!is_dir($dir)) exit('path error!'); $this->dump($dir, $this->readdir($dir)); } /** * 显示统计结果 * @param string $dir 目录 * @param array $result 统计结果(包含总行数,有效函数,总文件数 */ private function dump($dir, $result) { $totalline = $result['totalline']; $linenum = $result['linenum']; $filenum = $result['filenum']; echo "*************************************************************rn<br/>"; echo $dir . ":rn<br/>"; echo "totalline: " . $totalline . "rn<br/>"; echo "totalline with no comment and empty: " . $linenum . "rn<br/>"; echo 'totalfiles:' . $filenum . "rn<br/>"; } /** * 读取目录 * @param string $dir 目录 */ private function readdir($dir) { $num = array('totalline' => 0, 'linenum' => 0, 'filenum' => 0); if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if ($this->skipdir($file)) continue; if (is_dir($dir . '/' . $file)) { $result = $this->readdir($dir . '/' . $file); $num['totalline'] += $result['totalline']; $num['linenum'] += $result['linenum']; $num['filenum'] += $result['filenum']; } else { if ($this->skipfile($file)) continue; list($num1, $num2) = $this->readfiles($dir . '/' . $file); $num['totalline'] += $num1; $num['linenum'] += $num2; $num['filenum']++; } } closedir($dh); } else { echo 'open dir <' . $dir . '> error!' . "r"; } return $num; } /** * 读取文件 * @param string $file 文件 */ private function readfiles($file) { $str = file($file); $linenum = 0; foreach ($str as $value) { if ($this->skipline(trim($value))) continue; $linenum++; } $totalnum = count(file($file)); if (!$this->showeveryfile) return array($totalnum, $linenum); echo $file . "rn"; echo 'totalline in the file:' . $totalnum . "rn"; echo 'totalline with no comment and empty in the file:' . $linenum . "rn"; return array($totalnum, $linenum); } /** * 执行跳过的目录规则 * @param string $dir 目录名 */ private function skipdir($dir) { if (in_array($dir, $this->dirskip)) return true; return false; } /** * 执行跳过的文件规则 * @param string $file 文件名 */ private function skipfile($file) { if (strtolower(strrchr($file, '.')) != $this->ext) return true; if (!$this->fileskip) return false; foreach ($this->fileskip as $skip) { if (strpos($file, $skip) === 0) return true; } return false; } /** * 执行文件中行的跳过规则 * @param string $string 行内容 */ private function skipline($string) { if ($string == '') return true; foreach ($this->lineskip as $tag) { if (strpos($string, $tag) === 0) return true; } return false; } }
教程网址:http://www.phprm.com/code/36971.html
欢迎收藏∩_∩但请保留本文链接。