VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • PHP统计目录下的文件总数及代码行数

  1. <?php 
  2. /** 
  3.  * @author xiaoxiao <x_824@sina.com> 2011-1-12 
  4.  * @link http://xiaoyaoxia.cnblogs.com/ 
  5.  * @license 
  6.  * 统计目录下的文件行数及总文件数··去除注释 
  7.  */ 
  8.  
  9. $obj = new caculatefiles(); 
  10. //如果设置为false,这不会显示每个文件的信息,否则显示 
  11. $obj->setshowflag(false); 
  12. //会跳过所有all开头的文件 
  13. $obj->setfileskip(array('all'));  
  14. $obj->run("d:phpappphp_tests"); 
  15.  
  16. //所有文件,(默认格式为.php) 
  17. $obj->setfileskip(array()); 
  18. $obj->run("d:phpappphp"); 
  19.  
  20. $obj->setshowflag(true); 
  21. //跳过所有i和a开头的文件,(比如接口和抽象类开头) 
  22. $obj->setfileskip(array('i''a')); 
  23. $obj->run("d:phpappphp"); 
  24.  
  25.  
  26. /** 
  27.  * 执行目录中文件的统计(包括文件数及总行数 
  28.  *  
  29.  * 1、跳过文件的时候: 
  30.  *    匹配的规则只是从文件名上着手,匹配的规则也仅限在开头。 
  31.  * 2、跳过文件中的注释行: 
  32.  *    匹配的规则只是从注释段落的头部匹配,如果出现// 及 *及 #及/*开头的行及空行会被跳过。所以类似/*这种多汗注释,每行的开头都必须加上*号,否则无法匹配到这种的注释。 
  33.  * 3、目录过滤: 
  34.  *    匹配的规则是从目录名的全名匹配 
  35.  */ 
  36. class caculatefiles { 
  37.  /** 
  38.   * 统计的后缀 
  39.   */ 
  40.  private $ext = ".php"
  41.  /** 
  42.   * 是否显示每个文件的统计数,开源代码phpfensi.com 
  43.   */ 
  44.  private $showeveryfile = true; 
  45.  /** 
  46.   * 文件的的跳过规则 
  47.   */ 
  48.  private $fileskip = array(); 
  49.  /** 
  50.   * 统计的跳过行规则 
  51.   */ 
  52.  private $lineskip = array("*""/*""//""#"); 
  53.  /** 
  54.   * 统计跳过的目录规则 
  55.   */ 
  56.  private $dirskip = array("."".."'.svn'); 
  57.    
  58.  public function __construct($ext = ''$dir = ''$showeveryfile = true, $dirskip = array(), $lineskip = array(), $fileskip = array()) { 
  59.   $this->setext($ext); 
  60.   $this->setdirskip($dirskip); 
  61.   $this->setfileskip($fileskip); 
  62.   $this->setlineskip($lineskip); 
  63.   $this->setshowflag($showeveryfile); 
  64.   $this->run($dir); 
  65.  } 
  66.    
  67.  public function setext($ext) { 
  68.   trim($ext) && $this->ext = strtolower(trim($ext)); 
  69.  } 
  70.  public function setshowflag($flag = true) { 
  71.   $this->showeveryfile = $flag
  72.  } 
  73.  public function setdirskip($dirskip) { 
  74.   $dirskip && is_array($dirskip) && $this->dirskip = $dirskip
  75.  } 
  76.  public function setfileskip($fileskip) { 
  77.   $this->fileskip = $fileskip
  78.  } 
  79.  public function setlineskip($lineskip) { 
  80.   $lineskip && is_array($lineskip) && $this->lineskip = array_merge($this->lineskip, $lineskip); 
  81.  } 
  82.  /** 
  83.   * 执行统计 
  84.   * @param string $dir 统计的目录 
  85.   */ 
  86.  public function run($dir = '') { 
  87.   if ($dir == ''return
  88.   if (!is_dir($dir)) exit('path error!'); 
  89.   $this->dump($dir$this->readdir($dir)); 
  90.  } 
  91.      
  92.  /** 
  93.   * 显示统计结果 
  94.   * @param string $dir 目录 
  95.   * @param array $result 统计结果(包含总行数,有效函数,总文件数 
  96.   */ 
  97.  private function dump($dir$result) { 
  98.   $totalline = $result['totalline']; 
  99.   $linenum = $result['linenum']; 
  100.   $filenum = $result['filenum']; 
  101.   echo "*************************************************************rn<br/>"
  102.   echo $dir . ":rn<br/>"
  103.   echo "totalline: " . $totalline . "rn<br/>"
  104.   echo "totalline with no comment and empty: " . $linenum . "rn<br/>"
  105.   echo 'totalfiles:' . $filenum . "rn<br/>"
  106.  } 
  107.  
  108.  /** 
  109.   * 读取目录 
  110.   * @param string $dir 目录 
  111.   */ 
  112.  private function readdir($dir) { 
  113.   $num = array('totalline' => 0, 'linenum' => 0, 'filenum' => 0); 
  114.   if ($dh = opendir($dir)) { 
  115.    while (($file = readdir($dh)) !== false) { 
  116.     if ($this->skipdir($file)) continue
  117.     if (is_dir($dir . '/' . $file)) { 
  118.      $result = $this->readdir($dir . '/' . $file); 
  119.      $num['totalline'] += $result['totalline']; 
  120.      $num['linenum'] += $result['linenum']; 
  121.      $num['filenum'] += $result['filenum']; 
  122.     } else { 
  123.      if ($this->skipfile($file)) continue
  124.      list($num1$num2) = $this->readfiles($dir . '/' . $file); 
  125.      $num['totalline'] += $num1
  126.      $num['linenum'] += $num2
  127.      $num['filenum']++; 
  128.     } 
  129.    } 
  130.    closedir($dh); 
  131.   } else { 
  132.    echo 'open dir <' . $dir . '> error!' . "r"
  133.   } 
  134.   return $num
  135.  } 
  136.  
  137.  /** 
  138.   * 读取文件 
  139.   * @param string $file 文件 
  140.   */ 
  141.  private function readfiles($file) { 
  142.   $str = file($file); 
  143.   $linenum = 0; 
  144.   foreach ($str as $value) { 
  145.    if ($this->skipline(trim($value))) continue
  146.    $linenum++; 
  147.   } 
  148.   $totalnum = count(file($file)); 
  149.   if (!$this->showeveryfile) return array($totalnum$linenum); 
  150.   echo $file . "rn"
  151.   echo 'totalline in the file:' . $totalnum . "rn"
  152.   echo 'totalline with no comment and empty in the file:' . $linenum . "rn"
  153.   return array($totalnum$linenum); 
  154.  } 
  155.     
  156.     /** 
  157.   * 执行跳过的目录规则 
  158.   * @param string $dir 目录名 
  159.   */ 
  160.  private function skipdir($dir) { 
  161.   if (in_array($dir$this->dirskip)) return true; 
  162.   return false; 
  163.  } 
  164.      
  165.  /** 
  166.   * 执行跳过的文件规则 
  167.   * @param string $file 文件名 
  168.   */ 
  169.  private function skipfile($file) { 
  170.   if (strtolower(strrchr($file'.')) != $this->ext) return true; 
  171.   if (!$this->fileskip) return false; 
  172.   foreach ($this->fileskip as $skip) { 
  173.    if (strpos($file$skip) === 0) return true; 
  174.   } 
  175.   return false; 
  176.  } 
  177.      
  178.  /** 
  179.   * 执行文件中行的跳过规则 
  180.   * @param string $string 行内容 
  181.   */ 
  182.  private function skipline($string) { 
  183.   if ($string == ''return true; 
  184.   foreach ($this->lineskip as $tag) { 
  185.    if (strpos($string$tag) === 0) return true; 
  186.   } 
  187.   return false; 
  188.  } 
  189. ?> 
  190.  

出处:http://www.phpfensi.com/php/20140805/4218.html


相关教程