web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

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

351 ℃
     

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

1、设置php环境变量

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

//Linux:
vim /etc/profile
PATH="$PATH:/root/php/bin/"

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);
?>

3、语法执行

php count.php

4、结果返回

TotalCodeInfo Object                             
(                                                
    [fileCount:TotalCodeInfo:private] => 1024
    [lineCount:TotalCodeInfo:private] => 551689
)

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

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

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

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

利用php语法中WHERE子句排除不等字段功能

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

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

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

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

当前位置: 网站首页 > php入门
本文共计2485个字,预计阅读时长17分钟
生活小工具,收录了80多款小工具
上一篇: 推荐一个免费可商用psd文件素材网站——FreePik
下一篇: 一行代码轻松实现优雅的过渡动画插件——AutoAnimate
x 打工人ai神器