PHP代码规范有PSRs,为了能做到代码规范的自动化检查和修复,就需要用到PHPCS了。
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代码库后分析吧
The customized coding standard based on PSR2 with minor changes.
参考:
本博客所有文章如无特别注明均为原创。