Total number of files and lines of code in the PHP statistics directory (remove comments and blank lines)

  • 2020-03-31 21:33:06
  • OfStack

< ? PHP
/ * *
* @ author xiaoxiao < x_824@sina.com > The 2011-1-12
* @ the link http://xiaoyaoxia.cnblogs.com/
* @ license
* count the number of lines and total files in the directory , , remove comments
* /

$obj = new CaculateFiles ();
// if set to false, this will not display information for each file, otherwise it will
$obj - > SetShowFlag (false);
// will skip All files that start with All
$obj - > SetFileSkip (array (' All '));
$obj - > The run (" D: \ PHPAPP \ PHP \ _tests ");

// all files, (the default format is.php)
$obj - > SetFileSkip (array ());
$obj - > The run (" D: \ PHPAPP \ PHP ");

$obj - > SetShowFlag (true);
// skip all files that start with I and A (such as interface and abstract class)
$obj - > SetFileSkip (array (' I ', 'A'));
$obj - > The run (" D: \ PHPAPP \ PHP ");


/ * *
* perform statistics on files in the directory (including number of files and total number of lines)
*
* 1. When skipping files:
* matching rules only start at the file name, and matching rules are limited to the beginning.
* 2. Skip the comment line in the file:
The rule for * matches only from the head of the comment paragraph, and if there are // and * and lines beginning with # and /* and empty lines are skipped. So similar
The class CaculateFiles {

Private $ext = "PHP";

Private $showEveryFile = true;

Private $fileSkip = array ();

Private $lineSkip = array("*", "/*", "//", "#");

Private $dirSkip = array(".", "..") , 'SVN');

Public function s ($ext = ', $dir = ', $showEveryFile = true, $dirSkip = array(), $lineSkip = array(), $fileSkip = array()) {
$this - > SetExt ($ext);
$this - > SetDirSkip ($dirSkip);
$this - > SetFileSkip ($fileSkip);
$this - > SetLineSkip ($lineSkip);
$this - > SetShowFlag ($showEveryFile);
$this - > The run ($dir);
}

The public function setExt ($ext) {
Trim ($ext) && $this - > Ext = strtolower (trim ($ext));
}
Public function setShowFlag($flag = true) {
$this - > ShowEveryFile = $flag;
}
The public function setDirSkip ($dirSkip) {
$dirSkip && is_array($dirSkip) && $this-> DirSkip = $dirSkip;
}
The public function setFileSkip ($fileSkip) {
$this - > FileSkip = $fileSkip;
}
The public function setLineSkip ($lineSkip) {
$lineSkip && is_array($lineSkip) && $this-> LineSkip = array_merge ($this - > LineSkip, $lineSkip);
}

Public function run($dir = ") {
If ($dir == ") return;
If (! Is_dir ($dir)) exit (' Path error! ');
$this - > The dump ($dir, $this - > $dir readDir ());
}


Private function dump($dir, $result) {
$totalLine = $result [' totalLine];
$lineNum = $result [' lineNum];
$fileNum = $result [' fileNum];
Echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \ r \ n< Br / >" ;
Echo $dir. ": \ r \ n< Br / >" ;
Echo "TotalLine: ". $TotalLine. "\r\n< Br / >" ;
Echo "TotalLine with no comment and empty: ". $lineNum. "\r\n< Br / >" ;
Echo 'TotalFiles:'. $fileNum. "\r\n< Br / >" ;
}


Private function readDir ($dir) {
$num = array (' totalLine = > 0, 'lineNum = > 0, 'fileNum = > 0);
If ($dh = opendir($dir)) {
While (($file = readdir($dh))! = = false) {
If ($this - > SkipDir ($file)) continue;
If (is_dir($dir. '/'. $file)) {
$result = $this - > ReadDir ($dir. '/'. $file);
$num [' totalLine] + = $result [' totalLine];
$num [' lineNum] + = $result [' lineNum];
$num [' fileNum] + = $result [' fileNum];
} else {
If ($this - > SkipFile ($file)) continue;
The list ($num1, $num2) = $this - > Readfiles ($dir. '/'. $file);
$num [' totalLine] + = $num1;
$num [' lineNum] + = $num2;
$num [' fileNum] + +;
}
}
Closedir ($dh);
} else {
Echo 'open dir < '. $dir. '> The error! . '" \ r ";
}
Return the $num;
}


Private function readfiles ($file) {
$STR = file ($file);
$linenum = 0;
Foreach ($STR as $value) {
If ($this - > SkipLine (trim ($value))) continue;
$linenum++;
}
$totalnum = count (file ($file));
If (! $this - > ShowEveryFile) return an array ($totalnum, $linenum);
Echo $file. "\ r \ n";
Echo 'TotalLine in the file:'. $totalnum. "\r\n";
Echo 'TotalLine with no comment and empty in the file:'. $linenum. "\r\n";
Return array ($totalnum, $linenum);
}


Private function skipDir ($dir) {
If (in_array ($dir, $this - > DirSkip)) return true;
Return false;
}


Private function skipFile ($file) {
If (strtolower (STRRCHR (" c: "($file, '. '))! = $this - > Ext) return true;
if (!$this->fileSkip) return false;
foreach ($this->fileSkip as $skip) {
if (strpos($file, $skip) === 0) return true;
}
return false;
}


private function skipLine($string) {
if ($string == '') return true;
foreach ($this->lineSkip as $tag) {
if (strpos($string, $tag) === 0) return true;
}
return false;
}
}

Related articles: