php质量检查(phpmd,SonarLint)

1 PHPMD

安装

unix:

wget http://static.phpmd.org/php/latest/phpmd.phar -O phpmd.phar
chmod a+x phpmd.phar
mv phpmd.phar /usr/local/bin/phpmd.phar

使用

/usr/local/bin/phpmd.phar

Mandatory arguments:
1) A php source code filename or directory. Can be a comma-separated string
2) A report format
3) A ruleset filename or a comma-separated string of rulesetfilenames

Available formats: xml, text, html.
Available rulesets: cleancode, codesize, controversial, design, naming, unusedcode.

Optional arguments that may be put after the mandatory arguments:
--minimumpriority: rule priority threshold; rules with lower priority than this will not be used
--reportfile: send report output to a file; default to STDOUT
--suffixes: comma-separated string of valid source code filename extensions, e.g. php,phtml
--exclude: comma-separated string of patterns that are used to ignore directories
--strict: also report those nodes with a @SuppressWarnings annotation
--ignore-violations-on-exit: will exit with a zero code, even if any violations are found

命令行有三个参数

# phpmd 源代码路径 报告的格式 规则列表

# 源代码路径 支持
   一个文件 /path/to/file
   一个目录 /path/to/source 

# 报告的格式 支持 
    xml:以XML格式输出;
    text:简单的文本格式;
    html:输出到单个的html;

# 规则列表 支持
    phpmd_ruleset.xml 文件格式
    codesize,unusedcode,naming 单个命令集合

# 附加参数
  --exclude - 忽略的目录,以逗号分隔多个目录。

# 示例
phpmd /path/to/source html ./phpmd_ruleset.xml

规则集合列表

Clean Code Rules: 强化代码整洁度的规则集。
Code Size Rules: 代码尺寸规则集.
Controversial Rules: 有争议的代码规则.
Design Rules: 软件设计的相关问题规则集.
Naming Rules: 名称太长,规则太短,等等规则集.
Unused Code Rules: 找到未使用的代码的规则集.

示例:

  • 1 phpmd.xml


    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

  • 2 测试

phpmd.phar ~/data/github/eolinker/server/Server/Web/Controller html ./phpmd.xml --exclude application/cache,vendor > eolinker_server_Server_Web_Controller.html

  • 3 查看生成结果 phpmd.html
php质量检查(phpmd,SonarLint)_第1张图片
image.png

2 phpstorm 插件 (SonarLint)

phpstorm 插件仓库自带

php质量检查(phpmd,SonarLint)_第2张图片
image.png

右键执行,查看结果

php质量检查(phpmd,SonarLint)_第3张图片
image.png

sonar规则

php质量检查(phpmd,SonarLint)_第4张图片
image.png

你可能感兴趣的:(php质量检查(phpmd,SonarLint))