Home > Backend Development > PHP Tutorial > What is the file ftnstat.stat? PHP counts the total number of files and lines of code in the directory (remove comments and blank lines)

What is the file ftnstat.stat? PHP counts the total number of files and lines of code in the directory (remove comments and blank lines)

WBOY
Release: 2016-07-29 08:44:16
Original
1240 people have browsed it

/**
* @author xiaoxiao 2011-1-12
* @link http://xiaoyaoxia.cnblogs.com/
* @license
* Count the number of file lines and total number of files in the directory· ·Remove comments
*/
$obj = new CaculateFiles();
//If set to false, this will not display information about each file, otherwise it will display
$obj->setShowFlag( false);
//Will skip all files starting with All
$obj->setFileSkip(array('All'));
$obj->run("D:PHPAPPphp_tests");
//All files , (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)
$obj->setFileSkip(array('I', 'A'));
$obj->run("D:PHPAPPphp ");
/**
* Statistics of files in the execution directory (including the number of files and the total number of lines
*
* 1. When skipping files:
* The matching rules only start from the file name, and the matching rules are also Only at the beginning.
* 2. Skip comment lines in the file:
* The matching rules are only matched from the head of the comment paragraph. If there are lines starting with // and *, # and /*, and blank lines will be Skip. Therefore, sweaty comments like /* must be preceded by an * sign, otherwise such comments cannot be matched.
* 3、目录过滤:
* 匹配的规则是从目录名的全名匹配
*/
class CaculateFiles {
/**
* Statistical suffix
*/
private $ext = ".php";
/**
* Whether to display the statistics of each file
*/
private $showEveryFile = true;
/**
* File skipping rules
*/
private $fileSkip = array();
/**
* Statistical skip line rules
*/
private $lineSkip = array("*", "/*", "//", "#");
/**
* Statistics of skipped directory rules
*/
private $dirSkip = array(".", "..", '.svn');
public function __construct($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->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);
}
/**
* Execution statistics
* @param string $dir Statistics directory
*/
public function run($dir = '') {
if ($dir == '') return;
if (!is_dir($dir)) exit('Path error!');
$this->dump($dir, $this->readDir($dir));
}
/**
* Display statistical results
* @param string $dir directory
* @param array $result Statistical results (including total number of lines, valid functions, total number of files
*/
private function dump($dir, $result) {
$totalLine = $result['totalLine'];
$lineNum = $result['lineNum'];
$fileNum = $result['fileNum'];
echo "*************************************************************rn
";
echo $dir . ":rn
";
echo "TotalLine: " . $totalLine . "rn
";
echo "TotalLine with no comment and empty: " . $lineNum . "rn
";
echo 'TotalFiles:' . $fileNum . "rn
";
}
/**
* 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($num1, $num2) = $this->readfiles($dir . '/' . $file);
$num['totalLine'] += $num1;
$num['lineNum'] += $num2;
$num['fileNum']++;
}
}
closedir($dh);
} else {
echo 'open dir <' . $dir . '> error!' . "r";
}
return $num;
}
/**
* Read file
* @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 rules
* @param string $file file name
*/
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;
}
/**
* Execute skipping rules for lines in files
* @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;
}
}

The above introduces what kind of file ftnstat.stat is, the total number of files and the number of lines of code in the PHP statistics directory (removing comments and blank lines), including what kind of file ftnstat.stat is. I hope that friends who are interested in PHP tutorials can read it. helped.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template