计算PHP项目中的行数

Dun*_*oit 57 php count line

你知道任何可以计算PHP项目中所有代码行的工具吗?

Hen*_*aul 99

在POSIX操作系统(例如Linux或OS X)上,您可以将以下内容写入Bash shell:

wc -l `find . -iname "*.php"`
Run Code Online (Sandbox Code Playgroud)

这将计算当前目录和子目录中所有php文件中的行.(请注意,那些单个'引号'是反引号,而不是实际的单引号)

  • 如果你在Windows上,你可以下载并安装Cygwin,并做同样的事情.由于Mac现在也运行在BSD操作系统之上,我认为这是明确的答案. (2认同)

Joe*_*ord 21

我在自己的一个项目中做了一个小脚本.只需在项目根目录的php页面上使用以下代码即可.该脚本将对子文件夹进行递归检查.

<?php
/**
 * A very simple stats counter for all kind of stats about a development folder
 * 
 * @author Joel Lord
 * @copyright Engrenage (www.engrenage.biz)
 * 
 * For more information: joel@engrenage.biz

 */


$fileCounter = array();
$totalLines = countLines('.', $fileCounter); 
echo $totalLines." lines in the current folder<br>";
echo $totalLines - $fileCounter['gen']['commentedLines'] - $fileCounter['gen']['blankLines'] ." actual lines of code (not a comment or blank line)<br><br>";

foreach($fileCounter['gen'] as $key=>$val) {
    echo ucfirst($key).":".$val."<br>";
}

echo "<br>";

foreach($fileCounter as $key=>$val) {
    if(!is_array($val)) echo strtoupper($key).":".$val." file(s)<br>";
}




function countLines($dir, &$fileCounter) {
    $_allowedFileTypes = "(html|htm|phtml|php|js|css|ini)";
    $lineCounter = 0;
    $dirHandle = opendir($dir);
    $path = realpath($dir);
    $nextLineIsComment = false;

    if($dirHandle) {
        while(false !== ($file = readdir($dirHandle))) {
            if(is_dir($path."/".$file) && ($file !== '.' && $file !== '..')) {
                $lineCounter += countLines($path."/".$file, $fileCounter);
            } elseif($file !== '.' && $file !== '..') {
                //Check if we have a valid file 
                $ext = _findExtension($file);
                if(preg_match("/".$_allowedFileTypes."$/i", $ext)) {
                    $realFile = realpath($path)."/".$file;
                    $fileHandle = fopen($realFile, 'r');
                    $fileArray = file($realFile);
                    //Check content of file:
                    for($i=0; $i<count($fileArray); $i++) {
                        if($nextLineIsComment) {
                            $fileCounter['gen']['commentedLines']++;
                            //Look for the end of the comment block
                            if(strpos($fileArray[$i], '*/')) {
                                $nextLineIsComment = false;
                            }
                        } else {
                            //Look for a function
                            if(strpos($fileArray[$i], 'function')) {
                                $fileCounter['gen']['functions']++;
                            }
                            //Look for a commented line
                            if(strpos($fileArray[$i], '//')) {
                                $fileCounter['gen']['commentedLines']++;
                            }
                            //Look for a class
                            if(substr(trim($fileArray[$i]), 0, 5) == 'class') {
                                $fileCounter['gen']['classes']++;
                            }
                            //Look for a comment block
                            if(strpos($fileArray[$i], '/*')) {
                                $nextLineIsComment = true;
                                $fileCounter['gen']['commentedLines']++;
                                $fileCounter['gen']['commentBlocks']++;
                            }
                            //Look for a blank line
                            if(trim($fileArray[$i]) == '') {
                                $fileCounter['gen']['blankLines']++;
                            }
                        }

                    }
                    $lineCounter += count($fileArray);
                }
                //Add to the files counter
                $fileCounter['gen']['totalFiles']++;
                $fileCounter[strtolower($ext)]++;
            }
        }
    } else echo 'Could not enter folder';

    return $lineCounter;
}

function _findExtension($filename) {
    $filename = strtolower($filename) ; 
    $exts = split("[/\\.]", $filename) ; 
    $n = count($exts)-1; 
    $exts = $exts[$n]; 
    return $exts;  
}
Run Code Online (Sandbox Code Playgroud)

  • 我对该代码段进行了大量修改,并将其发布到github。它修复了所有警告,并允许在计数期间排除目录。https://raw.githubusercontent.com/danielson317/php-code-stats/master/code_stats.php (3认同)

mar*_*cog 13

SLOCCount是一个很棒的工具,可以为大量语言生成行计数报告.它还通过产生其他相关统计数据(例如预期的开发者成本)进一步发展.

这是一个例子:

$ sloccount .
Creating filelist for experimental
Creating filelist for prototype
Categorizing files.
Finding a working MD5 command....
Found a working MD5 command.
Computing results.


SLOC    Directory   SLOC-by-Language (Sorted)
10965   experimental    cpp=5116,ansic=4976,python=873
832     prototype       cpp=518,tcl=314


Totals grouped by language (dominant language first):
cpp:           5634 (47.76%)
ansic:         4976 (42.18%)
python:         873 (7.40%)
tcl:            314 (2.66%)




Total Physical Source Lines of Code (SLOC)                = 11,797
Development Effort Estimate, Person-Years (Person-Months) = 2.67 (32.03)
 (Basic COCOMO model, Person-Months = 2.4 * (KSLOC**1.05))
Schedule Estimate, Years (Months)                         = 0.78 (9.33)
 (Basic COCOMO model, Months = 2.5 * (person-months**0.38))
Estimated Average Number of Developers (Effort/Schedule)  = 3.43
Total Estimated Cost to Develop                           = $ 360,580
 (average salary = $56,286/year, overhead = 2.40).
SLOCCount, Copyright (C) 2001-2004 David A. Wheeler
SLOCCount is Open Source Software/Free Software, licensed under the GNU GPL.
SLOCCount comes with ABSOLUTELY NO WARRANTY, and you are welcome to
redistribute it under certain conditions as specified by the GNU GPL license;
see the documentation for details.
Please credit this data as "generated using David A. Wheeler's 'SLOCCount'."
Run Code Online (Sandbox Code Playgroud)


Sha*_*obe 11

不幸的是,SLOCCount有点长,对于PHP项目来说是一个痛苦的问题,特别是那些有vendor你不想要的嵌套目录的项目.此外,它会为每个没有结束标记的PHP文件发出警告(如果您不混合使用HTML和PHP,则应该是所有这些文件都应该是这样).

CLOC是一个更现代化的替代方案,可以完成SLOCCount所做的一切(编辑:几乎所有内容),但也支持一个--exclude-dir选项,它不会受到上述关闭标记问题的影响.它还会发出一个SQLite数据库,您可以从中提取一些非常高级的指标.


wes*_*ton 6

在命令行的窗口上:

findstr /R /N "^" *.php | find /C ":"
Run Code Online (Sandbox Code Playgroud)

感谢这篇文章.

要包含子目录,请使用\s :

findstr /s /R /N "^" *.php | find /C ":"
Run Code Online (Sandbox Code Playgroud)