PHP实现统计代码行数小工具
作者:oushunbao 发布时间:2023-11-15 18:53:20
标签:php,代码行数
本文实例为大家分享了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[] = "/your/path";
$obj = new TotalCode();
$obj->main($dirList);
来源:https://blog.csdn.net/oushunbao/article/details/84794069
0
投稿
猜你喜欢
- 请问如何处理Oracle中较大的文本数据?我们可在ASP中予以解决,如在Oracle8i中文版中,建立数据表:CREATE TABLE SY
- ASP实例代码,利用SQL语句动态创建Access表。留作参考,对在线升级数据库有用处.<% nowtime = now()
- SQL中的单记录函数 1.ASCII 返回与指定的字符对应的十进制数; SQL> select ascii('A')
- 第一步、在detect.py中177行左右,cv.imshow(str(p),im0)之前加上:cv2.putText(im0,f"
- 本文实例讲述了Python类的用法。分享给大家供大家参考。具体如下:先看一段代码:#!/usr/bin/env pythonclass Te
- 知识点: 1、拼接SQL 2、UNION ALL 3、EXEC 其代码如下: 代码如下:--测试示例 declare @sql
- 本文介绍了一种将英文字符首个字母串转换为大写的asp代码,当然这个功能可能英文网站比较有用。转换大写功能英文介绍:Code Title: P
- 1、场景描述通常来说,前端要拿到excel数据,都是先上传存储数据库,然后再请求后端接口,获取数据。但有100个产品经理,就会有101个不同
- 下面是模板的一般形式,显示了指定 SQL 查询和 XPath 查询的方式: <ROOT xmlns:sql="ur
- 很久没写blog,太忙了。没什么时间写复杂的东西,重新把颜色渐变效果写一遍。关于颜色的效果一般就两个,颜色梯度变化和样式的颜色渐变,前者在i
- 1.普通的输出:print(str)#str是任意一个字符串,数字···2.格式化输出: print('1,2,%s,%d'
- 前段时间在开发雨哲树网程序的时候,遇到需要转换地址中的参数,需要用到简单可逆运算的加密功能。在网上找了很多都不理想。因为我需要的这个可逆运算
- 你用过css么?当然,我是指你喜欢做网页的话,用过?很好,那你用过它的特效么?没有?那请跟我来。让我先通俗的介绍一下css,cs
- 1、简述asp的6个内置对象和其功能答案 : session ,server ,response,request,objectcontext
- 在数据库中,字符型的数据是最多的,可以占到整个数据库的80%以上。为此正确处理字符型的数据,对于提高数据库的性能有很大的作用。在字符型数据中
- 前言我们在写应用时,基本都会用到配置文件,从各种 shell 到 nginx 等,都有自己的配置文件。虽然这没有太多难度,但是配置项一般相对
- 源码及注释:import pygamefrom sys import exitfrom random import randintimpor
- 阅读上一章:Chapter 14 图片替换Chapter 15 为<body>指定样式把内容与显示效果分开设定的好处之一就是灵活
- 对于SQL的Join,在学习起来可能是比较乱的。我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于
- 反向单位矩阵单位矩阵即对角线为 1,如下:那么反向的单位矩阵就是反对角线为 1:左右镜像操作这里采用 numpy 实现。方案 1import