wearerequired / coding-standards
必需的编码标准
Requires
- php: ^7.2 || ^8.0
- phpcompatibility/phpcompatibility-wp: 2.1.4
- sirbrillig/phpcs-variable-analysis: 2.11.17
- slevomat/coding-standard: 8.14.1
- wp-coding-standards/wpcs: 3.0.1
Suggests
- dealerdirect/phpcodesniffer-composer-installer: Automatically adds this package's ruleset to the installed_paths option of PHP_CodeSniffer
- dev-master
- 6.0.0
- 6.0.0-RC.0
- 6.0.0-alpha.1
- 6.0.0-alpha.0
- 5.0.0
- 4.0.0
- 4.0.0-beta.1
- 4.0.0-beta.0
- 3.0.0
- 3.0.0-beta.0
- 3.0.0-alpha.0
- 2.2.0
- 2.1.0
- 2.0.0
- 2.0.0-rc.3
- 2.0.0-rc.2
- 2.0.0-rc.1
- 2.0.0-rc.0
- 2.0.0-beta.0
- 1.6.0
- 1.5.1
- 1.5.1-alpha.0
- 1.5.0
- 1.5.0-beta.5
- 1.5.0-beta.4
- v1.5.0-beta.3
- v1.5.0-beta.2
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.0
- dev-dependabot/composer/wp-coding-standards/wpcs-3.1.0
- dev-dependabot/composer/slevomat/coding-standard-8.15.0
- dev-dependabot/npm_and_yarn/braces-3.0.3
- dev-dependabot/npm_and_yarn/axios-1.7.7
- dev-dependabot/npm_and_yarn/stylelint-config-recommended-scss-14.1.0
- dev-dependabot/npm_and_yarn/ejs-3.1.10
- dev-dependabot/composer/phpcompatibility/phpcompatibility-wp-2.1.5
- dev-dependabot/npm_and_yarn/follow-redirects-1.15.6
- dev-dependabot/npm_and_yarn/ip-2.0.1
This package is auto-updated.
Last update: 2024-09-24 13:22:50 UTC
README
必需的编码标准代表了在团队中实现一致代码风格的最佳实践。这些标准不仅包含风格规则,还旨在防止潜在的安全漏洞或利用更现代的语法以提高性能。
本项目受到Human Made 编码标准项目的启发。
关于 JavaScript 编码标准,请访问 @wearerequired/eslint-config。
关于 (S)CSS 编码标准,请访问 @wearerequired/stylelint-config。
对于团队成员:如果您需要在环境中设置编码标准,请联系我们的内部手册。
必需的 PHP 编码标准
设置
composer require --dev dealerdirect/phpcodesniffer-composer-installer wearerequired/coding-standards
- 使用以下命令运行标准检查
vendor/bin/phpcs .
最后的 .
指定要测试的文件;这通常是当前目录(.
),但您也可以通过指定它们来选择性地检查文件或目录。
您可以将此添加到您的 Travis CI 配置中作为测试
script: - phpunit - vendor/bin/phpcs .
附加功能:为了方便起见,您可以将以下内容添加到您的 composer.json
文件中
{ "scripts": { "format": "vendor/bin/phpcbf --report-summary --report-source .", "lint": "vendor/bin/phpcs --report-summary --report-source ." } }
之后,运行 composer lint
以运行标准检查,并运行 composer format
以尝试自动修复错误和警告。
排除文件
此标准包括对 .phpcsignore
文件(将来,应将其内置到 phpcs 中)的特殊支持。您可以在根目录中放置一个 .phpcsignore
文件(您要从那里运行 phpcs
)。
此文件的格式类似于 .gitignore
和类似的文件:每行一个模式,注释行应以 #
开头,仅包含空白的行将被忽略。
# Exclude our tests directory.
tests/
# Exclude any file ending with ".inc"
*\.inc
注意,模式应匹配 PHP_CodeSniffer 风格:*
被转换为 .*
以方便使用,但所有其他字符都按正则表达式工作。
模式相对于包含 .phpcsignore
文件的目录。在加载时,它们被转换为绝对模式:例如,在 /your/dir/.phpcsignore
中的 */tests/*
将成为正则表达式 /your/dir/.*/tests/.*
。 这与常规 PHP_CodeSniffer 实践不同。
高级/扩展
如果您想扩展这些编码标准,可以创建自己的自定义标准文件(例如 phpcs.xml.dist
)。
<?xml version="1.0"?> <ruleset name="Project Standard"> <description>Project specific coding standard.</description> <!-- Files or directories to check --> <file>.</file> <!-- Use required Coding Standards --> <rule ref="Required-Strict"/> <!-- The minimum supported WordPress version for all sniffs which use it. --> <config name="minimum_supported_wp_version" value="6.1"/> <!-- The minimum PHP requirement. --> <config name="testVersion" value="7.4-"/> <!-- Your custom rules go here --> </ruleset>
然后,在运行 phpcs 时可以引用此文件。
vendor/bin/phpcs
自定义规则的示例是验证文本域
<rule ref="WordPress.WP.I18n"> <properties> <property name="text_domain" type="array"> <element value="plugin-name"/> </property> </properties> </rule>
或确保使用正确的前缀
<rule ref="WordPress.NamingConventions.PrefixAllGlobals"> <properties> <property name="prefixes" type="array"> <element value="Required\PluginName"/> <element value="plugin_name"/> </property> </properties> </rule>
如果项目是 WordPress 主题,您必须设置以下规则
<rule ref="WordPress.Files.FileName"> <properties> <property name="is_theme" value="true" /> </properties> </rule>
包含的检查
phpcs标准基于WordPress编码标准中的WordPress-Core
、WordPress-Docs
和WordPress-Extra
规则。它还使用了VariableAnalysis来处理问题变量使用,以及Slevomat编码标准中的某些SlevomatCodingStandard
规则。大多数嗅探器还为phpcbf
提供自动错误修复。
EditorConfig
EditorConfig标准帮助开发者在不同的编辑器和IDE之间定义和维护一致的编码风格。这样就可以更容易地遵循项目中的给定编码标准,而无需调整IDE设置。
WordPress项目的推荐.editorconfig
文件如下所示
root = true [*] charset = utf-8 indent_style = tab indent_size = 4 end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true [*.md] trim_trailing_whitespace = false [{*.json,*.yml}] indent_style = space indent_size = 2