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

Source: Internet
Author: User
<?php
/**
* @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:\PHPAPP\php\_tests");

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

$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:\PHPAPP\php");


/**
* 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 "*************************************************************\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/>";
}

/**
* 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);
}

/**
* 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.