PHP统计目录下的文件总数及代码行数(去除注释及空行)


Posted in PHP onJanuary 17, 2011

<?php
/**
* @author xiaoxiao <x_824@sina.com> 2011-1-12
* @link http://xiaoyaoxia.cnblogs.com/
* @license
* 统计目录下的文件行数及总文件数··去除注释
*/

$obj = new CaculateFiles();
//如果设置为false,这不会显示每个文件的信息,否则显示
$obj->setShowFlag(false);
//会跳过所有All开头的文件
$obj->setFileSkip(array('All'));
$obj->run("D:\PHPAPP\php\_tests");

//所有文件,(默认格式为.php)
$obj->setFileSkip(array());
$obj->run("D:\PHPAPP\php");

$obj->setShowFlag(true);
//跳过所有I和A开头的文件,(比如接口和抽象类开头)
$obj->setFileSkip(array('I', 'A'));
$obj->run("D:\PHPAPP\php");

/**
* 执行目录中文件的统计(包括文件数及总行数
*
* 1、跳过文件的时候:
* 匹配的规则只是从文件名上着手,匹配的规则也仅限在开头。
* 2、跳过文件中的注释行:
* 匹配的规则只是从注释段落的头部匹配,如果出现// 及 *及 #及/*开头的行及空行会被跳过。所以类似/*这种多汗注释,每行的开头都必须加上*号,否则无法匹配到这种的注释。
* 3、目录过滤:
* 匹配的规则是从目录名的全名匹配
*/
class CaculateFiles {
/**
* 统计的后缀
*/
private $ext = ".php";
/**
* 是否显示每个文件的统计数
*/
private $showEveryFile = true;
/**
* 文件的的跳过规则
*/
private $fileSkip = array();
/**
* 统计的跳过行规则
*/
private $lineSkip = array("*", "/*", "//", "#");
/**
* 统计跳过的目录规则
*/
private $dirSkip = array(".", "..", '.svn');

public function __construct($ext = '', $dir = '', $showEveryFile = true, $dirSkip = array(), $lineSkip = array(), $fileSkip = array()) {
$this->setExt($ext);
$this->setDirSkip($dirSkip);
$this->setFileSkip($fileSkip);
$this->setLineSkip($lineSkip);
$this->setShowFlag($showEveryFile);
$this->run($dir);
}

public function setExt($ext) {
trim($ext) && $this->ext = strtolower(trim($ext));
}
public function setShowFlag($flag = true) {
$this->showEveryFile = $flag;
}
public function setDirSkip($dirSkip) {
$dirSkip && is_array($dirSkip) && $this->dirSkip = $dirSkip;
}
public function setFileSkip($fileSkip) {
$this->fileSkip = $fileSkip;
}
public function setLineSkip($lineSkip) {
$lineSkip && is_array($lineSkip) && $this->lineSkip = array_merge($this->lineSkip, $lineSkip);
}
/**
* 执行统计
* @param string $dir 统计的目录
*/
public function run($dir = '') {
if ($dir == '') return;
if (!is_dir($dir)) exit('Path error!');
$this->dump($dir, $this->readDir($dir));
}

/**
* 显示统计结果
* @param string $dir 目录
* @param array $result 统计结果(包含总行数,有效函数,总文件数
*/
private function dump($dir, $result) {
$totalLine = $result['totalLine'];
$lineNum = $result['lineNum'];
$fileNum = $result['fileNum'];
echo "*************************************************************\r\n<br/>";
echo $dir . ":\r\n<br/>";
echo "TotalLine: " . $totalLine . "\r\n<br/>";
echo "TotalLine with no comment and empty: " . $lineNum . "\r\n<br/>";
echo 'TotalFiles:' . $fileNum . "\r\n<br/>";
}

/**
* 读取目录
* @param string $dir 目录
*/
private function readDir($dir) {
$num = array('totalLine' => 0, 'lineNum' => 0, 'fileNum' => 0);
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($this->skipDir($file)) continue;
if (is_dir($dir . '/' . $file)) {
$result = $this->readDir($dir . '/' . $file);
$num['totalLine'] += $result['totalLine'];
$num['lineNum'] += $result['lineNum'];
$num['fileNum'] += $result['fileNum'];
} else {
if ($this->skipFile($file)) continue;
list($num1, $num2) = $this->readfiles($dir . '/' . $file);
$num['totalLine'] += $num1;
$num['lineNum'] += $num2;
$num['fileNum']++;
}
}
closedir($dh);
} else {
echo 'open dir <' . $dir . '> error!' . "\r";
}
return $num;
}

/**
* 读取文件
* @param string $file 文件
*/
private function readfiles($file) {
$str = file($file);
$linenum = 0;
foreach ($str as $value) {
if ($this->skipLine(trim($value))) continue;
$linenum++;
}
$totalnum = count(file($file));
if (!$this->showEveryFile) return array($totalnum, $linenum);
echo $file . "\r\n";
echo 'TotalLine in the file:' . $totalnum . "\r\n";
echo 'TotalLine with no comment and empty in the file:' . $linenum . "\r\n";
return array($totalnum, $linenum);
}

