stellarwp / nexcess-coding-standards
StellarWP 包的集中化编码标准
Requires
- php: ^5.6 | ^7.0 | ^8.0
- dealerdirect/phpcodesniffer-composer-installer: *
- friendsofphp/php-cs-fixer: ^3.5
- phpcompatibility/phpcompatibility-wp: ^2.1
- wp-coding-standards/wpcs: ^2.3
Requires (Dev)
- assertwell/shellcheck: ^1.0
This package is auto-updated.
Last update: 2024-02-19 18:28:42 UTC
README
此包包含用于StellarWP包开发的编码标准的基本集合。
规则通常基于PSR-12编码标准,这在更大的PHP社区中通常被推荐。
此外,由于StellarWP主要关注WordPress,我们的标准还包括一些重要的WordPress相关规则(例如,输出延迟转义,输入清理,nonce使用等)。
包含内容
此包包含两个主要工具
PHP_CodeSniffer
PHP_CodeSniffer 是PHP的事实上的linting工具,有许多预配置的标准可用。
此包包括PHP_CodeSniffer自身,以及WordPress编码标准规则集,PHP兼容性检查,以及Dealerdirect的PHP_CodeSniffer Composer安装程序。
PHP-CS-Fixer
PHP-CS-Fixer 是由几个Symfony团队成员维护的PHP的附加编码标准检查工具。它为更复杂的检查和实验性功能提供了一些灵活性。
安装
应将此包安装为项目的开发依赖项
$ composer require --dev stellarwp/coding-standards
这将在您的项目vendor/bin
目录中自动公开phpcs
,phpcbf
和php-cs-fixer
二进制文件。
您可能还想添加以下Composer脚本来使运行检查更加容易
# composer.json { // ... "scripts": { // ... "test:standards": [ "phpcs --standard=StellarWP --cache ./src ./tests", "php-cs-fixer fix --config=vendor/stellarwp/coding-standards/src/php-cs-fixer.php -v --diff --dry-run" ], "test:standards-fix": [ "phpcbf --standard=StellarWP ./src ./tests", "php-cs-fixer fix --config=vendor/stellarwp/coding-standards/src/php-cs-fixer.php -v --diff" ] }, "scripts-descriptions": [ "test:standards": "Check coding standards.", "test:standards-fix": "Attempt to fix coding standards violations automatically.", ] }
注意:您可能需要调整路径以适应您的项目,特别是如果您已更改Composer的"vendor-dir"。
特定于项目的配置
默认编码标准对项目做了一些假设(所有这些都可以根据每个项目进行覆盖)
- 代码应与PHP 5.6或更高版本兼容(以匹配WordPress的最小要求)
- 代码应与最新的和上一个主要版本的WordPress兼容(即“当前减一”)
- 通过WordPress的国际化(i18n)函数传递的内容应使用“stellarwp”文本域
建议通过创建PHP_CodeSniffer配置文件而不是通过命令行参数覆盖这些值。此包中包含了一个带注释的起始配置,可以通过运行以下命令自动复制到项目根目录中(.phpcs.xml.dist
)
$ vendor/bin/make-phpcs-config
如果您需要更改PHP-CS-Fixer的规则,可以通过运行以下命令发布一个.php-cs-fixer.dist.php
文件:
$ vendor/bin/make-php-cs-fixer-config
如果您已经将默认的test:standards
和test:standards-fix
脚本复制到您的composer.json
文件中,请务必更新它们的参数!
# composer.json { // ... "scripts": { // ... "test:standards": [ "phpcs --cache", "php-cs-fixer fix --config=.php-cs-fixer.dist.php -v --diff --dry-run" ], "test:standards-fix": [ "phpcbf", "php-cs-fixer fix --config=.php-cs-fixer.dist.php -v --diff" ] }, }
许可协议
本库遵循MIT许可协议。