PHP - 如何计算应用程序中的代码行数
我需要计算我的应用程序中的代码行数(在 PHP 中,而不是命令行),并且由于网络上的代码片段没有太大帮助,所以我决定在这里询问。 感谢您的任何回复!
编辑
实际上,我需要整个片段来扫描和计算给定文件夹中的行数。我在 CakePHP 中使用这个方法,所以我希望无缝集成。
I need to count the number of lines of code within my application (in PHP, not command line), and since the snippets on the web didn't help too much, I've decided to ask here.
Thanks for any reply!
EDIT
Actually, I would need the whole snippet for scanning and counting lines within a given folder. I'm using this method in CakePHP, so I'd appreciate seamless integration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
要在目录上执行此操作,我将使用迭代器。
这将返回一个数组,其中每个文件作为键,行数作为值。然后,如果您只想要总数,只需执行
array_sum(countLines($path));
...To do it over a directory, I'd use an iterator.
That will return an array with each file as the key and the number of lines as the value. Then, if you want only a total, just do
array_sum(countLines($path));
...您可以使用
file
函数读取文件,然后count
:You can use the
file
function to read the file and thencount
:使用 ircmaxell 的代码,我用它做了一个简单的类,它现在对我来说非常有用
Using ircmaxell's code, I made a simple class out of it, it works great for me now
PHP Classes 有一个很好的类,用于计算目录中 php 文件的行数:
http://www.phpclasses.org/package/1091-PHP-Calculates-the-total-lines-of-code-in-a-directory.html
您可以在类的顶部指定要检查的文件类型。
PHP Classes has a nice class for counting lines for php files in a directory:
http://www.phpclasses.org/package/1091-PHP-Calculates-the-total-lines-of-code-in-a-directory.html
You can specify the file types you want to check at the top of the class.
https://github.com/sebastianbergmann/phploc
https://github.com/sebastianbergmann/phploc
有点脏,但你也可以使用 system / exec / passthru
wc -l *
a little dirty, but you can also use system / exec / passthru
wc -l *