Alpha Framework alpha--util--metrics
[ class tree: alpha--util--metrics ] [ index: alpha--util--metrics ] [ all elements ]

Source for file AlphaMetrics.inc

Documentation is available at AlphaMetrics.inc

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/exceptions/LibraryNotInstalledException.inc';
  4.  
  5. try{
  6.     fopen('PEAR/File_Find/Find.php''r'true);
  7.     require_once 'PEAR/File_Find/Find.php';
  8. }catch (Exception $e{
  9.     throw new LibraryNotInstalledException('Unable to generate code metrics as the PEAR::File_Find package is not installed!');
  10.     exit;
  11. }
  12.  
  13. /**
  14.  * Utility class for calcualting some software metics related to a project
  15.  *
  16.  * @package alpha::util::metrics
  17.  * @since 1.0
  18.  * @author John Collins <dev@alphaframework.org>
  19.  * @version $Id: AlphaMetrics.inc 1454 2011-12-04 15:14:05Z johnc $
  20.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  21.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  22.  *  All rights reserved.
  23.  * 
  24.  *  <pre>
  25.  *  Redistribution and use in source and binary forms, with or
  26.  *  without modification, are permitted provided that the
  27.  *  following conditions are met:
  28.  * 
  29.  *  * Redistributions of source code must retain the above
  30.  *    copyright notice, this list of conditions and the
  31.  *    following disclaimer.
  32.  *  * Redistributions in binary form must reproduce the above
  33.  *    copyright notice, this list of conditions and the
  34.  *    following disclaimer in the documentation and/or other
  35.  *    materials provided with the distribution.
  36.  *  * Neither the name of the Alpha Framework nor the names
  37.  *    of its contributors may be used to endorse or promote
  38.  *    products derived from this software without specific
  39.  *    prior written permission.
  40.  *   
  41.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  42.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  43.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  44.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  45.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  46.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  47.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  48.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  49.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  50.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  51.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  52.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  53.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54.  *  </pre>
  55.  *  
  56.  */
  57. class AlphaMetrics {
  58.     /**
  59.      * The directory to begin the calculations from
  60.      *
  61.      * @var string 
  62.      * @since 1.0
  63.      */
  64.     private $rootDir;
  65.     
  66.     /**
  67.      * The file extensions of the file types to include in the calculations
  68.      *
  69.      * @var array 
  70.      * @since 1.0
  71.      */
  72.     private $includeFileTypes array('.php''.ini''.html''.phtml''.inc''.js''.css''.xml');
  73.     
  74.     /**
  75.      * Any sub-directories which you might want to exclude from the calculations
  76.      *
  77.      * @var array 
  78.      * @since 1.0
  79.      */
  80.     private $excludeSubDirectories array('cache','lib','docs','attachments','dist');
  81.     
  82.     /**
  83.      * The Total Lines of Code (TLOC) for the project
  84.      *
  85.      * @var integer 
  86.      * @since 1.0
  87.      */
  88.     private $TLOC 0;
  89.     
  90.     /**
  91.      * The Total Lines of Code (TLOC) for the project, less comments defined in $comments
  92.      *
  93.      * @var integer 
  94.      * @since 1.0
  95.      */
  96.     private $TLOCLessComments 0;
  97.     
  98.     /**
  99.      * The count of the source code files in the project
  100.      *
  101.      * @var integer 
  102.      * @since 1.0
  103.      */
  104.     private $fileCount 0;
  105.     
  106.     /**
  107.      * An array of fileName => lines of code to be populated by this class
  108.      *
  109.      * @var array 
  110.      * @since 1.0
  111.      */
  112.     private $filesLOCResult array();
  113.     
  114.     /**
  115.      * An array of fileName => lines of code to be populated by this class,
  116.      * excluding comment lines defined in the $comments array
  117.      *
  118.      * @var array 
  119.      * @since 1.0
  120.      */
  121.     private $filesLOCNoCommentsResult array();
  122.     
  123.     /**
  124.      * An array of the source code file names in the project
  125.      *
  126.      * @var array 
  127.      * @since 1.0
  128.      */
  129.     private $files array();
  130.     
  131.     /**
  132.      * An array of the directories in the project
  133.      *
  134.      * @var array 
  135.      * @since 1.0
  136.      */
  137.     private $directories array();
  138.     
  139.     /**
  140.      * An array of the first characters of a comment line in source code
  141.      *
  142.      * @var array 
  143.      * @since 1.0
  144.      */
  145.     private $comments array('/','*','#');
  146.     
  147.     /**
  148.      * Constructor, default $rootDir is .
  149.      *
  150.      * @param string $rootDir 
  151.      * @since 1.0
  152.      */
  153.     public function __construct($rootDir '.'{
  154.         $this->rootDir $rootDir;
  155.         // populate the file and directories arrays using the File_Find class
  156.         list($this->directories$this->filesFile_Find::maptree($rootDir);    
  157.     }
  158.  
  159.     /**
  160.      * Calculates the Lines of Code (LOC)
  161.      * 
  162.      * @since 1.0
  163.      */
  164.     public function calculateLOC({
  165.         foreach ($this->files as $file{
  166.             $file_type substr($filestrrpos($file'.'));
  167.             if (in_array($file_type$this->includeFileTypes)) {
  168.                 $exclude false;
  169.                 foreach ($this->excludeSubDirectories as $dir{
  170.                     if (preg_match("/".$dir."/i"$file)) {
  171.                         $exclude true;
  172.                     }
  173.                 }
  174.                 
  175.                 if (!$exclude{
  176.                     $current_file file($file);
  177.  
  178.                     $LOC count($current_file);                        
  179.                     $this->filesLOCResult[$file$LOC;
  180.                     $LOC_less_comments $this->disregardCommentsLOC($file);
  181.                     $this->filesLOCNoCommentsResult[$file$LOC_less_comments;
  182.  
  183.                     $this->TLOC += $LOC;
  184.                     $this->TLOCLessComments += $LOC_less_comments;
  185.                     $this->fileCount++;                
  186.                 }
  187.             }
  188.         }
  189.     }
  190.     
  191.     /**
  192.      * Generates a HTML table containing the metrics results
  193.      * 
  194.      * @return string 
  195.      * @since 1.0
  196.      */
  197.     public function resultsToHTML({
  198.         $count 1;
  199.         
  200.         $html '<table class="list_view"><tr>';
  201.         $html .= '<th width="10%">File #:</th>';
  202.         $html .= '<th width="50%">File name:</th>';
  203.         $html .= '<th width="20%">Lines of Code (LOC):</th>';
  204.         $html .= '<th width="20%">Lines of Code (less comments):</th>';
  205.         $html .= '</tr>';
  206.         foreach(array_keys($this->filesLOCResultas $result{
  207.             $html .= "<tr><td>$count</td><td>$result</td><td>".$this->filesLOCResult[$result]."</td><td>".$this->filesLOCNoCommentsResult[$result]."</td></tr>";
  208.             $count++;
  209.         }        
  210.         $html .= '</table>';
  211.         
  212.         $html .= "<p>Total files: ".number_format(count($this->files))."</p>";
  213.         $html .= "<p>Total source code files: ".number_format($this->fileCount)."</p>";
  214.         $html .= "<p>Total Lines of Code (TLOC): ".number_format($this->TLOC)."</p>";
  215.         $html .= "<p>Total Lines of Code (TLOC), less comments: ".number_format($this->TLOCLessComments)."</p>";
  216.         
  217.         return $html;
  218.     }
  219.     
  220.     /**
  221.      * Filters comments from LOC metric
  222.      *
  223.      * @param string $sourceFile 
  224.      * @return integer 
  225.      * @since 1.0
  226.      */
  227.     private function disregardCommentsLOC($sourceFile{
  228.         $file file($sourceFile);
  229.         
  230.         $LOC 0;
  231.         
  232.         foreach ($file as $line{
  233.             $exclude false;
  234.             $line ltrim($line);
  235.             
  236.             if(empty($line)) {
  237.                 $exclude true;
  238.             }else{            
  239.                 foreach ($this->comments as $comment{
  240.                     if (substr($line01== $comment{
  241.                         $exclude true;
  242.                     }
  243.                 }
  244.             }
  245.                 
  246.             if (!$exclude{
  247.                 $LOC++;            
  248.             }
  249.         }
  250.         
  251.         return $LOC;
  252.     }
  253. }
  254.  
  255. ?>

Documentation generated on Tue, 13 Dec 2011 20:26:11 +0000 by phpDocumentor 1.4.3