【PHP】代码规范检查工具PHPCS
PHP代码规范有PSRs,为了能做到代码规范的自动化检查和修复,就需要用到PHPCS了。
项目官网:https://github.com/squizlabs/PHP_CodeSniffer
PHPCS安装
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar php phpcs.phar -h curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar php phpcbf.phar -h // 也许需要 sudo mv phpcs.phar /usr/bin/phpcs mv phpcbf.phar /usr/bin/phpcbf chmod +x /usr/bin/phpcs chmod +x /usr/bin/phpcbf
常用命令
- 检查单个文件:phpcs /path/to/code
- 检查目录下的文件:phpcs /path/to/code/
- 查看已经安装的标准:phpcs -i
- 设置默认检查标准:phpcs --config-set default_standard /path/to/standard_file
- 查看配置:phpcs --config-show
- 指定报告格式:phpcs --report=summary /path/to/code ;可用的报告格式有full, xml, checkstyle, csv, json, emacs, source, summary, diff, svnblame, gitblame, hgblame, notifysend,默认为full
- 查看帮助:phpcs -h
- 自动修复:phpcbf /path/to/code
- 详细的使用说明见官方wiki
提供一个基于PSR2修改的代码规范,更多的可用规则大家自行clone代码库后分析吧
<?xml version="1.0"?> <ruleset name="Customized"> <description>The customized coding standard based on PSR2 with minor changes.</description> <rule ref="PSR2"> <!-- Allow using tab for indent --> <exclude name="Generic.WhiteSpace.DisallowTabIndent"/> <!-- Allow putting the open brace in the same line of the class declaration --> <exclude name="PSR2.Classes.ClassDeclaration.OpenBraceNewLine"/> <!-- Allow putting the open brace in the same line of the function declaration --> <exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine"/> </rule> <!-- Disable using space for indent --> <rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/> <!-- Require putting the open brace in the same line of the class declaration --> <rule ref="Generic.Classes.OpeningBraceSameLine" /> <!-- Require putting the open brace in the same line of the function declaration --> <rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie" /> </ruleset>
参考:
标签: PHP
还没有人抢沙发呢~