时间:2021-05-26
本文实例为大家分享了PHP实现统计代码行数小工具,供大家参考,具体内容如下
为了方面统计编程代码行数,做了一个小工具。
自动统计指定目录以及目录下的所有文件。
<?php class TotalCode { /** * 统计当前文件有多少行代码, * @return TotalCodeInfo */ public function totalByFile($fullFileName) { $fileContent = file_get_contents($fullFileName); $lines = explode("\n", $fileContent); $lineCount = count($lines); for($i = $lineCount -1; $i > 0; $i -= 1) { $line = $lines[$i]; if ($line != "") break; $lineCount -= 1; //最后几行是空行的要去掉。 } unset($fileContent); unset($lines); $totalCodeInfo = new TotalCodeInfo(); $totalCodeInfo->setFileCount(1); $totalCodeInfo->setLineCount($lineCount); return $totalCodeInfo; } /** * 统计当前目录下(含子目录) * 有多少文件,以及多少行代码 * * totalInfo = array( "fileCount"=>?, "lineCount"=>? ); * * @return TotalCodeInfo */ public function totalByDir($dirName) { $fileList = scandir($dirName); $totalCodeDir = new TotalCodeInfo(); foreach ($fileList as $fileName) { if ($fileName == "." || $fileName == "..") continue; $fullFileName = $dirName . "/" . $fileName; if (is_file($fullFileName)) { $totalCodeSub = $this->totalByFile($dirName . "/" . $fileName); } else if (is_dir($fullFileName)) { $totalCodeSub = $this->totalByDir($dirName . "/" . $fileName); } else { $totalCodeSub = new TotalCodeInfo(); } $totalCodeDir->increaseByOther($totalCodeSub); } return $totalCodeDir; } public function totalByDirOrFile($dirOrFileName) { if (is_dir($dirOrFileName)) { return $this->totalByDir($dirOrFileName); } else if (is_file($dirOrFileName)) { return $this->totalByFile($dirOrFileName); } else { return new TotalCodeInfo(); } } public function test() { $re = $this->totalByDir("/export/www/pm_web/configs"); var_dump($re); } public function main($dirList) { $totalCodeAll = new TotalCodeInfo(); foreach($dirList as $dirName) { $totalCodeSub = $this->totalByDirOrFile($dirName); $totalCodeAll->increaseByOther($totalCodeSub); } print_r($totalCodeAll); } } class TotalCodeInfo { private $fileCount = 0; private $lineCount = 0; public function getFileCount() { return $this->fileCount; } public function getLineCount() { return $this->lineCount; } public function setFileCount($fileCount) { $this->fileCount = $fileCount; return $this; } public function setLineCount($lineCount) { $this->lineCount = $lineCount; return $this; } /** * 累加 */ public function increaseByOther($totalCodeInfo) { $this->setFileCount( $this->fileCount + $totalCodeInfo->getFileCount()); $this->setLineCount( $this->lineCount + $totalCodeInfo->getLineCount()); return $this; }} $dirList = array();$dirList[] = "/your/path"; $obj = new TotalCode();$obj->main($dirList);以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
一个用python实现的统计代码行数的小工具,供大家参考,具体内容如下实现功能计算出某一目录以及子目录下代码文件的行数在计算代码的过程中,只对标准命名的文件进行
让主题支持小工具WordPress的小工具(widget)是一大特色,它让用户自由拖动组合内容,而且任何插件和主题都可以添加一个额外的小工具,增加扩展性。默认情
微软之所以在Windows8中取消桌面小工具功能,很有可能是因为桌面小工具特性与整体Metro风格的小程序有冲突嫌疑。此外,桌面小工具的实际使用率较低,好似鸡肋
网络直播很火,为了提高人气,一般会设置很多和观众互动的小工具。斗鱼直播就提供了一个歌曲点播小工具。那么这个小工具怎么启动和使用呢?软件名称:斗鱼TV直播伴侣v5
ios8小工具怎么用?怎么添加?小编带来了ios8小工具使用方法,在iOS8系统中我们可以添加并使用第三方开发者的小工具,更加实用哦~详细步骤请看下文吧。