實例詳解PHP統計代碼行數及文件數量的方法-全球熱推薦

          來源:php中文網 | 2022-12-27 17:07:49 |

          本篇文章主要給大家介紹有關PHP統計的相關知識,聊聊怎么用PHP來實現統計代碼行數以及文件數量,希望對需要的朋友有所幫助!

          用PHP統計代碼行數和文件數量

          有時候我們需要統計代碼行數和文件數量(比如申請軟著),我們可以用 PHP 進行統計。

          首先設置 PHP 環境變量


          (相關資料圖)

          Windows:

          我的電腦->屬性->高級系統設置->PATH 加入PHP.exe的所在目錄

          Linux 設置環境變量:

          vim /etc/profilePATH="$PATH:/root/php/bin/"

          在項目文件夾里執行以下代碼

          <?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[] = "./";$obj = new TotalCode();$obj->main($dirList);?>

          結果

          執行代碼

          php count.php

          成功

          TotalCodeInfo Object                             (                                                    [fileCount:TotalCodeInfo:private] => 1094        [lineCount:TotalCodeInfo:private] => 419702  )

          推薦學習:《PHP視頻教程》

          以上就是實例詳解PHP統計代碼行數及文件數量的方法的詳細內容,更多請關注php中文網其它相關文章!

          關鍵詞:

          久久久久久久综合日本亚洲| 国产亚洲综合成人91精品| 亚洲国产成人久久综合一| 亚洲AV永久无码精品| 亚洲不卡中文字幕| 四虎精品亚洲一区二区三区| 亚洲色爱图小说专区| 亚洲日本在线播放| 日本亚洲中午字幕乱码| 国产成人亚洲精品91专区高清| 亚洲阿v天堂在线| 自拍日韩亚洲一区在线| 亚洲精品高清在线| 亚洲人成网站影音先锋播放| 亚洲精品中文字幕无码A片老| 亚洲午夜精品久久久久久浪潮| 久久精品国产亚洲av麻豆小说 | 亚洲高清无码在线观看| 亚洲av最新在线网址| 亚洲国产系列一区二区三区| 四虎精品亚洲一区二区三区| 亚洲va在线va天堂va不卡下载| 中文字幕亚洲码在线| 亚洲人精品午夜射精日韩 | 亚洲大片免费观看| 亚洲成a人片在线观看日本麻豆 | 日韩一卡2卡3卡4卡新区亚洲| 亚洲国产情侣一区二区三区| 亚洲А∨精品天堂在线| 亚洲美女激情视频| 亚洲精品网站在线观看不卡无广告 | 国产成人亚洲综合一区| 亚洲日本一区二区三区在线| 亚洲综合欧美色五月俺也去| 亚洲精品国产成人片| 亚洲AV成人潮喷综合网| 自怕偷自怕亚洲精品| 亚洲AV无码资源在线观看| 久久久久亚洲精品影视| 亚洲AV无码国产一区二区三区 | 国产精品高清视亚洲一区二区|