PHP - How to count lines of code in an application [duplicate]
Asked Answered
A

7

1

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.

Argueta answered 2/12, 2010 at 19:0 Comment(2)
Do you want actual lines of code? Including/Excluding comments? Inc/Exc blanks? Inc/Exc multi-line strings? There's many different definitions of "line" when it comes to source code.Seemaseeming
All lines, whether they contain code, comments or are blank.Argueta
A
5

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

Athens answered 2/12, 2010 at 20:18 Comment(4)
Thanks you for your answer. I didn't quite get what the RecursiveIteratorIterator class does and if it could be replaced with something that could easily integrate with CakePHP (As I mentioned in the question, I would appreciate not including any other 'foreign' class in my application).Argueta
The 'foreign' class is a part of PHP itself. As for integrating with Cake, you can't get much simpler than copy-pasting that function.Vandalism
@linkyndy: RecursiveIteratorIterator and RecursiveDirectoryIterator are both core PHP classes since PHP 5.0.0. There's no need to include any foreign classes. Just copy and past the code above and it will work for any framework (since it's just raw PHP)...Athens
I get the following error: Fatal error: Call to undefined method SplFileInfo::isDot() in - Why???Karena
R
4

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

$c = count(file('filename.php'));
Rupertruperta answered 2/12, 2010 at 19:3 Comment(1)
I've edited the question since it was not very clear :)Argueta
C
3
$fp = "file.php";
$lines = file($fp);
echo count($lines);
Chiccory answered 2/12, 2010 at 19:9 Comment(1)
I've edited the question since it was not very clear :)Argueta
S
3

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

?>
Sholokhov answered 19/7, 2011 at 22:32 Comment(0)
H
1

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.

Horologium answered 2/12, 2010 at 19:7 Comment(2)
Thank you for your suggestion, but since I am integrating this in CakePHP, it will be more appropriate if I don't import a separate class for achieving this :)Argueta
You can always look at the code and implement the parts you want, it's pretty small :-)Horologium
B
1

https://github.com/sebastianbergmann/phploc

Burnley answered 2/12, 2010 at 19:27 Comment(3)
I need to integrate it easier with CakePHP :)Argueta
Then you can look at the code and integrate it.Burnley
This repository has been archived by the owner on Jan 10, 2023. But Sebastian Bergmann created similar tool: github.com/sebastianbergmann/lines-of-codeBeller
V
0

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

Villanueva answered 2/12, 2010 at 19:5 Comment(1)
I mentioned that I need a PHP snippet, not a command line input. Sorry!Argueta

© 2022 - 2024 — McMap. All rights reserved.