The total number of files and lines of code in the PHP statistics directory (remove annotations and Blank lines)

Source: Internet
Author: User
Tags php tutorial trim

<?php Tutorial
/**
* @author Xiaoxiao <x_824@sina.com> 2011-1-12
* @link http://xiaoyaoxia.cnblogs.com/
* @license
* Number of rows 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);
Skips all files beginning with all
$obj->setfileskip (' all '));
$obj->run ("d:phpappphp_tests");

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

$obj->setshowflag (TRUE);
Skip all files starting with I and a, such as interfaces and abstract classes begin
$obj->setfileskip (Array (' I ', ' a '));
$obj->run ("d:phpappphp");


/**
* Statistics of files in the execution directory (including file number and total number of rows)
*
* 1, skip the file when:
* The matching rule only starts with the filename, and the matching rule is only at the beginning.
* 2, skip the comment line in the file:
* The matching rules are only matched from the header of the annotation paragraph, and if the line and empty rows at the beginning of the///and #及/* are skipped. So like/* This hyperhidrosis annotation, each line must be preceded by a * number, otherwise it will not be able to match the annotation.
* 3, Directory filter:
* The matching rule is to match the full name of the directory name
*/
Class Caculatefiles {
/**
* The suffix of statistics
*/
Private $ext = ". php";
/**
* Show statistics for each file
*/
Private $showeveryfile = true;
/**
* Skip rule for file
*/
Private $fileskip = Array ();
/**
* Skip line rule for statistics
*/
Private $lineskip = Array ("*", "/*", "//", "#");
/**
* Statistics skipped directory rules
*/
Private $dirskip = Array (".", "..", '. svn ');

The 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);
}
/**
* Implementation 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 Statistic results
* @param string $dir directory
* @param array $result Statistical results (including total number of rows, valid functions, number of files
*/
Private function Dump ($dir, $result) {
$totalline = $result [' Totalline '];
$linenum = $result [' LineNum '];
$filenum = $result [' FileNum '];
echo "*************************************************************rn<br/>";
Echo $dir. ":rn<br/>";
echo "Totalline:". $totalline. "Rn<br/>";
echo "Totalline with no comment and empty:". $linenum. "Rn<br/>";
Echo ' Totalfiles: '. $filenum. "Rn<br/>";
}

/**
* 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. "RN";
Echo ' Totalline in the file: '. $totalnum. "RN";
Echo ' Totalline with no comment and empty in the file: '. $linenum. "RN";
Return Array ($totalnum, $linenum);
}

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

/**
* Execute skipped file rule
* @param string $file filename
*/
Private Function Skipfile ($file) {
if (Strtolower strrchr ($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 rule for executing file
* @param string $string line contents
*/
Private Function SkipLine ($string) {
if ($string = = ") return true;
foreach ($this->lineskip as $tag) {
if (Strpos ($string, $tag) = = 0) return true;
}
return false;
}
}

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.