PHP 统计代码行数

  最后我还是用PHP写了,BAT的那些变量太麻烦了,而且统计的还不准确,不知道为什么。

  PHP的,功能应该足够了,信息输出、之定义拓展名,递归统计。当然使用的是命令行,web访问可不靠谱,所以接受部分参数。当然没去注释什么的,要去注释就先去注释,然后格式化再统计吧,貌似去注释的正则好麻烦的。

<?php
if (isset($argv[1])) {
    $path = $argv[1];
} else {
    $path = "";
}
if (empty($path) || !is_dir($path)) {
    //路径判断
    die("Usage: PHP {$argv[0]} path [-ro] [*|ext,ext2..]\r\n");
}
$out = $r = false;
if (isset($argv[2])) {
    //对是否递归等操作进行参数判断
    $param = str_split(strtolower(trim($argv[2])));
    if (in_array('r', $param)) {
        $r = true;
    }
    if (in_array('o', $param)) {
        $out = true;
    }
}


$ex = [];
if (isset($argv[3]) && $argv[3] != "*") {
    //取拓展名
    //默认为全部类型
    $ex = array_flip(array_flip(array_map('trim', explode(",", strtolower($argv[3])))));
}

$count = read_dir($path, $r, $ex, $out);

echo "\r\n-----------------------------------\r\n--- ALL Count: {$count}\r\n-----------------------------------";

/**
 * 读取目录
 * @param string $path 文件夹路径
 * @param bool $r 是否递归读取
 * @param array $ex 拓展名列表
 * @param bool $out 是否输出详细信息
 * @param int $deep 当前递归深度
 * @return int 当前文件夹代码行数
 */

function read_dir($path, $r = false, $ex = [], $out = false, $deep = 0)
{
    $handler = opendir($path);
    $files = [];
    $dirs = [];
    if ($handler) {
        while ($file = readdir($handler)) {
            if ($file[0] == '.')
                continue;
            $file_path = $path . "/" . $file;
            if (is_file($file_path)) {
                if (count($ex) == 0 || in_array(strtolower(pathinfo($file_path, PATHINFO_EXTENSION)), $ex)) {
                    $files[] = $file_path;
                }
            } else if ($r && is_dir($file_path)) {
                //判断是递归且是否是文件夹
                $dirs[] = $file_path;
            }
            //TODO:目录链接操作
        }
        closedir($handler);
    }
    $c = count($files) + count($dirs);
    if ($c && $out) {
        echo str_repeat("│   ", $deep) . "┌─ $path\r\n";
    }
    $count = 0;
    foreach ($dirs as $v) {
        //对文件夹递归
        $count += read_dir($v, $r, $ex, $out, $deep + 1);
    }
    foreach ($files as $v) {
        //对文件统计
        $count += count_file($v, $out, $deep);
    }
    if ($c && $out) {
        echo str_repeat("│   ", $deep) . "└─ DIR: $path:$count\r\n" .
            str_repeat("│   ", $deep) . "\r\n";
    }
    return $count;
}


/**
 * 读取文件行数
 * @param string $file 文件路径
 * @param bool $out 是否输出详情
 * @param  int $deep 深度
 * @return int 文件行数
 */

function count_file($file, $out = false, $deep)
{
    $c = 0;
    //读取文件并分割
    $content = file_get_contents($file);
    $x = explode("\n", $content);
    foreach ($x as $v) {
        //统计非空白行
        if (strlen(trim($v)) > 0) {
            ++$c;
        }
    }
    if ($out) {
        echo str_repeat("│   ", $deep + 1) . "FILE: " . $file . ":" . $c . "\r\n";
    }
    return $c;
}

BAT的脚本也分享一下吧,仅供参考。

@echo off
set /a count=0

call :process %1
goto :end

:process
::查询目录中的文件列表
for /f "delims=" %%a in ('dir /a-d /b %1\*.php') do (
    set /a v=0
    for /f %%b in ('type %1\%%a') do (
        if "%%b" NEQ "" (
            set /a count+=1
        )
    )
)
::查询文件夹列表
for /f "delims=" %%a in ('dir /ad /b %1') do (
    ::判断是否存在文件夹
    if exist "%1\%%a" (
        echo -------------------------------------------
        echo DIR%1\%%a
        ::开始进行递归操作
        call :process %1\%%a
    )
)
goto :eof

