本文实例为大家分享了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; //最后几行是空行的要去掉。 } unt($filecontent); unt($lines); $totalcodeinfo = new totalcodeinfo(); $totalcodeinfo->tfilecount(1); $totalcodeinfo->tlinecount($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); } el if (is_dir($fullfilename)) { $totalcodesub = $this->totalbydir($dirname . "/" . $filename); } el { $totalcodesub = new totalcodeinfo(); } $totalcodedir->increabyother($totalcodesub); } return $totalcodedir; } public function totalbydirorfile($dirorfilename) { if (is_dir($dirorfilename)) { return $this->totalbydir($dirorfilename); } el if (is_file($dirorfilename)) { return $this->totalbyfile($dirorfilename); } el { return new totalcodeinfo(); } } public function test() { $re =网站开发背景 $thi房产证明s->totalbydir("/export/www/pm_web/configs"); var_dump($re); } public function main($dirlist) { $totalcodeall = new totalcodeinfo想表达感恩感谢的句子(); foreach($dirlist as $dirname) { $totalcodesu泉州师院b = $this->totalbydirorfile($dirname); $totalcodeall->increabyother($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 tfilecount($filecount) { $this->filecount = $filecount; return $this; } public function tlinecount($linecount) { $this->linecount = $linecount; ret古风句子超拽霸气urn $this; } /** * 累加 */ public function increabyother($totalcodeinfo) { $this->tfilecount( $this->filecount + $totalcodeinfo->getfilecount()); $this->tlinecount( $this->linecount + $totalcodeinfo->getlinecount()); return $this; }} $dirlist = array();$dirlist[] = "/your/path"; $obj = new totalcode();$obj->main($dirlist);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-08 07:29:04,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/d1bd0b7457a4ef10c2c01e7ad08bad7d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:PHP实现统计代码行数小工具.doc
本文 PDF 下载地址:PHP实现统计代码行数小工具.pdf
留言与评论(共有 0 条评论) |