🇬🇧 Configure PHPCS and PHPStan in DDEV for Drupal

Configure PHPCS and PHPStan in DDEV for Drupal

✅ PHPCS and PHPStan Setup with DDEV for Drupal Projects

Follow this quick guide to configure your Drupal project with code analysis tools using DDEV, Composer and custom config files.

📋 Prerequisites

Before starting, make sure you have the following installed:

  • ✅ A working Drupal project (9.x or 10.x)
  • DDEV installed and initialized in your project
  • Composer available inside your DDEV container
  • ✅ Your custom code is located under docroot/modules/custom, themes/custom, or similar

🛠️ 1. Install PHP_CodeSniffer and Drupal Coder standards

ddev composer require --dev squizlabs/php_codesniffer drupal/coder

🔧 2. Set the installed paths for PHPCS

vendor/bin/phpcs --config-set installed_paths vendor/drupal/coder/coder_sniffer
vendor/bin/phpcs -i

You should see something like:

The installed coding standards are MySource, PEAR, PSR1, PSR2, PSR12, Squiz, Zend, Drupal, DrupalPractice

📦 3. Install PHPStan and Drupal extensions

ddev composer require --dev phpstan/phpstan phpstan/extension-installer mglaman/phpstan-drupal

📁 4. Create your configuration files

phpcs.xml

<?xml version="1.0"?>
<ruleset name="Cosmos PHP coding standards for Drupal">
  <rule ref="Drupal">
    <exclude name="Drupal.CSS.ClassDefinitionNameSpacing"/>
    <exclude name="Drupal.CSS.ColourDefinition"/>
    <exclude name="Squiz.CSS.Indentation"/>
    <exclude name="Squiz.CSS.MissingColon"/>
    <exclude name="Squiz.CSS.SemicolonSpacing"/>
    <exclude name="Squiz.CSS.ColonSpacing"/>
    <exclude name="Squiz.CSS.ClassDefinitionOpeningBraceSpace"/>
    <exclude name="Squiz.CSS.ClassDefinitionClosingBraceSpace"/>
    <exclude name="Squiz.CSS.EmptyClassDefinition"/>
    <exclude name="Squiz.CSS.EmptyStyleDefinition"/>
    <exclude name="Squiz.CSS.DisallowMultipleStyleDefinitions"/>
    <exclude name="Squiz.WhiteSpace.LanguageConstructSpacing"/>
    <exclude name="MySource.Debug.DebugCode"/>
  </rule>
  <rule ref="DrupalPractice"/>
  <file>./docroot/modules/custom/</file>
  <file>./docroot/themes/custom/</file>
  <file>./docroot/profiles/custom/</file>
  <file>./docroot/sites/default/settings.php</file>
  <exclude-pattern>*/.git/*</exclude-pattern>
  <exclude-pattern>*/config/*</exclude-pattern>
  <exclude-pattern>*/css/*</exclude-pattern>
  <exclude-pattern>*/js/*</exclude-pattern>
  <exclude-pattern>*/icons/*</exclude-pattern>
  <exclude-pattern>*/vendor/*</exclude-pattern>
  <exclude-pattern>*/node_modules/*</exclude-pattern>
  <exclude-pattern>*rules_export.txt</exclude-pattern>
  <arg name="extensions" value="php,module,inc,install,test,profile,theme,info,twig" />
  <arg name="cache" value=".phpcs-cache" />
  <arg name="colors"/>
  <arg value="sp"/>
</ruleset>

phpstan.neon

parameters:
  level: 5
  paths:
    - %currentWorkingDirectory%/docroot/modules/custom
  excludePaths:
    - *node_modules/*
    - *.twig
  fileExtensions:
    - php
    - module
    - inc
    - install
    - test
    - profile
    - theme
  reportUnmatchedIgnoredErrors: false
  ignoreErrors:
    - '#^Unsafe usage of new static#'
  treatPhpDocTypesAsCertain: false

🚀 5. Run the tools

PHPCS:

vendor/bin/phpcs

phpcs

PHPStan:

vendor/bin/phpstan analyse

phpcs

✅ Done!

You now have PHPCS and PHPStan configured with Drupal standards inside your DDEV project. This will help you maintain high code quality and follow community best practices.

Authored by Eduardo Telaya.


© 2024. All rights reserved.