chi-teck/drupal-coder-extension

An extension of Drupal coding standards.

Maintainers

👁 chi-teck

Package info

github.com/Chi-teck/drupal-coder-extension

Type:phpcodesniffer-standard

pkg:composer/chi-teck/drupal-coder-extension

Statistics

Installs: 85 474

Dependents: 2

Suggesters: 0

Stars: 5

Open Issues: 0

2.0.0-rc2 2025-03-10 11:10 UTC

Requires

Requires (Dev)

None

Provides

None

Conflicts

None

Replaces

None

GPL-2.0-or-later 1af986a0503334d64fafad25f2cde6b4de487ed0


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.