PHP 代码检测工具

CV大法好,好就好在TMDB
接手一外包代码你就知道有多惨

phpcpd - 用于PHP代码的复制/黏贴检测器

phpcpd - PHP Copy/Paste Detector

1
2
3
4
5
6
7
8
9
# 安装
composer require --dev sebastian/phpcpd

# 使用 (使用vendor/bin/phpcpd可执行文件调用它, 也可以配置PATH)
phpcpd --fuzzy /项目目录/

# 直接导出
phpcpd --fuzzy /项目目录/ > /导出文件/
phpcpd --fuzzy ./App > ./app.txt

phpdcd - PHP代码的死代码检测器

phpdcd - PHP Dead Code Detector

1
2
# 安装
composer global require 'sebastian/phpdcd=*'

phpcs - PHP代码质量检测器

phpcs - PHP Code Sniffer

1
2
# 安装
composer global require "squizlabs/php_codesniffer=*"

安装完后就会在全局的 Vendor 目录下的 bin 中生成两个软链接:

1
2
phpcbf -> ../squizlabs/php_codesniffer/bin/phpcbf
phpcs -> ../squizlabs/php_codesniffer/bin/phpcs
  • 格式化前
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
namespace App\Http\Controllers;
use App\Traits\Tool;
class TestController extends Controller
{
public function test_i18n(){
return Tool::sysMsg('SYS_MSG_TEST');
}

public function test_i18n2(){

return Tool::sysMsg('SYS_MSG_TEST', 'en');
}
}
  • phpcs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ phpcs TestController.php

FILE: D:\app\phpStudy\PHPTutorial\WWW\Lumen\app\Http\Controllers\TestController.php
----------------------------------------------------------------------------------------------
FOUND 8 ERRORS AFFECTING 4 LINES
----------------------------------------------------------------------------------------------
2 | ERROR | [ ] Missing file doc comment
4 | ERROR | [ ] Missing doc comment for class TestController
6 | ERROR | [ ] Missing doc comment for function test_i18n()
6 | ERROR | [ ] Public method name "TestController::test_i18n" is not in camel caps format
6 | ERROR | [x] Opening brace should be on a new line
10 | ERROR | [ ] Missing doc comment for function test_i18n2()
10 | ERROR | [ ] Public method name "TestController::test_i18n2" is not in camel caps format
10 | ERROR | [x] Opening brace should be on a new line
----------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------------------------------

Time: 75ms; Memory: 4MB
  • phpcbf
1
2
3
4
5
6
7
8
9
10
11
12
$ phpcbf TestController.php

PHPCBF RESULT SUMMARY
--------------------------------------------------------------------------------------------------
FILE FIXED REMAINING
--------------------------------------------------------------------------------------------------
D:\app\phpStudy\PHPTutorial\WWW\Lumen\app\Http\Controllers\TestController.php 2 6
--------------------------------------------------------------------------------------------------
A TOTAL OF 2 ERRORS WERE FIXED IN 1 FILE
--------------------------------------------------------------------------------------------------

Time: 79ms; Memory: 4MB
  • 格式化后
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php

namespace App\Http\Controllers;

use App\Traits\Tool;

class TestController extends Controller
{
public function test_i18n()
{
return Tool::sysMsg('SYS_MSG_TEST');
}

public function test_i18n2()
{
return Tool::sysMsg('SYS_MSG_TEST', 'en');
}
}

参考

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2021 Keep It Simple And Stupid All Rights Reserved.

访客数 : | 访问量 :