:end
echo ---------------------------------------
echo Count:%count%

最后给个统计信息的输出例子,系统的视图部分的代码统计

┌─.\sys\view
│   ┌─.\sys\view/Comment
│   │   FILE:.\sys\view/Comment/comment.php:34
│   │   FILE:.\sys\view/Comment/display.php:152
│   └─ DIR:.\sys\view/Comment:186
│   
│   ┌─.\sys\view/Control
│   │   FILE:.\sys\view/Control/main.php:52
│   │   FILE:.\sys\view/Control/main_show.php:1
│   │   FILE:.\sys\view/Control/message.php:97
│   │   FILE:.\sys\view/Control/message_send.php:83
│   │   FILE:.\sys\view/Control/option.php:130
│   │   FILE:.\sys\view/Control/permalink.php:157
│   │   FILE:.\sys\view/Control/pic_server.php:79
│   │   FILE:.\sys\view/Control/pic_server_edit.php:73
│   │   FILE:.\sys\view/Control/thumbnail.php:47
│   │   FILE:.\sys\view/Control/user.php:69
│   │   FILE:.\sys\view/Control/user_add.php:39
│   │   FILE:.\sys\view/Control/user_edit.php:35
│   │   FILE:.\sys\view/Control/user_permission.php:30
│   └─ DIR:.\sys\view/Control:892
│   
│   ┌─.\sys\view/Follow
│   │   FILE:.\sys\view/Follow/comment_chose.php:8
│   │   FILE:.\sys\view/Follow/comment_show.php:36
│   │   FILE:.\sys\view/Follow/error.php:6
│   │   FILE:.\sys\view/Follow/feed.php:114
│   │   FILE:.\sys\view/Follow/gallery.php:83
│   │   FILE:.\sys\view/Follow/main.php:8
│   │   FILE:.\sys\view/Follow/me.php:67
│   │   FILE:.\sys\view/Follow/mutual.php:68
│   │   FILE:.\sys\view/Follow/ta.php:48
│   └─ DIR:.\sys\view/Follow:438
│   
│   ┌─.\sys\view/Home
│   │   FILE:.\sys\view/Home/404.php:11
│   │   FILE:.\sys\view/Home/footer.php:14
│   │   FILE:.\sys\view/Home/forget_password.php:51
│   │   FILE:.\sys\view/Home/header.php:90
│   │   FILE:.\sys\view/Home/login.php:84
│   │   FILE:.\sys\view/Home/logout_error.php:2
│   │   FILE:.\sys\view/Home/permission.php:4
│   │   FILE:.\sys\view/Home/register.php:159
│   │   FILE:.\sys\view/Home/reset_password.php:65
│   │   FILE:.\sys\view/Home/sql_error.php:17
│   └─ DIR:.\sys\view/Home:497
│   
│   ┌─.\sys\view/MailTemplate
│   │   ┌─.\sys\view/MailTemplate/mail_notice
│   │   │   FILE:.\sys\view/MailTemplate/mail_notice/comment_gallery.html:10
│   │   │   FILE:.\sys\view/MailTemplate/mail_notice/comment_picture.html:11
│   │   │   FILE:.\sys\view/MailTemplate/mail_notice/follow_gallery.html:7
│   │   │   FILE:.\sys\view/MailTemplate/mail_notice/follow_me.html:7
│   │   │   FILE:.\sys\view/MailTemplate/mail_notice/like_comment.html:12
│   │   │   FILE:.\sys\view/MailTemplate/mail_notice/like_gallery.html:10
│   │   │   FILE:.\sys\view/MailTemplate/mail_notice/like_picture.html:10
│   │   │   FILE:.\sys\view/MailTemplate/mail_notice/login_restrictions.html:10
│   │   │   FILE:.\sys\view/MailTemplate/mail_notice/reply_comment.html:13
│   │   │   FILE:.\sys\view/MailTemplate/mail_notice/send_message.html:9
│   │   │   FILE:.\sys\view/MailTemplate/mail_notice/send_system_message.html:12
│   │   └─ DIR:.\sys\view/MailTemplate/mail_notice:111
│   │   
│   │   ┌─.\sys\view/MailTemplate/message_notice
│   │   │   FILE:.\sys\view/MailTemplate/message_notice/comment_gallery.md:7
│   │   │   FILE:.\sys\view/MailTemplate/message_notice/comment_picture.md:8
│   │   │   FILE:.\sys\view/MailTemplate/message_notice/follow_gallery.md:4
│   │   │   FILE:.\sys\view/MailTemplate/message_notice/follow_me.md:4
│   │   │   FILE:.\sys\view/MailTemplate/message_notice/like_comment.md:9
│   │   │   FILE:.\sys\view/MailTemplate/message_notice/like_gallery.md:6
│   │   │   FILE:.\sys\view/MailTemplate/message_notice/like_picture.md:7
│   │   │   FILE:.\sys\view/MailTemplate/message_notice/login_restrictions.md:7
│   │   │   FILE:.\sys\view/MailTemplate/message_notice/reply_comment.md:10
│   │   └─ DIR:.\sys\view/MailTemplate/message_notice:62
│   │   
│   │   FILE:.\sys\view/MailTemplate/activation.html:8
│   │   FILE:.\sys\view/MailTemplate/edit_email.html:7
│   │   FILE:.\sys\view/MailTemplate/new_user_registered.html:10
│   │   FILE:.\sys\view/MailTemplate/reset_password.html:7
│   └─ DIR:.\sys\view/MailTemplate:205
│   
│   ┌─.\sys\view/Message
│   │   FILE:.\sys\view/Message/inbox.php:51
│   │   FILE:.\sys\view/Message/main.php:39
│   │   FILE:.\sys\view/Message/option.php:44
│   │   FILE:.\sys\view/Message/outbox.php:39
│   │   FILE:.\sys\view/Message/view.php:57
│   └─ DIR:.\sys\view/Message:230
│   
│   ┌─.\sys\view/Photo
│   │   FILE:.\sys\view/Photo/add_gallery.php:33
│   │   FILE:.\sys\view/Photo/add_pic.php:212
│   │   FILE:.\sys\view/Photo/add_pic2.php:279
│   │   FILE:.\sys\view/Photo/comment_show.php:48
│   │   FILE:.\sys\view/Photo/edit_gallery.php:334
│   │   FILE:.\sys\view/Photo/edit_pic.php:141
│   │   FILE:.\sys\view/Photo/list_gallery.php:92
│   │   FILE:.\sys\view/Photo/list_pic.php:119
│   │   FILE:.\sys\view/Photo/select_user_pic.php:149
│   └─ DIR:.\sys\view/Photo:1407
│   
│   ┌─.\sys\view/Posts
│   │   FILE:.\sys\view/Posts/comment_show.php:48
│   │   FILE:.\sys\view/Posts/edit.php:100
│   │   FILE:.\sys\view/Posts/management.php:69
│   │   FILE:.\sys\view/Posts/not_found.php:8
│   │   FILE:.\sys\view/Posts/post.php:31
│   └─ DIR:.\sys\view/Posts:256
│   
│   ┌─.\sys\view/Show
│   │   FILE:.\sys\view/Show/gallery.php:142
│   │   FILE:.\sys\view/Show/gallery_list.php:66
│   │   FILE:.\sys\view/Show/home.php:38
│   │   FILE:.\sys\view/Show/picture.php:98
│   │   FILE:.\sys\view/Show/post.php:18
│   │   FILE:.\sys\view/Show/post_list.php:46
│   │   FILE:.\sys\view/Show/time_line.php:55
│   │   FILE:.\sys\view/Show/user.php:93
│   │   FILE:.\sys\view/Show/user_header.php:17
│   └─ DIR:.\sys\view/Show:573
│   
│   ┌─.\sys\view/User
│   │   FILE:.\sys\view/User/activation.php:80
│   │   FILE:.\sys\view/User/edit_avatar.php:161
│   │   FILE:.\sys\view/User/edit_email.php:106
│   │   FILE:.\sys\view/User/edit_info.php:72
│   │   FILE:.\sys\view/User/edit_password.php:117
│   │   FILE:.\sys\view/User/footer.php:16
│   │   FILE:.\sys\view/User/header.php:83
│   │   FILE:.\sys\view/User/user_info.php:45
│   └─ DIR:.\sys\view/User:680
│   
└─ DIR:.\sys\view:5364


-----------------------------------
--- ALL Count: 5364
-----------------------------------

7条评论在“PHP 统计代码行数”

写下你最简单的想法