PHP實(shí)現(xiàn)統(tǒng)計(jì)代碼行數(shù)小工具
本文實(shí)例為大家分享了PHP實(shí)現(xiàn)統(tǒng)計(jì)代碼行數(shù)小工具,供大家參考,具體內(nèi)容如下
為了方面統(tǒng)計(jì)編程代碼行數(shù),做了一個(gè)小工具。
自動(dòng)統(tǒng)計(jì)指定目錄以及目錄下的所有文件。
<?php
class TotalCode {
/**
* 統(tǒng)計(jì)當(dā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)計(jì)當(dā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);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
php實(shí)現(xiàn)模擬post請(qǐng)求用法實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)模擬post請(qǐng)求用法,分析了php模擬post請(qǐng)求的三種常見(jiàn)用法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
基于PHPexecl類(lèi)生成復(fù)雜的報(bào)表表頭示例
這篇文章主要介紹了基于PHPexecl類(lèi)生成復(fù)雜的報(bào)表表頭功能,結(jié)合實(shí)例形式分析了實(shí)例化PHPexecl類(lèi)生成復(fù)雜報(bào)表表頭的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
PHP設(shè)計(jì)模式之組合模式定義與應(yīng)用示例
這篇文章主要介紹了PHP設(shè)計(jì)模式之組合模式定義與應(yīng)用,結(jié)合實(shí)例形式詳細(xì)分析了PHP組合模式基本原理、定義與使用方法,需要的朋友可以參考下2020-02-02
PHP 獲取客戶端真實(shí)IP地址多種方法小結(jié)
PHP 獲取客戶端真實(shí)IP地址多種方法小結(jié),需要的朋友可以參考下。2010-05-05
php中靜態(tài)類(lèi)與靜態(tài)變量用法的區(qū)別分析
這篇文章主要介紹了php中靜態(tài)類(lèi)與靜態(tài)變量用法的區(qū)別,以實(shí)例形式較為詳細(xì)的分析了php中靜態(tài)類(lèi)與靜態(tài)變量的定義、功能及使用技巧,需要的朋友可以參考下2015-01-01
php中time()與$_SERVER[REQUEST_TIME]用法區(qū)別
這篇文章主要介紹了php中time()與$_SERVER[REQUEST_TIME]用法區(qū)別,詳細(xì)分析了time()與$_SERVER[REQUEST_TIME]的用法,并以實(shí)例形式對(duì)比總結(jié)了二者在獲取當(dāng)前系統(tǒng)時(shí)間戳與請(qǐng)求時(shí)間戳的區(qū)別,需要的朋友可以參考下2014-11-11

