Home > Backend Development > PHP Tutorial > PHP counts the total number of files and lines of code in the directory (removing comments and blank lines)_PHP Tutorial

PHP counts the total number of files and lines of code in the directory (removing comments and blank lines)_PHP Tutorial

WBOY
Release: 2016-07-21 15:32:20
Original
928 people have browsed it

/**
* @author xiaoxiao 2011-1-12
* @link http://xiaoyaoxia.cnblogs.com/
* @license
* under the statistics directory Number of file lines and total number of files·Remove comments
*/

$obj = new CaculateFiles();
//If set to false, this will not display information for each file, otherwise Show
$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 total lines
*
* 1. When skipping files:
* The matching rule only starts from the file name, matching The rules are also limited to the beginning.
* 2. Skip the comment lines in the file:
* The matching rules only match from the head of the comment paragraph, if // and * and # and /* appear. Lines and blank lines will be skipped. Therefore, sweaty comments like /* must be preceded by an * sign, otherwise such comments cannot be matched.
* 3、目录过滤:
* 匹配的规则是从目录名的全名匹配
*/
class CaculateFiles {
/**
* Statistics suffix
*/
private $ext = ".php";
/**
* Whether to display statistics for each file
*/
private $showEveryFile = true;
/**
* Skip rules for files
*/
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 the 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;
}
}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/322811.htmlTechArticle?php /*** @author xiaoxiao x_824@sina.com 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...
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