Total number of files and lines of code in PHP statistics directory (remove comments and Blank lines) _php tutorial

Source: Internet
Author: User
/**
* @author Xiaoxiao 2011-1-12
* @link http://xiaoyaoxia.cnblogs.com/
* @license
* Number of file lines and total files in the statistics directory • • Remove annotations
*/

$obj = new Caculatefiles ();
If set to False, this does not display information for each file, otherwise the display
$obj->setshowflag (FALSE);
Will skip all files beginning with all
$obj->setfileskip (Array (' all '));
$obj->run ("D:\PHPAPP\php\_tests");

All files, (the default format is. php)
$obj->setfileskip (Array ());
$obj->run ("D:\PHPAPP\php");

$obj->setshowflag (TRUE);
Skip all files beginning with I and a, (e.g. interface and abstract class start)
$obj->setfileskip (Array (' I ', ' A '));
$obj->run ("D:\PHPAPP\php");


/**
* Statistics of files in the execution directory (including number of files and total number of rows)
*
* 1, skip the file when:
* The matching rules are only started from the file name, and the matching rules are limited to the beginning.
* 2. Skip the comment lines in the file:
* Matching rules are only matched from the header of the comment paragraph, if the//and * and #及/* and the lines beginning with//and the empty row are skipped. So, like/* This multi-sweat annotation, the beginning of each line must be preceded by an * number, otherwise it cannot match the comment.
* 3, Directory filtering:
* Matching rules are matched from the full name of the directory name
*/
Class Caculatefiles {
/**
* Suffix of statistics
*/
Private $ext = ". php";
/**
* Whether to show the statistics of each file
*/
Private $showEveryFile = true;
/**
* Skip rules for files
*/
Private $fileSkip = Array ();
/**
* Statistics of Skip line rules
*/
Private $lineSkip = Array ("*", "/*", "//", "#");
/**
* Statistics skipped directory rules
*/
Private $dirSkip = Array (".", "..", '. svn ');

Public function __construct ($ext = ", $dir =", $showEveryFile = true, $dirSkip = Array (), $lineSkip = Array (), $fileSki p = Array ()) {
$this->setext ($ext);
$this->setdirskip ($DIRSKIP);
$this->setfileskip ($FILESKIP);
$this->setlineskip ($LINESKIP);
$this->setshowflag ($showEveryFile);
$this->run ($dir);
}

Public Function Setext ($ext) {
Trim ($ext) && $this->ext = Strtolower (Trim ($ext));
}
Public Function Setshowflag ($flag = True) {
$this->showeveryfile = $flag;
}
Public Function Setdirskip ($dirSkip) {
$dirSkip && Is_array ($dirSkip) && $this->dirskip = $dirSkip;
}
Public Function Setfileskip ($fileSkip) {
$this->fileskip = $fileSkip;
}
Public Function Setlineskip ($lineSkip) {
$lineSkip && Is_array ($lineSkip) && $this->lineskip = Array_merge ($this->lineskip, $lineSkip);
}
/**
* Executive Statistics
* @param a directory of string $dir statistics
*/
Public function Run ($dir = ") {
if ($dir = = ") return;
if (!is_dir ($dir)) exit (' Path error! ');
$this->dump ($dir, $this->readdir ($dir));
}

/**
* Show Statistical results
* @param string $dir directory
* @param array $result Statistical results (including total rows, valid functions, number of files
*/
Private function Dump ($dir, $result) {
$totalLine = $result [' Totalline '];
$lineNum = $result [' LineNum '];
$fileNum = $result [' FileNum '];
echo "*************************************************************\r\n
";
Echo $dir. ": \ r \ n
";
echo "Totalline:". $totalLine. "\ r \ n
";
echo "Totalline with no comment and empty:". $lineNum. "\ r \ n
";
Echo ' Totalfiles: '. $fileNum. "\ r \ n
";
}

/**
* Read Directory
* @param string $dir directory
*/
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;
List ($num 1, $num 2) = $this->readfiles ($dir. '/' . $file);
$num [' totalline '] + = $num 1;
$num [' linenum '] + = $num 2;
$num [' FileNum ']++;
}
}
Closedir ($DH);
} else {
Echo ' Open dir < '. $dir. ' > error! '. "\ r";
}
return $num;
}

/**
* Read files
* @param string $file file
*/
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 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);
}

/**
* Perform skipped directory rules
* @param string $dir directory Name
*/
Private Function Skipdir ($dir) {
if (In_array ($dir, $this->dirskip)) return true;
return false;
}

/**
* Execution of skipped file rules
* @param string $file file name
*/
Private Function Skipfile ($file) {
if (Strtolower ($file, '. '))! = $this->ext) return true;
if (! $this->fileskip) return false;
foreach ($this->fileskip as $skip) {
if (Strpos ($file, $skip) = = = 0) return true;
}
return false;
}

/**
* Skip rules for executing rows in a file
* @param string $string line content
*/
Private Function SkipLine ($string) {
if ($string = = ") return true;
foreach ($this->lineskip as $tag) {
if (Strpos ($string, $tag) = = = 0) return true;
}
return false;
}
}

http://www.bkjia.com/PHPjc/322811.html www.bkjia.com true http://www.bkjia.com/PHPjc/322811.html techarticle PHP/** * @author xiaoxiao x_824@sina.com 2011-1-12 * @link http://xiaoyaoxia.cnblogs.com/* @license * The number of file lines in the statistics directory and the total text Number of pieces • Remove Comments */$obj = new ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.