Source for file AlphaMetrics.inc
Documentation is available at AlphaMetrics.inc
require_once $config->get('sysRoot'). 'alpha/exceptions/LibraryNotInstalledException.inc';
fopen('PEAR/File_Find/Find.php', 'r', true);
require_once 'PEAR/File_Find/Find.php';
* Utility class for calcualting some software metics related to a project
* @package alpha::util::metrics
* @author John Collins <dev@alphaframework.org>
* @version $Id: AlphaMetrics.inc 1454 2011-12-04 15:14:05Z johnc $
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License
* @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the
* following conditions are met:
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
* * Neither the name of the Alpha Framework nor the names
* of its contributors may be used to endorse or promote
* products derived from this software without specific
* prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* The directory to begin the calculations from
* The file extensions of the file types to include in the calculations
private $includeFileTypes = array('.php', '.ini', '.html', '.phtml', '.inc', '.js', '.css', '.xml');
* Any sub-directories which you might want to exclude from the calculations
private $excludeSubDirectories = array('cache','lib','docs','attachments','dist');
* The Total Lines of Code (TLOC) for the project
* The Total Lines of Code (TLOC) for the project, less comments defined in $comments
private $TLOCLessComments = 0;
* The count of the source code files in the project
* An array of fileName => lines of code to be populated by this class
private $filesLOCResult = array();
* An array of fileName => lines of code to be populated by this class,
* excluding comment lines defined in the $comments array
private $filesLOCNoCommentsResult = array();
* An array of the source code file names in the project
private $files = array();
* An array of the directories in the project
private $directories = array();
* An array of the first characters of a comment line in source code
private $comments = array('/','*','#');
* Constructor, default $rootDir is .
$this->rootDir = $rootDir;
// populate the file and directories arrays using the File_Find class
list ($this->directories, $this->files) = File_Find::maptree($rootDir);
* Calculates the Lines of Code (LOC)
foreach ($this->files as $file) {
if (in_array($file_type, $this->includeFileTypes)) {
foreach ($this->excludeSubDirectories as $dir) {
$current_file = file($file);
$LOC = count($current_file);
$this->filesLOCResult[$file] = $LOC;
$LOC_less_comments = $this->disregardCommentsLOC($file);
$this->filesLOCNoCommentsResult[$file] = $LOC_less_comments;
$this->TLOCLessComments += $LOC_less_comments;
* Generates a HTML table containing the metrics results
$html = '<table class="list_view"><tr>';
$html .= '<th width="10%">File #:</th>';
$html .= '<th width="50%">File name:</th>';
$html .= '<th width="20%">Lines of Code (LOC):</th>';
$html .= '<th width="20%">Lines of Code (less comments):</th>';
foreach(array_keys($this->filesLOCResult) as $result) {
$html .= "<tr><td>$count</td><td>$result</td><td>". $this->filesLOCResult[$result]. "</td><td>". $this->filesLOCNoCommentsResult[$result]. "</td></tr>";
$html .= "<p>Total source code files: ". number_format($this->fileCount). "</p>";
$html .= "<p>Total Lines of Code (TLOC): ". number_format($this->TLOC). "</p>";
$html .= "<p>Total Lines of Code (TLOC), less comments: ". number_format($this->TLOCLessComments). "</p>";
* Filters comments from LOC metric
* @param string $sourceFile
private function disregardCommentsLOC($sourceFile) {
$file = file($sourceFile);
foreach ($file as $line) {
foreach ($this->comments as $comment) {
if (substr($line, 0, 1) == $comment) {
|