Code Snippet: Q & D Source Statistics

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Code Snippet: Q & D Source Statistics

Post 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 :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Are you wanting to submit this to the code snipplets or what?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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??? :?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Ahh NOTE to mods... :P

Code Snippet??? as I'm not really looking for critique, am I? :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
Post Reply