• 技术文章 >后端开发 >php教程

    实例详解PHP统计代码行数及文件数量的方法

    藏色散人藏色散人2022-12-27 16:50:17转载236
    本篇文章主要给大家介绍有关PHP统计的相关知识,聊聊怎么用PHP来实现统计代码行数以及文件数量,希望对需要的朋友有所帮助!

    用PHP统计代码行数和文件数量

    有时候我们需要统计代码行数和文件数量(比如申请软著),我们可以用 PHP 进行统计。

    首先设置 PHP 环境变量

    我的电脑->属性->高级系统设置->PATH 加入PHP.exe的所在目录

    vim /etc/profile
    PATH="$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中文网其它相关文章!

    声明:本文转载于:learnku,如有侵犯,请联系admin@php.cn删除
    专题推荐:php
    上一篇:一文带你了解PHP中的文件与目录操作 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • PHP循环学习八:统计1~10000的完数个数,并输出全部完数• PHP循环学习十一:怎么判断素数并打印1~100的素数、统计其数目• php怎么统计字符串中不同字符的个数• php怎么统计数组里同一个值有几个• 用PHP来统计在线人数的四个方法详解
    1/1

    PHP中文网