elendev / drupal-coder
Coder是一个用于审查Drupal代码的库。
Requires
- php: >=7.1
- ext-mbstring: *
- dealerdirect/phpcodesniffer-composer-installer: ^0.7.1
- sirbrillig/phpcs-variable-analysis: ^2.10
- slevomat/coding-standard: ^7.0
- squizlabs/php_codesniffer: ^3.6.0
- symfony/yaml: >=2.0.5
Requires (Dev)
- phpstan/phpstan: ^1.4.9
- phpunit/phpunit: ^7.0 || ^8.0
Replaces
- drupal/coder: *
This package is not auto-updated.
Last update: 2024-09-25 19:44:48 UTC
README
Coder是一个用于自动审查Drupal代码和修复编码规范的库。它为PHP_CodeSniffer定义了规则
内置支持
- "Drupal": 编码规范 https://www.drupal.org/coding-standards
- "DrupalPractice": Drupal模块开发的最佳实践
在线文档: https://www.drupal.org/node/1419980
注意:已移除对JavaScript的支持。请使用ESLint检查和修复JavaScript文件,并查看Drupal ESLint文档。
全局安装
首先,确保Composer已正确安装
which composer
如果您遇到“composer not found”或类似问题,请按照Composer的安装说明进行操作。
在您的家目录中的全局Composer目录(~/.config/composer
)中安装Coder(8.3.x)
composer global require drupal/coder
要将phpcs
和phpcbf
命令添加到全局路径,请将Composer bin路径添加到~/.profile
、~/.bashrc
或~/.zshrc
中的$PATH
变量
export PATH="$PATH:$HOME/.config/composer/vendor/bin"
用法
检查Drupal编码规范
phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml /file/to/drupal/example_module
检查Drupal最佳实践
phpcs --standard=DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml /file/to/drupal/example_module
自动修复编码规范
phpcbf --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml /file/to/drupal/example_module
在您的Drupal项目中本地安装
对于协作开发团队,在本地锁定Coder版本与您的其他开发依赖项是有意义的。在包含您的composer.json文件的项目中执行以下操作
composer require --dev drupal/coder
然后您可以像这样在您的自定义模块代码上运行Coder
./vendor/bin/phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml web/modules/custom
为了简化执行,强烈建议使用设置文件,见下文。
在 phpcs.xml.dist 文件中存储设置
为了将PHPCS设置保存并提交到Git,您可以在项目中使用如下的 phpcs.xml.dist 文件
<?xml version="1.0" encoding="UTF-8"?> <ruleset name="myproject"> <description>PHP CodeSniffer configuration for myproject development.</description> <!-- Check all files in the current directory and below. --> <file>.</file> <arg name="extensions" value="php,module,inc,install,test,profile,theme,css,info,txt,md,yml"/> <!-- Change this value to 7 if you want to check Drupal 7 code. --> <config name="drupal_core_version" value="8"/> <!-- If you have Coder installed locally then you can reference the Drupal standards with relative paths. Otherwise simply use "Drupal" and "DrupalPractice. --> <rule ref="../vendor/drupal/coder/coder_sniffer/Drupal"> <!-- Example how you would disable a rule you are not compliant with yet: <exclude name="Drupal.Commenting.Deprecated"/> --> </rule> <rule ref="../vendor/drupal/coder/coder_sniffer/DrupalPractice"/> <!-- Example how you would disable an external rule you do not like: <rule ref="PEAR.Functions.ValidDefaultValue.NotAtEnd"> <severity>0</severity> </rule> --> </ruleset>
然后您可以不使用任何选项直接调用 phpcs,它将从这个当前目录读取 phpcs.xml.dist。这也可以用于持续集成设置。示例
./vendor/bin/phpcs -p
与编辑器一起使用
Drupal Code Sniffer可以与各种编辑器一起使用。
编辑器
自动化测试 (PHPUnit + PHPCS + PHPStan)
Coder Sniffer附带了PHPUnit测试套件,以确保嗅探器正常工作。使用Composer安装依赖项
composer install
然后执行测试
./vendor/bin/phpunit
然后对Coder本身执行编码规范检查器
./vendor/bin/phpcs
然后执行静态分析
./vendor/bin/phpstan analyse
贡献
- 确保在https://www.drupal.org/project/issues/coder存在一个问题
- 在https://github.com/pfrenssen/coder上创建一个Pull Request
- 将Pull Request的链接发布到drupal.org上的问题,并将问题设置为“需要审查”
谢谢!
维护者
- Pieter Frenssen, https://www.drupal.org/u/pfrenssen
- Michael Welford, https://www.drupal.org/u/mikejw
- 克劳斯·普勒,https://www.drupal.org/u/klausi
鸣谢
格雷格·舍伍德和Squiz Pty Ltd,许多嗅探器是他们对PHPCS原始工作的修改版本。