| | |||||||
| |||||||
| |||||||
|
|
|
|
|
|
|
I was curious as to how many line of code i had in my various projects. my projects usually span multipule directories so i needed to build-in directory walking. this function came as a result. By : mydimension <?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 } ?>
More Counters Code Articles |
| |
| |