<?PHP
if (LW_DEBUG && isset($_GET[LW_SHOW_ANALYSIS_STRING]))
{
AnalyzeFile($_SERVER['SCRIPT_FILENAME']);
exit(0);
}
/**
* Provides and analysis of a given PHP file.
* @name AnalyzeFile
* @author Jim Wilson
* @since 04/23/2004
* @param string $filename The name of the PHP file to analyze.
*/
function AnalyzeFile($filename)
{
// Get the conetents of the file
$fp = fopen($filename,'r');
$source = fread($fp,filesize($filename));
fclose($fp);
// Parse the file into functions
$functions = preg_split('/\nfunction/',$source);
foreach ($functions as $key=>$val)
$functions[$key] = htmlspecialchars($val);
echo('<html><head><title>Analysis of '.$filename.'</title></head><body>'."\n");
echo('<h3>File: '.$filename."</h3>\n");
echo("<h3>Number of functions: ".count($functions)."</h3>\n");
echo('<hr width="200" align="left" />');
// For each function,
for ($i=1; $i<count($functions); $i++)
{
$data = AnalyzeFunction($functions[$i]);
echo('<strong>'.$data['name']."</strong><br />\n");
echo(' Blank lines = '.$data['blanks']."<br />\n");
echo(' Comment lines = '.$data['comments']."<br />\n");
echo(' Control structures = '.$data['controls']."<br />\n");
echo(' Query lines = '.$data['queries']."<br />\n");
echo(' Bracket lines = '.$data['bracketlines']."<br />\n");
echo(' Echo/Print lines = '.$data['echolines']."<br />\n");
echo(' <b>Total line count = '.$data['linecount']."</b><br />\n");
echo(' <b>Total (-comments -brackets) = '.($data['linecount']-$data['comments']-$data['bracketlines'])."</b><br />\n");
echo('<hr width="200" align="left" />');
}
}
/**
* Parses a string containing PHP source for a function into an array which contains data about it.
* @name AnalyzeFunction
* @author Jim Wilson
* @since 04/23/2004
* @param string $source The function source string to parse.
* @return array An array of the function's characteristics.
* @see AnalyzeFile()
*/
function AnalyzeFunction($function)
{
// Initialize data array
$data = array();
// Determine the function's name
preg_match('/\s*(.*?)[\s\(\n]/',$function,$matches);
$data['name'] = $matches[1];
// Retrieve content between opening and closing brackets
preg_match('/\{[\s\S]*\}/',$function,$matches);
$content = $matches[0];
$data['content'] = $content;
// Strip out all multi-line comments
$content = preg_replace('|/\*[\s\S]*?\*/|','',$content);
// Split the function body into lines
$lines = preg_split('/\n/',$content);
$data['linecount'] = count($lines);
$data['lines'] = $lines;
// Loop through the lines, counting special lines
$data['blanks'] = 0;
$data['comments'] = 0;
$data['queries'] = 0;
$data['bracketlines'] = 0;
$data['echolines'] = 0;
foreach ($lines as $line)
{
if (preg_match('/^\s*$/',$line)) $data['blanks']++;
if (preg_match('|^\s*//|',$line)) $data['comments']++;
if (preg_match('|^\s*\$query\s*\.?=|',$line)) $data['queries']++;
if (preg_match('|^[\s\}\{]*$|',$line)) $data['bracketlines']++;
if (preg_match('|^\s*(echo\|print)|',$line)) $data['echolines']++;
}
// Count the number of control structures
$data['controls'] = preg_match_all('/(if|each|for|while)/',$content,$matches);
// Return resulting data array
return $data;
}
?>