PHP实现统计代码行数小工具
本文实例为大家分享了PHP实现统计代码行数小工具,供大家参考,具体内容如下
为了方面统计编程代码行数,做了一个小工具。
自动统计指定目录以及目录下的所有文件。
0;$i-=1){ $line=$lines[$i]; if($line!="")break; $lineCount-=1;//最后几行是空行的要去掉。 } unset($fileContent); unset($lines); $totalCodeInfo=newTotalCodeInfo(); $totalCodeInfo->setFileCount(1); $totalCodeInfo->setLineCount($lineCount); return$totalCodeInfo; } /** *统计当前目录下(含子目录) *有多少文件,以及多少行代码 * *totalInfo=array("fileCount"=>?,"lineCount"=>?); * *@returnTotalCodeInfo */ publicfunctiontotalByDir($dirName){ $fileList=scandir($dirName); $totalCodeDir=newTotalCodeInfo(); foreach($fileListas$fileName){ if($fileName=="."||$fileName=="..")continue; $fullFileName=$dirName."/".$fileName; if(is_file($fullFileName)){ $totalCodeSub=$this->totalByFile($dirName."/".$fileName); }elseif(is_dir($fullFileName)){ $totalCodeSub=$this->totalByDir($dirName."/".$fileName); }else{ $totalCodeSub=newTotalCodeInfo(); } $totalCodeDir->increaseByOther($totalCodeSub); } return$totalCodeDir; } publicfunctiontotalByDirOrFile($dirOrFileName){ if(is_dir($dirOrFileName)){ return$this->totalByDir($dirOrFileName); }elseif(is_file($dirOrFileName)){ return$this->totalByFile($dirOrFileName); }else{ returnnewTotalCodeInfo(); } } publicfunctiontest(){ $re=$this->totalByDir("/export/www/pm_web/configs"); var_dump($re); } publicfunctionmain($dirList){ $totalCodeAll=newTotalCodeInfo(); foreach($dirListas$dirName){ $totalCodeSub=$this->totalByDirOrFile($dirName); $totalCodeAll->increaseByOther($totalCodeSub); } print_r($totalCodeAll); } } classTotalCodeInfo{ private$fileCount=0; private$lineCount=0; publicfunctiongetFileCount(){return$this->fileCount;} publicfunctiongetLineCount(){return$this->lineCount;} publicfunctionsetFileCount($fileCount){ $this->fileCount=$fileCount; return$this; } publicfunctionsetLineCount($lineCount){ $this->lineCount=$lineCount; return$this; } /** *累加 */ publicfunctionincreaseByOther($totalCodeInfo){ $this->setFileCount($this->fileCount+$totalCodeInfo->getFileCount()); $this->setLineCount($this->lineCount+$totalCodeInfo->getLineCount()); return$this; } } $dirList=array(); $dirList[]="/your/path"; $obj=newTotalCode(); $obj->main($dirList);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。