栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

PHP统计目录下的文件总数及代码行数(去除注释及空行)

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP统计目录下的文件总数及代码行数(去除注释及空行)



$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");



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);
}

public function run($dir = '') {
if ($dir == '') return;
if (!is_dir($dir)) exit('Path error!');
$this->dump($dir, $this->readDir($dir));
}


private function dump($dir, $result) {
$totalLine = $result['totalLine'];
$lineNum = $result['lineNum'];
$fileNum = $result['fileNum'];
echo "*************************************************************rn
";
echo $dir . ":rn
";
echo "TotalLine: " . $totalLine . "rn
";
echo "TotalLine with no comment and empty: " . $lineNum . "rn
";
echo 'TotalFiles:' . $fileNum . "rn
";
}


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


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);
}


private function skipDir($dir) {
if (in_array($dir, $this->dirSkip)) return true;
return false;
}


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


private function skipLine($string) {
if ($string == '') return true;
foreach ($this->lineSkip as $tag) {
if (strpos($string, $tag) === 0) return true;
}
return false;
}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/51720.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号