chi-teck/drupal-coder-extension

Drupal编码标准的扩展。

安装次数 48,494

依赖项: 1

建议者: 0

安全性: 0

星标: 5

关注者: 5

分支: 0

开放问题: 0

类型:phpcodesniffer-standard

2.0.0-beta3 2024-04-03 14:17 UTC

This package is auto-updated.

Last update: 2024-09-03 15:08:55 UTC


README

本项目提供了一组预配置的规则,主要用于PHP Code Sniffer,主要针对尚未被Drupal编码标准覆盖的新PHP特性。

系统要求

PHP 8.1+

安装

通过Composer在本地上安装标准。

composer require --dev chi-teck/drupal-coder-extension

DrupalExtended标准添加到项目中的phpcs.xml文件。

<?xml version="1.0"?>
<ruleset name="Drupal Extended"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="../vendor/squizlabs/php_codesniffer/phpcs.xsd">
  <rule ref="DrupalExtended">
    <!-- Sniffs to exclude. -->
  </rule>
    
  <!-- Override settings for enabled rules or enable that excluded. -->
  <rule ref="SlevomatCodingStandard.Classes.RequireAbstractOrFinal.ClassNeitherAbstractNorFinal">
      <exclude-pattern>./src/Exception</exclude-pattern>
  </rule>
  <rule ref="SlevomatCodingStandard.ControlStructures.EarlyExit"/>
</ruleset>

完整的phpcs.xml示例

<?xml version="1.0"?>
<ruleset name="Drupal Extended"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="../vendor/squizlabs/php_codesniffer/phpcs.xsd">
  <description>PHP Code Sniffer configuration for My Project.</description>
  <arg name="colors"/>
  <arg name="extensions" value="php,module,inc,install,theme,info,txt,md,yml"/>
    
  <!-- Paths to scan for problems recursively. -->
  <file>./web/modules/custom</file>
  <file>./web/themes/custom</file>
  <file>./tests</file>

  <!-- Exclude vendors. -->
  <exclude-pattern>./docroot/themes/custom/example/node_modules</exclude-pattern>

  <rule ref="DrupalExtended"/>
</ruleset>

使用方法

运行phpcs脚本来检查自定义代码。

./vendor/bin/phpcs -ps --standard=phpcs.xml

运行phpcbf脚本来自动纠正编码标准违规。

./vendor/bin/phpcbf

您也可以将此命令放入项目的composer.json文件中

{
  
  "scripts": {
    "phpcs": "vendor/bin/phpcs -ps"
  }
}

然后您可以使用它如下

composer phpcs

链接

许可证

GNU通用公共许可证,版本2或更高版本。