php统计项目目录代码行数

<?php

/**
 * @author xiaoxia xu <x_824@sina.com> 2011-1-12
 * @link http://www.phpwind.com
 * @copyright Copyright &copy; 2003-2110 phpwind.com
 * @license
 * 统计目录下的文件行数及总文件数··去除注释
 */


$fileExt = ".php";
$filePath = "D:\install\wamp\www\qshx\Lawyer\Home";
if (!is_dir($filePath)) exit('Path error!');
list($totalnum, $linenum, $filenum) = readGiveDir($filePath, $fileExt);
echo "*************************************************************\r\n";
echo "总行数: " . $totalnum . "\r\n";
echo "总有效行数: " . $linenum . "\r\n";
echo '总文件个数:' . $filenum;


function readGiveDir($dir, $fileExt) {
    $totalnum = 0;
    $linenum = 0;
    $filenum = 0;
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if (in_array($file, array(".", "..", '.svn'))) continue;
            if (is_dir($dir . '/' . $file)) {
                list($num1, $num2, $num3) = readGiveDir($dir . '/' . $file, $fileExt);
                $totalnum += $num1;
                $linenum += $num2;
                $filenum += $num3;
            } else {
                if (strrchr($file, '.') == $fileExt) {
                    list($num1, $num2) = readfiles($dir . '/' . $file);
                    $totalnum += $num1;
                    $linenum += $num2;
                    $filenum ++;
                    continue;
                }
            }
        }
        closedir($dh);
    } else {
        echo 'open dir <' . $dir . '> error!' . "\r";
    }
    return array($totalnum, $linenum, $filenum);
}

function readfiles($file) {
    echo $file . "\r\n";
    //$p = php_strip_whitespace($file);
    $str = file($file);
    $linenum = 0;
    foreach ($str as $value) {
        if (skip(trim($value))) continue;
        $linenum ++;
    }
    $totalnum = count(file($file));
    echo '行数:' . $totalnum . "\r\n";
    echo '有效行数:' . $linenum . "\r\n";
    return array($totalnum, $linenum);
}

function skip($string) {
    if ($string == '') return true;
    $array = array("*", "/*", "//", "#");
    foreach ($array as $tag) {
        if (strpos($string, $tag) === 0) return true;
    }
    return false;
}

Tags: PHP

仅有一条评论

  1. 软件

    windows推荐使用cloc.exe软件
    https://github.com/AlDanial/cloc

添加新评论