I recently completed a Senior Design project and was asked by a grading professor to give him a lines of code count. I had time so I wrote this little bit of code to do it. The professor counts a "line" as any line that ends with a semicolon and wanted only the php and javascript files counted.
Let me know what you think of the design and if it could be tweeked and/or otherwise fiddled with.
<?php
// WOWBAGGER LINE COUNT //
function count_directory ( $directory )
{
$directory_stream = opendir($directory) or die("cannot open directory ($directory)!");
$total_count = 0;
while ( !(($file = readdir($directory_stream)) === false ) )
{
if ( is_dir("$directory/$file") && $file != ".." && $file != "." )
{
$total_count += count_directory("$directory/$file");
}
else if ( !is_dir("$directory/$file") && is_countable($file) )
{
$total_count += count_lines($file,$directory);
}
}
return $total_count;
}
function count_lines ( $file , $directory )
{
$lines = file("$directory/$file");
$total = count($lines);
$count = 0;
for ( $i = 0 ; $i < $total ; $i++ )
{
$lines[$i] = rtrim($lines[$i]);
if ( substr($lines[$i],-1,1) == ";" )
{
$count++;
}
}
return $count;
//return count(file("$directory/$file"));
}
function is_countable ( $file )
{
$extension = strtolower(array_pop(explode(".",$file)));
switch ($extension)
{
case "php" : return true;
case "js" : return true;
default : return false;
}
}
$directory = ($_GET['dir'] != NULL) ? $_GET['dir'] : ".";
print "Directory \"$directory\" contains " . count_directory($directory) . " total lines.<br><br>";
?>
Note: It defaults to the current directory, but you can also give it a specific sub directory if you'd like.