佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > PHP實(shí)現(xiàn)統(tǒng)計(jì)代碼行數(shù)小工具

PHP實(shí)現(xiàn)統(tǒng)計(jì)代碼行數(shù)小工具

熱門(mén)標(biāo)簽:Linux服務(wù)器 電子圍欄 服務(wù)器配置 Mysql連接數(shù)設(shè)置 阿里云 團(tuán)購(gòu)網(wǎng)站 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 銀行業(yè)務(wù)

本文實(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í)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • PHP統(tǒng)計(jì)代碼行數(shù)的小代碼
  • php 廣告點(diǎn)擊統(tǒng)計(jì)代碼(php+mysql)
  • php+memcache實(shí)現(xiàn)的網(wǎng)站在線人數(shù)統(tǒng)計(jì)代碼
  • php利用cookie實(shí)現(xiàn)訪問(wèn)次數(shù)統(tǒng)計(jì)代碼
  • PHP遞歸統(tǒng)計(jì)系統(tǒng)中代碼行數(shù)

標(biāo)簽:衢州 萍鄉(xiāng) 廣元 衡水 江蘇 大理 棗莊 蚌埠

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP實(shí)現(xiàn)統(tǒng)計(jì)代碼行數(shù)小工具》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢(xún)

    • 400-1100-266
    灵丘县| 紫阳县| 鄂州市| 凤冈县| 景德镇市| 石城县| 阜南县| 长治市| 乌审旗| 惠来县| 肇东市| 和政县| 巴彦淖尔市| 汾西县| 盖州市| 团风县| 道孚县| 个旧市| 垣曲县| 永嘉县| 铁岭县| 运城市| 乌拉特前旗| 新宁县| 舒城县| 五华县| 梁山县| 浑源县| 肃南| 江口县| 桐城市| 昌吉市| 南昌县| 原平市| 盘锦市| 巴里| 新丰县| 仁寿县| 云阳县| 汉寿县| 砀山县|