计算 PHP 项目中的行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/790956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
count lines in a PHP project
提问by Duncan Benoit
Do you know any tool which can count all the code lines from a PHP project?
你知道有什么工具可以计算 PHP 项目中的所有代码行吗?
回答by Henrik Paul
On a POSIX operating system (e.g. Linux or OS X) you can write the following into your Bash shell:
在 POSIX 操作系统(例如 Linux 或 OS X)上,您可以将以下内容写入 Bash shell:
wc -l `find . -iname "*.php"`
This will count the lines in all php-files in the current directory and also subdirectories. (Note that those single 'quotes' are backticks, not actual single quotes)
这将计算当前目录和子目录中所有 php 文件中的行数。(请注意,那些单引号是反引号,而不是实际的单引号)
回答by Joel Lord
I made myself a small script to do that in one of my projects. Simply use the following code on a php page in the root of your project. The script will do recursive check on sub folders.
我为自己制作了一个小脚本,以便在我的一个项目中做到这一点。只需在项目根目录的 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: [email protected]
*/
$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;
}
回答by moinudin
SLOCCountis an awesome tool that produces a line-count report for a large number of languages. It also goes further by producing other, related statistics such as expected developer cost.
SLOCCount是一个很棒的工具,可以为大量语言生成行数报告。它还进一步生成其他相关统计数据,例如预期的开发人员成本。
Here's an example:
下面是一个例子:
$ 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 = ,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'."
回答by Shabbyrobe
Unfortunately, SLOCCount is a bit long in the tooth and a pain in the neck for PHP projects, particularly ones that have a nested vendordirectory you don't want counted. Also, it emits a warning for every PHP file that doesn't have a closing tag (which should be all of them if you aren't mixing HTML and PHP).
不幸的是,SLOCCount 有点长,而且对于 PHP 项目来说很头疼,尤其是那些具有vendor您不想计算的嵌套目录的项目。此外,它会为每个没有结束标记的 PHP 文件发出警告(如果您不混合使用 HTML 和 PHP,则应该是所有结束标记)。
CLOCis a more modern alternative that does everything (edit: nearly everything) SLOCCount does, but also supports an --exclude-diroption and it doesn't suffer from the aforementioned close tag problem. It also emits a SQLite database that you can extract some pretty advanced metrics from.
CLOC是一个更现代的替代方案,它可以做所有事情(编辑:几乎所有事情)SLOCCount 可以做,但也支持一个--exclude-dir选项,它不会受到前面提到的关闭标签问题的影响。它还发出一个 SQLite 数据库,您可以从中提取一些非常高级的指标。
回答by weston
On windows from a command line:
在 Windows 上从命令行:
findstr /R /N "^" *.php | find /C ":"
Thanks to this article.
感谢这篇文章。
To include sub directories, use \s:
要包含子目录,请使用\s:
findstr /s /R /N "^" *.php | find /C ":"
回答by Bengt
The SLOCs of a PHP-project can counted with sloccountusing something like this:
PHP 项目的SLOC可以使用sloccount计算,如下所示:
find . -not -wholename '*/libraries/*' -not -wholename '*/lib/*' -not -wholename '*/vendor/*' -type f xargs sloccount
Sample output for a sizey drupal project:
大型 drupal 项目的示例输出:
[...]
SLOC Directory SLOC-by-Language (Sorted)
44892 top_dir pascal=33176,php=10293,sh=1423
Totals grouped by language (dominant language first):
pascal: 33176 (73.90%)
php: 10293 (22.93%)
sh: 1423 (3.17%)
Total Physical Source Lines of Code (SLOC) = 44,892
Development Effort Estimate, Person-Years (Person-Months) = 10.86 (130.31)
(Basic COCOMO model, Person-Months = 2.4 * (KSLOC**1.05))
Schedule Estimate, Years (Months) = 1.33 (15.91)
(Basic COCOMO model, Months = 2.5 * (person-months**0.38))
Estimated Average Number of Developers (Effort/Schedule) = 8.19
Total Estimated Cost to Develop = $ 1,466,963
(average salary = ,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'."
回答by Shahbaz
<?php
passthru('wc -l `find . -iname "*.php"`');
?>
Just run this on your current directory where all the php files are placed, it will display count lines on browser.
只需在放置所有 php 文件的当前目录上运行它,它就会在浏览器上显示计数行。

