PHP
文章内容
PHP 常用代码规范检查工具如下:
工具 | lint 命令 | 支持 IDE |
---|---|---|
CodeSniffer | phpcs –standard=PSR12 app/ | VSCode、IDEA |
PHPMD | phpmd app/ | VSCode |
PHPStan | phpstan analyse app tests | VSCode |
CodeSniffer
CodeSniffer 内置多种规范,其中常用的「PSR12」规范包括下列规则:
- 每行代码最大长度 120 个字符;
- 运算符左右各 1 个空格;
安装:
$ composer require --dev squizlabs/php_codesniffer
全量检查:
$ ./vendor/bin/phpcs --standard=PSR12 src/
FILE: laravel-wechat/src/ServiceProvider.php
-------------------------------------------------------------------
FOUND 30 ERRORS AND 1 WARNING AFFECTING 12 LINES
-------------------------------------------------------------------
43 | ERROR | [x] Expected at least 1 space before "."; 0 found
43 | ERROR | [x] Expected at least 1 space after "."; 0 found
91 | WARNING | [ ] Line exceeds 120 characters; contains 130 characters
101 | ERROR | [x] Expected at least 1 space before "."; 0 found
101 | ERROR | [x] Expected at least 1 space after "."; 0 found
本地增量检查:
git diff --diff-filter=d --name-only HEAD | xargs ./vendor/bin/phpcs --extensions=php --standard=PSR12
持续集成合并请求增量检查:
sh "git diff --diff-filter=d --name-only ${env.MR_TARGET_BRANCH}... | xargs ./vendor/bin/phpcs --extensions=php --standard=PSR12"
问题反馈 >
2022-05-07最近更新
感谢反馈有用
感谢反馈没用
在阅读中是否遇到以下问题?*
您希望我们如何改进?*
如果您希望得到回复,请留下您的邮箱地址。