php专区

 首页 > php专区 > PHP应用 > php类库 > PHP统计目录下的文件总数及代码行数 - php类库

PHP统计目录下的文件总数及代码行数 - php类库

分享到:
【字体:
导读:
          ?php ***@authorxiaoxiaox_824@sina com2011-1-12*@linkhttp: xiaoyaoxia cnblogs com *@license*统计目录下的文件行数及总文件数去除注释* $obj=newcaculatefiles(); 如果设置为false,这...

PHP统计目录下的文件总数及代码行数

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