php如何利用语法统计代码行数及文件数量
php如何利用语法统计代码行数及文件数量,下面编程教程网小编给大家详细介绍一下具体操作流程!
1、设置php环境变量
//Windows:
我的电脑->属性->高级系统设置->PATH 加入PHP.exe的所在目录
//Linux:
vim /etc/profile
PATH="$PATH:/root/php/bin/"
2、新增count.php文件,输入以下代码:
<?php
class TotalCode {
/**
* 统计当前文件有多少行代码,
* @return TotalCodeInfo
*/
public function totalByFile($fullFileName) {
$fileContent = file_get_contents($fullFileName);
$lines = explode("\n",$fileContent);
$lineCount = count($lines);
for ($i = $lineCount -1; $i > 0; $i -= 1) {
$line = $lines[$i];
if ($line != "") break;
$lineCount -= 1;
//最后几行是空行的要去掉。
}
unset($fileContent);
unset($lines);
$totalCodeInfo = new TotalCodeInfo();
$totalCodeInfo->setFileCount(1);
$totalCodeInfo->setLineCount($lineCount);
return $totalCodeInfo;
}
/**
* 统计当前目录下(含子目录)
* 有多少文件,以及多少行代码
*
* totalInfo = array( "fileCount"=>?,"lineCount"=>? );
*
* @return TotalCodeInfo
*/
public function totalByDir($dirName) {
$fileList = scandir($dirName);
$totalCodeDir = new TotalCodeInfo();
foreach ($fileList as $fileName) {
if ($fileName == "." || $fileName == "..") continue;
$fullFileName = $dirName . "/" . $fileName;
if (is_file($fullFileName)) {
$totalCodeSub = $this->totalByFile($dirName . "/" . $fileName);
} else if (is_dir($fullFileName)) {
$totalCodeSub = $this->totalByDir($dirName . "/" . $fileName);
} else {
$totalCodeSub = new TotalCodeInfo();
}
$totalCodeDir->increaseByOther($totalCodeSub);
}
return $totalCodeDir;
}
public function totalByDirOrFile($dirOrFileName) {
if (is_dir($dirOrFileName)) {
return $this->totalByDir($dirOrFileName);
} else if (is_file($dirOrFileName)) {
return $this->totalByFile($dirOrFileName);
} else {
return new TotalCodeInfo();
}
}
public function test() {
$re = $this->totalByDir("/export/www/pm_web/configs");
var_dump($re);
}
public function main($dirList) {
$totalCodeAll = new TotalCodeInfo();
foreach($dirList as $dirName) {
$totalCodeSub = $this->totalByDirOrFile($dirName);
$totalCodeAll->increaseByOther($totalCodeSub);
}
print_r($totalCodeAll);
}
}
class TotalCodeInfo {
private $fileCount = 0;
private $lineCount = 0;
public function getFileCount() {
return $this->fileCount;
}
public function getLineCount() {
return $this->lineCount;
}
public function setFileCount($fileCount) {
$this->fileCount = $fileCount;
return $this;
}
public function setLineCount($lineCount) {
$this->lineCount = $lineCount;
return $this;
}
/*** 累加 ***/
public function increaseByOther($totalCodeInfo) {
$this->setFileCount( $this->fileCount + $totalCodeInfo->getFileCount());
$this->setLineCount( $this->lineCount + $totalCodeInfo->getLineCount());
return $this;
}
}
$dirList = array();
$dirList[] = "./";
$obj = new TotalCode();
$obj->main($dirList);
?>
3、语法执行
php count.php
4、结果返回
TotalCodeInfo Object
(
[fileCount:TotalCodeInfo:private] => 1024
[lineCount:TotalCodeInfo:private] => 551689
)
以上是编程学习网小编为您介绍的“php如何利用语法统计代码行数及文件数量”的全面内容,想了解更多关于 php入门 内容,请继续关注编程基础学习网。