Page 1 of 1

Code Snippet: Q & D Source Statistics

Posted: Fri Dec 29, 2006 3:09 pm
by alex.barylski
This is a quick and dirty code analysis I just threw togather as I'm always curious of progress I've made on projects...

It's not 100% accurate, as SLOC is *only* what I consider an actual line of code...it would be trivial to add a counter for comments and perform calculations such as comments/line ratios, etc...

I simply wanted something quick and dirty to give me an idea of how many lines I have removed from an existing project...

Use:

Line: 35 globr()
- first parameter is the directory you wish to traverse relative to the executing script
- second parameter is the filter list the example is obvious???

Problems:

It's slow on large projects. You will likely need to increase the time PHP is allowed to execute. It barely finished on my project which is just over 30,000 physical lines.

Code: Select all

<?php
  
  //
  // Borrowed from php.net/glob 
  function globr($sDir, $sPattern, $nFlags = NULL) 
  { 
    $sDir = escapeshellcmd($sDir); 
  
    // Get the list of all matching files currently in the 
    // directory. 
  
    $aFiles = glob("$sDir/$sPattern", $nFlags); 
  
    // Then get a list of all directories in this directory, and 
    // run ourselves on the resulting array.  This is the 
    // recursion step, which will not execute if there are no 
    // directories. 
  
    foreach (glob("$sDir/*", GLOB_ONLYDIR) as $sSubDir) 
    { 
     $aSubFiles = globr($sSubDir, $sPattern, $nFlags); 
     $aFiles = array_merge($aFiles, $aSubFiles); 
    } 
  
    // The array we return contains the files we found, and the 
    // files all of our children found. 
  
    return $aFiles; 
  } 

  $sts = 0; // Statements
  $con = 0; // Keywords
  $lin = 0; // Actual physical lines
  $siz = 0;
  $dir = globr('webedit', '{*.php,*.inc}', GLOB_BRACE);  
  
  // Iterate files which match above glob()
  for($h=0; $h<count($dir); $h++){
    $src = file_get_contents($dir[$h]);
    $arr = token_get_all($src);  
    $cnt = count($arr);
    
    $siz += strlen($src);
    
    // Physical line counter
    for($i=0; $i<strlen($src); $i++){
      if(ord($src[$i]) == 10) $lin++;
    }
    
    // Iterate tokens
    for($i=0; $i<$cnt; $i++){
      if($arr[$i] == ';') $sts++;  // Increment statement counter
      
      switch($arr[$i][1]){
        case 'do':  
        case 'if':  
        case 'for':
        case 'case':
        case 'loop':
        case 'while':
        case 'elseif':
        case 'switch':
        case 'default':
        case 'foreach':
          $con++; // Increment construct counter
      }
    }
  }
  
  printf("Physical Files: %d<br>", count($dir));
  printf("Physical Lines: %d<br>", $lin);
  printf("Size: %d Bytes<br>", $siz);
  printf("Statements: %d<br>", $sts);
  printf("Constructs: %d<br>", $con);
  printf("--------------------------<br>");
  printf("<b>SLOC Total: %d</b>", $sts+$con);
Cheers :)

Posted: Fri Dec 29, 2006 3:12 pm
by John Cartwright
Are you wanting to submit this to the code snipplets or what?

Posted: Fri Dec 29, 2006 3:32 pm
by alex.barylski
Jcart wrote:Are you wanting to submit this to the code snipplets or what?
Yup...I tried...but it said only mod's can...so I posted here??? :?

Posted: Sat Dec 30, 2006 4:09 pm
by alex.barylski
Ahh NOTE to mods... :P

Code Snippet??? as I'm not really looking for critique, am I? :)

Posted: Sat Dec 30, 2006 5:13 pm
by John Cartwright
It is kind of the bin for code that hasn't been approved for code snippets. A quick vote by the moderator team is done after a request for a snippet to be moved into the code snippet forum. Although I think we should have made that more clear in the forum description.

Posted: Sat Dec 30, 2006 6:14 pm
by alex.barylski
Ahhh...cool...whatever the destiny is cool by me...it's a pretty simple script so I wasn't looking for critique :)

As long as I know where to find it in the future :)

Cheers :)