php质量检查(phpmd,SonarLint)
2019-01-23 本文已影响14人
零一间
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
<?xml version="1.0"?>
<ruleset name="customer/php"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"
>
<rule ref="rulesets/codesize.xml/TooManyFields" />
<!--- 一个类中不能有太多fields字段,默认值15 -->
<rule ref="rulesets/codesize.xml/NPathComplexity" />
<!--- 一个方法中NPath复杂性,默认值200 -->
<rule ref="rulesets/codesize.xml/ExcessiveMethodLength" />
<!--- 相同的方法执行同一功能,默认值100 -->
<rule ref="rulesets/codesize.xml/ExcessiveClassLength" />
<!--- 一个类的最大行数,默认值1000 -->
<rule ref="rulesets/codesize.xml/TooManyMethods" />
<!--- 一个类的最大方法数,默认值25 -->
<rule ref="rulesets/controversial.xml/CamelCaseClassName" />
<!--- 以驼峰式命名类 -->
<rule ref="rulesets/controversial.xml/CamelCaseParameterName" />
<!--- 以驼峰式命名参数名字 -->
<rule ref="rulesets/controversial.xml/CamelCaseVariableName" />
<!--- 以驼峰式命名变量名字 -->
<rule ref="rulesets/design.xml/DevelopmentCodeFragment" />
<!-- 代码中不要有var_dump(), print_r()等这种调试输出结构 -->
<rule ref="rulesets/design.xml/EvalExpression" />
<!-- 代码中不要有eval()这种结构 -->
<rule ref="rulesets/design.xml/GotoStatement" />
<!-- 代码中不要有goto这种结构 -->
<rule ref="rulesets/naming.xml/BooleanGetMethodName" />
<!-- 返回值为布尔型而以'getX()'格式命名的方法改写成'isX()' 或者 'hasX()'-->
<rule ref="rulesets/naming.xml/ConstantNamingConventions" />
<!-- 类/接口常量必须用大写字母定义-->
<rule ref="rulesets/unusedcode.xml/UnusedLocalVariable" />
<!-- 存在已声明或赋值的局部变量,但是却未使用-->
<rule ref="rulesets/unusedcode.xml/UnusedPrivateField" />
<!-- 存在已声明或赋值的局部变量,但是却未使用-->
</ruleset>
- 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
2 phpstorm 插件 (SonarLint)
image.pngphpstorm 插件仓库自带
image.png image.png右键执行,查看结果