PHP实现统计代码行数小工具
程序员文章站
2023-01-26 21:56:47
本文实例为大家分享了php实现统计代码行数小工具,供大家参考,具体内容如下
为了方面统计编程代码行数,做了一个小工具。
自动统计指定目录以及目录下的所有文件。
<&...
本文实例为大家分享了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);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。