/**
* 执行跳过的目录规则
* @param string $dir 目录名
*/
private function skipDir($dir) {
if (in_array($dir, $this->dirSkip)) return true;
return false;
}

/**
* 执行跳过的文件规则
* @param string $file 文件名
*/
private function skipFile($file) {
if (strtolower(strrchr($file, '.')) != $this->ext) return true;
if (!$this->fileSkip) return false;
foreach ($this->fileSkip as $skip) {
if (strpos($file, $skip) === 0) return true;
}
return false;
}

/**
* 执行文件中行的跳过规则
* @param string $string 行内容
*/
private function skipLine($string) {
if ($string == '') return true;
foreach ($this->lineSkip as $tag) {
if (strpos($string, $tag) === 0) return true;
}
return false;
}
}

PHP 相关文章推荐
PHP Mysql编程之高级技巧
Aug 27 PHP
php smarty函数扩展
Mar 15 PHP
在VS2008中编译MYSQL5.1.48的方法
Jul 03 PHP
array_multisort实现PHP多维数组排序示例讲解
Jan 04 PHP
PHP实现服务器状态监控的方法
Dec 09 PHP
php提示Warning:mysql_fetch_array() expects的解决方法
Dec 16 PHP
PHP生成条形码大揭秘
Sep 24 PHP
PHP处理Ajax请求与Ajax跨域问题
Feb 13 PHP
php批量删除操作代码分享
Feb 26 PHP
PHP基于接口技术实现简单的多态应用完整实例
Apr 26 PHP
PHP实现执行外部程序的方法详解
Aug 17 PHP
PHP自定义序列化接口Serializable用法分析
Dec 29 PHP
php短域名转换为实际域名函数
Jan 17 #PHP
PHP学习笔记之三 数据库基本操作
Jan 17 #PHP
PHP学习笔记之二
Jan 17 #PHP
PHP学习笔记之一
Jan 17 #PHP
php下连接mssql2005的代码
Jan 17 #PHP
Php Image Resize图片大小调整的函数代码
Jan 17 #PHP
php生成随机密码的几种方法
Jan 17 #PHP
You might like
PHP 程序授权验证开发思路
2009/07/09 PHP
PHP转换IP地址到真实地址的方法详解
2013/06/09 PHP
php+mysql实现简单的增删改查功能
2015/07/13 PHP
php实现zip文件解压操作
2015/11/03 PHP
PHP标准类(stdclass)用法示例
2016/09/28 PHP
Laravel框架中VerifyCsrfToken报错问题的解决
2017/08/30 PHP
利用php + Laravel如何实现部署自动化详解
2017/10/11 PHP
PHP实现链式操作的三种方法详解
2017/11/16 PHP
PHP实现统计代码行数小工具
2019/09/19 PHP
laravel通过a标签从视图向控制器实现传值
2019/10/15 PHP
JavaScript 操作键盘的Enter事件(键盘任何事件),兼容多浏览器
2010/10/11 Javascript
基于Jquery与WebMethod投票功能实现代码
2011/01/19 Javascript
js 实现css风格选择器(压缩后2KB)
2012/01/12 Javascript
jQuery中focus事件用法实例
2014/12/26 Javascript
Bootstrap布局组件教程之Bootstrap下拉菜单
2016/06/12 Javascript
Javascript OOP之面向对象
2016/07/31 Javascript
详解js实时获取并显示当前时间的方法
2019/05/10 Javascript
layer iframe 设置关闭按钮的方法
2019/09/12 Javascript
微信小程序实现录制、试听、上传音频功能(带波形图)
2020/02/27 Javascript
javascript的hashCode函数实现代码小结
2020/08/11 Javascript
[03:42]2014DOTA2西雅图国际邀请赛7月9日TOPPLAY
2014/07/09 DOTA
Python常用模块用法分析
2014/09/08 Python
python通过imaplib模块读取gmail里邮件的方法
2015/05/08 Python
Python实现制度转换(货币,温度,长度)
2019/07/14 Python
简单了解python调用其他脚本方法实例
2020/03/26 Python
在python中求分布函数相关的包实例
2020/04/15 Python
Django数据模型中on_delete使用详解
2020/11/30 Python
使用分层画布来优化HTML5渲染的教程
2015/05/08 HTML / CSS
美国滑雪板和装备购物网站:Skis.com
2018/12/20 全球购物
十佳文明家庭事迹
2014/05/25 职场文书
环境卫生标语
2014/06/09 职场文书
趣味运动会广播稿
2014/09/13 职场文书
实习协议书
2015/01/27 职场文书
大学生奶茶店创业计划书
2019/06/25 职场文书
用php如何解决大文件分片上传问题
2021/07/07 PHP
Python实现归一化算法详情
2022/03/18 Python