PHP - 如何计算应用程序中的代码行数

发布于 2024-10-06 09:21:04 字数 175 浏览 0 评论 0原文

我需要计算我的应用程序中的代码行数(在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

み格子的夏天 2024-10-13 09:21:04

要在目录上执行此操作,我将使用迭代器。

function countLines($path, $extensions = array('php')) {
    $it = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($path)
    );
    $files = array();
    foreach ($it as $file) {
        if ($file->isDir() || $file->isDot()) {
            continue;
        }
        $parts = explode('.', $file->getFilename());
        $extension = end($parts);
        if (in_array($extension, $extensions)) {
            $files[$file->getPathname()] = count(file($file->getPathname()));
        }
    }
    return $files;
}

这将返回一个数组,其中每个文件作为键,行数作为值。然后,如果您只想要总数,只需执行 array_sum(countLines($path));...

To do it over a directory, I'd use an iterator.

function countLines($path, $extensions = array('php')) {
    $it = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($path)
    );
    $files = array();
    foreach ($it as $file) {
        if ($file->isDir() || $file->isDot()) {
            continue;
        }
        $parts = explode('.', $file->getFilename());
        $extension = end($parts);
        if (in_array($extension, $extensions)) {
            $files[$file->getPathname()] = count(file($file->getPathname()));
        }
    }
    return $files;
}

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));...

芸娘子的小脾气 2024-10-13 09:21:04

您可以使用file函数读取文件,然后count

$c = count(file('filename.php'));

You can use the file function to read the file and then count:

$c = count(file('filename.php'));
计㈡愣 2024-10-13 09:21:04
$fp = "file.php";
$lines = file($fp);
echo count($lines);
$fp = "file.php";
$lines = file($fp);
echo count($lines);
沐歌 2024-10-13 09:21:04

使用 ircmaxell 的代码,我用它做了一个简单的类,它现在对我来说非常有用

<?php
class Line_Counter
{
    private $filepath;
    private $files = array();

    public function __construct($filepath)
    {
        $this->filepath = $filepath;
    }

    public function countLines($extensions = array('php'))
    {
        $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->filepath));
        foreach ($it as $file)
        {
           // if ($file->isDir() || $file->isDot())
           if ($file->isDir() )
            {
                continue;
            }
            $parts = explode('.', $file->getFilename());
            $extension = end($parts);
            if (in_array($extension, $extensions))
            {
                $files[$file->getPathname()] = count(file($file->getPathname()));
            }
        }
        return $files;
    }

    public function showLines()
    {
        echo '<pre>';
        print_r($this->countLines());
        echo '</pre>';
    }

    public function totalLines()
    {
        return array_sum($this->countLines());
    }

}

// Get all files with line count for each into an array
$loc = new Line_Counter('E:\Server\htdocs\myframework');
$loc->showLines();

echo '<br><br> Total Lines of code: ';
echo $loc->totalLines();

?>

Using ircmaxell's code, I made a simple class out of it, it works great for me now

<?php
class Line_Counter
{
    private $filepath;
    private $files = array();

    public function __construct($filepath)
    {
        $this->filepath = $filepath;
    }

    public function countLines($extensions = array('php'))
    {
        $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->filepath));
        foreach ($it as $file)
        {
           // if ($file->isDir() || $file->isDot())
           if ($file->isDir() )
            {
                continue;
            }
            $parts = explode('.', $file->getFilename());
            $extension = end($parts);
            if (in_array($extension, $extensions))
            {
                $files[$file->getPathname()] = count(file($file->getPathname()));
            }
        }
        return $files;
    }

    public function showLines()
    {
        echo '<pre>';
        print_r($this->countLines());
        echo '</pre>';
    }

    public function totalLines()
    {
        return array_sum($this->countLines());
    }

}

// Get all files with line count for each into an array
$loc = new Line_Counter('E:\Server\htdocs\myframework');
$loc->showLines();

echo '<br><br> Total Lines of code: ';
echo $loc->totalLines();

?>
孤独岁月 2024-10-13 09:21:04

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.

糖粟与秋泊 2024-10-13 09:21:04

有点脏,但你也可以使用 system / exec / passthru wc -l *

a little dirty, but you can also use system / exec / passthru wc -l *

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文