php如何利用语法统计代码行数及文件数量

575 ℃

php如何利用语法统计代码行数及文件数量,下面web建站小编给大家详细介绍一下具体操作流程!

1、设置php环境变量

复制代码//Windows:
我的电脑->属性->高级系统设置->PATH 加入PHP.exe的所在目录

//Linux:
vim /etc/profile
PATH="$PATH:/root/php/bin/"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2、新增count.php文件,输入以下代码:

复制代码<?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);
?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99

3、语法执行

复制代码php count.php
  • 1

4、结果返回

复制代码TotalCodeInfo Object                             
(                                                
    [fileCount:TotalCodeInfo:private] => 1024
    [lineCount:TotalCodeInfo:private] => 551689
)
  • 1
  • 2
  • 3
  • 4
  • 5

php语法对index.php首页进行判断(根据需要显示不同页面)

html文本如何读取数据库代码示例

php表单提交报错显示适当的错误消息代码

php语法中sleep函数的基本用法

利用php语法中NOT IN排除多个字段功能

标签: php语法, 统计代码行数, 统计文件数量

上面是“php如何利用语法统计代码行数及文件数量”的全面内容,想了解更多关于 php入门 内容,请继续关注web建站教程。

当前网址:https://m.ipkd.cn/webs_3351.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

推荐一个可以获取省、市、区县、镇及街道的地图网站
网站降级的一般原因是什么?
javascript如何展示股票行情分析结果
精益求精-帝国网站管理系统7.5测试版开源发布
vue开发中关于eslint代码配置检查说明