PHP实现统计代码行数小工具

本文实例为大家分享了PHP实现统计代码行数小工具,供大家参考,具体内容如下

为了方面统计编程代码行数,做了一个小工具。

自动统计指定目录以及目录下的所有文件。

  1. <?php
  2. class TotalCode {
  3. /**
  4. * 统计当前文件有多少行代码,
  5. * @return TotalCodeInfo
  6. */
  7. public function totalByFile($fullFileName) {
  8. $fileContent = file_get_contents($fullFileName);
  9. $lines = explode("\n", $fileContent);
  10. $lineCount = count($lines);
  11. for($i = $lineCount -1; $i > 0; $i -= 1) {
  12. $line = $lines[$i];
  13. if ($line != "") break;
  14. $lineCount -= 1; //最后几行是空行的要去掉。
  15. }
  16. unset($fileContent);
  17. unset($lines);
  18. $totalCodeInfo = new TotalCodeInfo();
  19. $totalCodeInfo->setFileCount(1);
  20. $totalCodeInfo->setLineCount($lineCount);
  21. return $totalCodeInfo;
  22. }
  23. /**
  24. * 统计当前目录下(含子目录)
  25. * 有多少文件,以及多少行代码
  26. *
  27. * totalInfo = array( "fileCount"=>?, "lineCount"=>? );
  28. *
  29. * @return TotalCodeInfo
  30. */
  31. public function totalByDir($dirName) {
  32. $fileList = scandir($dirName);
  33. $totalCodeDir = new TotalCodeInfo();
  34. foreach ($fileList as $fileName) {
  35. if ($fileName == "." || $fileName == "..") continue;
  36. $fullFileName = $dirName . "/" . $fileName;
  37. if (is_file($fullFileName)) {
  38. $totalCodeSub = $this->totalByFile($dirName . "/" . $fileName);
  39. } else if (is_dir($fullFileName)) {
  40. $totalCodeSub = $this->totalByDir($dirName . "/" . $fileName);
  41. } else {
  42. $totalCodeSub = new TotalCodeInfo();
  43. }
  44. $totalCodeDir->increaseByOther($totalCodeSub);
  45. }
  46. return $totalCodeDir;
  47. }
  48. public function totalByDirOrFile($dirOrFileName) {
  49. if (is_dir($dirOrFileName)) {
  50. return $this->totalByDir($dirOrFileName);
  51. } else if (is_file($dirOrFileName)) {
  52. return $this->totalByFile($dirOrFileName);
  53. } else {
  54. return new TotalCodeInfo();
  55. }
  56. }
  57. public function test() {
  58. $re = $this->totalByDir("/export/www/pm_web/configs");
  59. var_dump($re);
  60. }
  61. public function main($dirList) {
  62. $totalCodeAll = new TotalCodeInfo();
  63. foreach($dirList as $dirName) {
  64. $totalCodeSub = $this->totalByDirOrFile($dirName);
  65. $totalCodeAll->increaseByOther($totalCodeSub);
  66. }
  67. print_r($totalCodeAll);
  68. }
  69. }
  70. class TotalCodeInfo {
  71. private $fileCount = 0;
  72. private $lineCount = 0;
  73. public function getFileCount() { return $this->fileCount; }
  74. public function getLineCount() { return $this->lineCount; }
  75. public function setFileCount($fileCount) {
  76. $this->fileCount = $fileCount;
  77. return $this;
  78. }
  79. public function setLineCount($lineCount) {
  80. $this->lineCount = $lineCount;
  81. return $this;
  82. }
  83. /**
  84. * 累加
  85. */
  86. public function increaseByOther($totalCodeInfo) {
  87. $this->setFileCount( $this->fileCount + $totalCodeInfo->getFileCount());
  88. $this->setLineCount( $this->lineCount + $totalCodeInfo->getLineCount());
  89. return $this;
  90. }
  91. }
  92. $dirList = array();
  93. $dirList[] = "/your/path";
  94. $obj = new TotalCode();
  95. $obj->main($dirList);