PHP實現(xiàn)統(tǒng)計代碼行數(shù)小工具
更新時間:2019年09月19日 10:06:46 作者:oushunbao
這篇文章主要為大家詳細介紹了PHP實現(xiàn)統(tǒng)計代碼行數(shù)小工具,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了PHP實現(xiàn)統(tǒng)計代碼行數(shù)小工具,供大家參考,具體內容如下
為了方面統(tǒng)計編程代碼行數(shù),做了一個小工具。
自動統(tǒng)計指定目錄以及目錄下的所有文件。
<?php
class TotalCode {
/**
* 統(tǒng)計當前文件有多少行代碼,
* @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;
}
/**
* 統(tǒng)計當前目錄下(含子目錄)
* 有多少文件,以及多少行代碼
*
* 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);
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
php基于登陸時間判斷實現(xiàn)一天多次登錄只積分一次功能示例
這篇文章主要介紹了php基于登陸時間判斷實現(xiàn)一天多次登錄只積分一次功能,適合會員系統(tǒng)的積分功能,涉及php時間判斷與數(shù)據(jù)庫相關操作技巧,需要的朋友可以參考下2017-10-10
PHP中strncmp()函數(shù)比較兩個字符串前2個字符是否相等的方法
這篇文章主要介紹了PHP中strncmp()函數(shù)比較兩個字符串前2個字符是否相等的方法,實例分析了strncmp()函數(shù)的功能,參數(shù)用法與使用技巧,需要的朋友可以參考下2016-01-01
PHP使用file_get_contents發(fā)送http請求功能簡單示例
這篇文章主要介紹了PHP使用file_get_contents發(fā)送http請求功能,結合實例形式分析了file_get_contents結合stream_context_create實現(xiàn)的發(fā)送post請求數(shù)據(jù)相關原理與操作技巧,需要的朋友可以參考下2018-04-04
PHP實現(xiàn)數(shù)字補零功能的2個函數(shù)介紹
這篇文章主要介紹了PHP實現(xiàn)數(shù)字補零功能的2個函數(shù)介紹,需要的朋友可以參考下2014-05-05
關于file_get_contents返回為空或函數(shù)不可用的解決方案
本篇文章是對file_get_contents返回為空或函數(shù)不可用的解決方案進行了詳細的分析介紹,需要的朋友參考下2013-06-06

