1.本地检查及修复工具
# 工具名为PHP_CodeSniffer,项目地址为https://github.com/squizlabs/PHP_CodeSniffer,其中phpcs.phar为检查工具,phpcbf.phar为修复工具
2.安装
1.curl安装curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.pharcurl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar2.wget安装wget https://squizlabs.github.io/PHP_CodeSniffer/phpcs.pharwget https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar3.composer安装composer global require "squizlabs/php_codesniffer=*"4.phive安装phive install phpcsphive install phpcbf5.pear安装pear install PHP_CodeSniffer6.git clonegit clone https://github.com/squizlabs/PHP_CodeSniffer.git
3.设置
1.设置标准(一般采用PSR2)php phpcs.phar --config-set default_standard PSR2# 查看已经安装的标准php phpcs.phar -i2.设置字符集php phpcs.phar --config-set encoding utf-8
4.使用
1.默认检测php phpcs.phar /path/to/file.phpphp phpcs.phar /path/to/directory2.PSR2检测# 默认不指定standard参数,则PSR1生效,而PSR1很多规则已经废弃不再适用,我们一般采用PSR2检测风格php phpcs.phar --standard=PSR2 /path/to/file.phpphp phpcs.phar --standard=PSR2 /path/to/direcotry3.修复# 检测出的问题可通过自带工具自动修复,不过我一般手动,原因你懂的php phpcbf.phar /path/to/file.phpphp phpcbf.phar /path/to/directory
5.与git集成
# 通过与git集成可以达到若检测不通过,将无法提交代码的效果1.编辑pre-commit钩子# 打开当前项目的.git/hooks目录,里面有很多xxx.sample文件,其中一个就是pre-commit.samplecp pre-commit.sample pre-commit && vim pre-commit2.将pre-commit的内容替换如下wget https://evernote-1258366890.cos.ap-guangzhou.myqcloud.com/1911061541.sh