
php如何利用语法统计代码行数及文件数量,下面web建站小编给大家详细介绍一下具体操作流程!
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删除接口:在指定的数据库数据表中根据ID删除指定的数据
上面是“php如何利用语法统计代码行数及文件数量”的全面内容,想了解更多关于 php入门 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_3351.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

网站访问出现“504 Gateway Time out”错误怎么解决?
推荐一款基于ThinkPHP8和Vue3的后台管理系统——BuildAdmin
文生图工作流:cuban tody multicolor on tree
推荐一款专业的跨平台的音频处理软件——Audacity(免费开源)
奇游手游加速器