Mega Code Archive

 
Categories / Php / Statistics and Counters
 

Count Code Lines

<?php /* What it counts: all lines containing a semi-colon and all lines containing either an opened brace or a closed brace BUT NOT both. What it dosen't count: lines terminated by a newline. Syntax: int countCodeLines( string $directory ) */ function countCodeLines ($directory) { $curDir = getcwd(); chdir($directory); static $totalCodeLines; $dir = opendir($directory); while ($item = readdir($dir)) { if ((is_dir($item)) && ($item != ".") && ($item != "..")) { //check to see if we need to walk into another directory countCodeLines(realpath($item)); //recursive directory walking } elseif (strrchr($item, ".") == ".php") { //count only php files $lines = file($directory . "/" . $item); foreach ($lines as $line) { //count lines containg a semi-colon or //lines containing either an opened brace or a closed brace BUT NOT both if (preg_match("/\;/", $line) xor (preg_match("/\{/", $line) xor preg_match("/\}/", $line))) { $totalCodeLines++; } } } } chdir($curDir); return $totalCodeLines; //return the final count } ?>