PHPCSStandards/PHP_CodeSniffer
GitHub: PHPCSStandards/PHP_CodeSniffer
PHP_CodeSniffer 是一款用于检测和自动修正 PHP 代码规范违规的开发工具,帮助团队保持代码整洁一致。
Stars: 1537 | Forks: 112
# PHP_CodeSniffer
## 关于
PHP_CodeSniffer 包含两个 PHP 脚本;主要的 `phpcs` 脚本会对 PHP 文件进行分词,以检测是否违反了既定的编码规范,另一个 `phpcbf` 脚本则用于自动修正编码规范的违规之处。PHP_CodeSniffer 是一款必不可少的开发工具,它能确保你的代码保持整洁和一致。
## 环境要求
PHP_CodeSniffer 需要 PHP 7.2.0 或更高版本。有关必需和推荐的 PHP 扩展的更多信息,请参阅[要求手册页](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Requirements)。
个别的 sniff 可能有额外的要求,例如外部应用程序和脚本。有关这些要求的列表,请参阅[配置选项手册页](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Configuration-Options)。
如果你是在团队中使用 PHP_CodeSniffer,或者在 [CI](https://en.wikipedia.org/wiki/Continuous_integration) 服务器上运行它,你可能需要[使用配置文件](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Advanced-Usage#using-a-default-configuration-file)来配置项目的设置。
## 安装
开始使用 PHP_CodeSniffer 最简单的方法是下载各个命令的 Phar 文件:
```
# 使用 curl 下载
curl -OL https://phars.phpcodesniffer.com/phpcs.phar
curl -OL https://phars.phpcodesniffer.com/phpcbf.phar
# 或者使用 wget 下载
wget https://phars.phpcodesniffer.com/phpcs.phar
wget https://phars.phpcodesniffer.com/phpcbf.phar
# 然后测试下载的 PHARs
php phpcs.phar -h
php phpcbf.phar -h
```
这些 Phar 文件使用 PHPCS 的官方发布密钥进行签名,其
指纹为 `D91D 8696 3AF3 A29B 6520 4622 97B0 2DD8 E507 1466`。
从 PHP_CodeSniffer 3.10.3 版本开始,可以使用 [GitHub CLI 工具](https://cli.github.com/)通过 [GitHub Artifact Attestations](https://docs.github.com/en/actions/how-tos/secure-your-work/use-artifact-attestations/use-artifact-attestations) 来验证与某个版本相关的 PHAR 文件的来源,命令如下:`gh attestation verify [phpcs|phpcbf].phar -o PHPCSStandards`。
### Composer
如果你使用 Composer,可以通过以下命令在系统全局安装 PHP_CodeSniffer:
```
composer global require "squizlabs/php_codesniffer=*"
```
确保你的 PATH 包含 Composer 的 bin 目录。默认值为 `~/.composer/vendor/bin/`,但你可以通过运行 `composer global config bin-dir --absolute` 来查看你需要使用的具体路径。
或者,你也可以在你的 `composer.json` 文件中添加 `squizlabs/php_codesniffer` 依赖。例如:
```
{
"require-dev": {
"squizlabs/php_codesniffer": "^4.0"
}
}
```
然后你就可以从 vendor bin 目录运行 PHP_CodeSniffer 了:
```
./vendor/bin/phpcs -h
./vendor/bin/phpcbf -h
```
### Phive
如果你使用 Phive,可以使用以下命令将 PHP_CodeSniffer 作为项目工具进行安装:
```
phive install --trust-gpg-keys D91D86963AF3A29B6520462297B02DD8E5071466 phpcs
phive install --trust-gpg-keys D91D86963AF3A29B6520462297B02DD8E5071466 phpcbf
```
然后你就可以从 `tools` 目录运行 PHP_CodeSniffer 了:
```
./tools/phpcs -h
./tools/phpcbf -h
```
### Git 克隆
你也可以下载 PHP_CodeSniffer 的源码,并直接从 Git 克隆中运行 `phpcs` 和 `phpcbf` 命令:
```
git clone https://github.com/PHPCSStandards/PHP_CodeSniffer.git
cd PHP_CodeSniffer
php bin/phpcs -h
php bin/phpcbf -h
```
## 快速入门
PHP_CodeSniffer 使用的默认编码规范是 PSR12 编码规范。要根据 PSR12 编码规范检查某个文件,只需指定该文件的路径:
```
phpcs /path/to/code/myfile.php
```
或者,如果你想检查整个目录,可以指定目录路径而不是文件路径。
```
phpcs /path/to/code-directory
```
如果你想根据 PEAR 编码规范检查代码,请使用 `--standard` 命令行参数:
```
phpcs --standard=PEAR /path/to/code-directory
```
如果 PHP_CodeSniffer 发现任何编码规范错误,运行命令后会显示一份报告。
完整的使用说明和示例报告可在[使用页面](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Usage)上找到。
## 文档
PHP_CodeSniffer 的文档可在 [GitHub wiki](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki) 上找到。
## 问题
可以在 [GitHub Issue Tracker](https://github.com/PHPCSStandards/PHP_CodeSniffer/issues) 上提交错误报告和功能请求。
## 版本控制
PHP_CodeSniffer 使用 `MAJOR.MINOR.PATCH`(主版本号.次版本号.修订号)的版本编号格式。
`MAJOR`(主)版本号在以下情况会增加:
- 对 `phpcs` 或 `phpcbf` 命令的使用方式进行了不向后兼容的更改,或
- 对 `ruleset.xml` 格式进行了不向后兼容的更改,或
- 对 sniff 开发者使用的 API 进行了不向后兼容的更改,或
- 移除了自定义的 PHP_CodeSniffer token 类型,或
- 完全从 PHP_CodeSniffer 中移除了现有的 sniff
`MINOR`(次)版本号在以下情况会增加:
- 向 `phpcs` 和 `phpcbf` 命令添加了向后兼容的新功能,或
- 对 `ruleset.xml` 格式进行了向后兼容的更改,或
- 对 sniff 开发者使用的 API 进行了向后兼容的更改,或
- 向内置标准中添加了新的 sniff,或
- 从内置标准中移除了现有的 sniff
`PATCH`(修订)版本号在以下情况会增加:
- 进行了向后兼容的 bug 修复
标签:ffuf, OpenVAS, PHP, SOC Prime, 云安全监控, 代码审查, 代码规范, 开发工具, 静态分析