Counters Code

  Home arrow Counters Code arrow countCodeLines
COUNTERS CODE

countCodeLines
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2002-11-01

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    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
    }
    ?>
    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.

    More Counters Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    COUNTERS CODE ARTICLES

    - TG Who's Online
    - Text Based Counter that formats output
    - Counter & visitor statistics
    - Server Uptime Statistics
    - Chris Dingman's Hit Tracker Script
    - Graph Maker Function
    - Simple userOnline class
    - Adv. Log file generator
    - Logwriter
    - time_left()
    - Basic Statistics
    - Easy Counter
    - countCodeLines
    - MySQL Counter
    - bandwidthmeter

    Developer Shed Affiliates

     



    © 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap