当前位置: 首页 > 工具软件 > PHPMD > 使用案例 >

php 代码质量检测工具,PHP代码规范质量检测工具 PHPMD PHPCS

吕扬
2023-12-01

安装

root@kerin:~/project# composer require "squizlabs/php_codesniffer=*"

root@kerin:~/project# composer require phpmd/phpmd

root@kerin:~/project# ls vendor/bin

pdepend phpcbf phpcs phpmd

也可以直接安装在系统里面

apt -y install php-codesniffer phpmd

查看已经安装的标准

root@kerwin:~/project# vendor/bin/phpcs -i

The installed coding standards are PSR1, PSR2, Zend, Squiz, PEAR and MySource

使用

新建测试文件

root@kerwin:~/project# vi a.php

#a.php

namespace Ctwj;

class Test{

public $test;

private $test_p;

public function __constract( )

{

$this->v = 'test';

}

}

new Test();

检测

root@kerwin:~/project# vendor/bin/phpcs ./a.php --standard=PSR2

FILE: /root/project/a.php

-----------------------------------------------------------------------------------------

FOUND 9 ERRORS AND 1 WARNING AFFECTING 8 LINES

-----------------------------------------------------------------------------------------

1 | WARNING | [ ] A file should declare new symbols (classes, functions, constants,

| | etc.) and cause no other side effects, or it should execute logic

| | with side effects, but should not do both. The first symbol is

| | defined on line 5 and the first side effect is on line 16.

5 | ERROR | [x] Opening brace of a class must be on the line after the definition

7 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed

8 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed

8 | ERROR | [x] Scope keyword "private" must be followed by a single space; found 2

10 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed

10 | ERROR | [x] Expected 0 spaces between brackets of function declaration; 1 found

11 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed

12 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed

13 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed

-----------------------------------------------------------------------------------------

PHPCBF CAN FIX THE 9 MARKED SNIFF VIOLATIONS AUTOMATICALLY

-----------------------------------------------------------------------------------------

Time: 102ms; Memory: 4Mb

--standard 是测试的标准

修复

root@kerwin:~/project# ls

a.php composer.json composer.lock vendor

root@kerwin:~/project# vendor/bin/phpcbf ./a.php --suffix=.fixed

PHPCBF RESULT SUMMARY

----------------------------------------------------------------------

FILE FIXED REMAINING

----------------------------------------------------------------------

/root/project/a.php 13 6

----------------------------------------------------------------------

A TOTAL OF 13 ERRORS WERE FIXED IN 1 FILE

----------------------------------------------------------------------

Time: 51ms; Memory: 4Mb

root@kerwin:~/project# ls

a.php a.php.fixed composer.json composer.lock vendor

--suffix 指定修复的后缀,如果如指定,会直接在原文件上修复

质量检测

root@kerwin:~/project# vendor/bin/phpmd ./fixed.php text codesize,unusedcode,naming

/root/project/fixed.php:9 Avoid unused private fields such as '$test_p'

这里检测到了一个没有使用的私有变量

其他

 类似资料: