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.

linkyndy
  • 17,038
  • 20
  • 114
  • 194
  • 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. – Marc B Dec 02 '10 at 19:08
  • All lines, whether they contain code, comments or are blank. – linkyndy Dec 02 '10 at 19:56

7 Answers7

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

ircmaxell
  • 163,128
  • 34
  • 264
  • 314
  • 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). – linkyndy Dec 02 '10 at 20:29
  • 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. – salathe Dec 03 '10 at 10:45
  • @linkyndy: `RecursiveIteratorIterator` and `RecursiveDirectoryIterator` are both [core PHP classes](http://us.php.net/manual/en/spl.iterators.php) 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)... – ircmaxell Dec 03 '10 at 13:33
  • I get the following error: `Fatal error: Call to undefined method SplFileInfo::isDot() in` - Why??? – Pathros Mar 07 '17 at 21:57
4

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

$c = count(file('filename.php'));
elitalon
  • 9,191
  • 10
  • 50
  • 86
Srisa
  • 967
  • 1
  • 7
  • 17
3
$fp = "file.php";
$lines = file($fp);
echo count($lines);
Ibrahim
  • 1,247
  • 3
  • 13
  • 21
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();

?>
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
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.

wajiw
  • 12,239
  • 17
  • 54
  • 73
  • 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 :) – linkyndy Dec 02 '10 at 20:00
  • You can always look at the code and implement the parts you want, it's pretty small :-) – wajiw Dec 02 '10 at 20:14
1

https://github.com/sebastianbergmann/phploc

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
0

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

Aif
  • 11,015
  • 1
  • 30
  • 44