Raised: $0
0% of monthly goal Help us cross the finish line!
Goal: $12,000
Raised: $0 Goal: $12,000
0% of monthly goal Help us cross the finish line!
Sponsor DDEV

If you find this add-on useful, please star it on GitHub — stars show appreciation and help maintainers know their work matters.

DDEV PHP Code Quality

Adds phpcs, phpcbf and phpstan commands to wrap web container commands.

Installation

ddev add-on get useraccesshub/ddev-php-code-quality
ddev restart

Post Installation Configuration

This add-on does not include any specific standards so these will need to be included manually.

Drupal 11

  1. Add the drupal/core-dev package or individual phpcs/phpstan packages:
ddev composer require --dev drupal/core-dev

OR

ddev composer require --dev drupal/coder
ddev composer require --dev phpstan/phpstan phpstan/extension-installer mglaman/phpstan-drupal phpstan/phpstan-deprecation-rules
  1. Add a phpcs.xml file at the project root:
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Drupal Project">
  <description>Default PHP CodeSniffer configuration for Drupal.</description>

  <!-- Check these file extensions -->
  <arg name="extensions" value="php,module,inc,install,test,profile,theme"/>
  
  <!-- Use colors in output and show sniff names -->
  <arg name="colors"/>
  <arg value="s"/>

  <!-- Core Drupal standards from the Coder module -->
  <rule ref="Drupal"/>
  <rule ref="DrupalPractice"/>

  <!-- Files or directories to check -->
  <file>web/modules/custom</file>
  <file>web/themes/custom</file>

  <!-- Directories to ignore -->
  <exclude-pattern>*/vendor/*</exclude-pattern>
  <exclude-pattern>*/node_modules/*</exclude-pattern>
  <exclude-pattern>*/dist/*</exclude-pattern>
</ruleset>

  1. Add a phpstan.neon file at the project root:
parameters:
  level: 5
  excludePaths:
    - *node_modules/*
    - *vendor/*
    - *.twig
  fileExtensions:
    - php
    - module
    - inc
    - install
    - test
    - profile
    - theme

Usage

Run commands

ddev phpcs path/to/file/or/directory
ddev phpcbf path/to/file/or/directory
ddev phpstan analyze path/to/file/or/directory

Show configuration options:

ddev phpcs --config-show

Get installed code standards:

ddev phpcs -i

If you find this add-on useful, please star it on GitHub — stars show appreciation and help maintainers know their work matters.