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);
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 } ?>
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.