chi-teck/drupal-coder-extension

An extension of Drupal coding standards.

Installs: 37 978

Dependents: 1

Suggesters: 0

Security: 0

Stars: 4

Watchers: 5

Forks: 0

Open Issues: 0

Type:phpcodesniffer-standard

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

This package is auto-updated.

Last update: 2024-04-03 14:18:12 UTC


README

This project offers a set of preconfigured rules for PHP Code Sniffer mainly related to new PHP features that are not covered by Drupal coding standards yet.

System Requirements

PHP 8.1+

Installation

Install the standard locally through Composer.

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

Add DrupalExtended standard to your project's 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>

A complete example of 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>

Usage

Run the phpcs script to check custom code.

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

Run the phpcbf script to automatically correct coding standard violations.

./vendor/bin/phpcbf

You also can put this command into projects composer.json:

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

Then you can use it like

composer phpcs

Links

License

GNU General Public License, version 2 or later.