itcig / php-coding-standards
PHP_CodeSniffer 规则(sniffs)用于强制执行 CIG 编码规范
Requires
- php: ^7.2 || ^8
- phpcompatibility/php-compatibility: ^9.0
- phpcompatibility/phpcompatibility-wp: ^2.1
- phpcsstandards/phpcsextra: ^1.0
- phpcsstandards/phpcsutils: ^1.0
- sirbrillig/phpcs-variable-analysis: ^2.11
- squizlabs/php_codesniffer: ^3.6
Requires (Dev)
Suggests
- roave/security-advisories: dev-master || Helps prevent installing dependencies with known security issues.
This package is auto-updated.
Last update: 2024-09-16 23:25:53 UTC
README
使用 CIG PHP 编码规范规则集进行 PHPCodeSniffer,您可以分析项目的代码库,以检查其与其它 _itcig 项目的 PHP 兼容性。
此仓库包含什么?
PHPCodeSniffer 的规则集,用于检查所有 _itcig PHP 项目的 PHP 兼容性和标准。
此 WordPress 特定规则集通过排除 WordPress 提供的回填和填充项,防止来自 PHPCompatibility 标准的误报。
要求
这些都将由该规则集自动安装,因此您无需明确包含它们。
- PHP_CodeSniffer。为了获得最佳结果,请使用 PHP_CodeSniffer 的最新稳定版。PHP_CodeSniffer 的最小推荐版本是 3.0.2。
- PHPCompatibility 9.0.0+.
安装说明
唯一支持的安装方法是使用 Composer。
如果您已经安装了 Composer 的 PHP_CodeSniffer 插件,请运行
composer require --dev itcig/php-coding-standards:"*"
composer install
接下来,运行
vendor/bin/phpcs -i
如果一切顺利,您现在将看到已为 PHP_CodeSniffer 安装了 CIG
、CIG-Docs
、CIG-Wordpress
、CIG-Core
、PHPCompatibility
、PHPCompatibilityWP
以及一些更多的 PHPCompatibility 标准。
如何使用
现在您可以使用以下命令来检查您的代码
./vendor/bin/phpcs -p . --standard=CIG
默认情况下,您将只收到有关格式化和已弃用以及/或删除的 PHP 功能的通知。
为了充分利用 CIG 编码和 PHPCompatibility 标准,您应该指定一个 testVersion
以进行对比检查。这将启用对已弃用/删除的 PHP 功能以及使用新 PHP 功能的代码的检测。
如果您想强制最低 PHP 要求,请在命令行命令中添加 --runtime-set testVersion 7.2-
或在您的 自定义规则集 中添加 <config name="testVersion" value="7.2-"/>
。
例如
# For a project which should be compatible with PHP 7.2 and higher: ./vendor/bin/phpcs -p . --standard=CIG --runtime-set testVersion 7.2-
有关设置 testVersion
的更详细信息,请参阅通用 PHPCompatibility 标准的 README。
仅测试 PHP 文件
默认情况下,PHP_CodeSniffer 将分析 PHP、JavaScript 和 CSS 文件。由于 PHPCompatibility sniffs 仅针对 PHP 代码,您可以通过告诉 PHP_CodeSniffer 仅检查 PHP 文件来使运行稍微快一些
./vendor/bin/phpcs -p . --standard=CIG --extensions=php --runtime-set testVersion 7.2-