Skip to content

Instantly share code, notes, and snippets.

@d0t15t
Last active August 11, 2021 10:15
Show Gist options
  • Save d0t15t/4ba01f12a689594fd780e03010af9c78 to your computer and use it in GitHub Desktop.
Save d0t15t/4ba01f12a689594fd780e03010af9c78 to your computer and use it in GitHub Desktop.
Add PHP Code Sniffer for Drupal DDEV with git hook

Add PHP-Codesniffer & DrupalStandards

ddev ssh
composer require drupal/coder --dev
composer require dealerdirect/phpcodesniffer-composer-installer --dev
composer require loomgmbh/git-hooks-pre-commit-ddev --dev

Set PHPCS config

# .ddev/setup.local.sh
hook_post_setup() {
  
+  echo "Add custom PHPCS config."
+  phpcs --config-set installed_paths vendor/drupal/coder/coder_sniffer;

Test PHPCS

phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,js,info,txt,md web/modules/custom
phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,js,info,txt,md web/themes/custom
# With automatic fix
phpcbf --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md web/modules/custom

Setup pre-commit hook

When installed, the phpcbf will run on every commit and attempt to make corrections. Corrections made by phpcbf must then be seperately committed.

# .ddev/setup.local.sh
hook_post_setup() {
  echo "Add custom PHPCS config."
  phpcs --config-set installed_paths vendor/drupal/coder/coder_sniffer;

+  echo "Setup pre-commit-ddev.";
+  cp "vendor/loomgmbh/git-hooks-pre-commit-ddev/pre-commit-ddev" ".git/hooks/pre-commit";
+  chmod +x ".git/hooks/pre-commit";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment