Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save preciado04/85a4d78d745c2a2daace95301fda4d9e to your computer and use it in GitHub Desktop.
Save preciado04/85a4d78d745c2a2daace95301fda4d9e to your computer and use it in GitHub Desktop.
Set up PHP CodeSniffer with Drupal coding standards on pre-commit git hook.

Set up PHP CodeSniffer with Drupal coding standards on pre-commit git hook.

This script has the goal of improving the development of custom code for themes, modules oriented to drupal and general code. It validates Drupal coding standards on files before commit them and returns a message to fix each line of code on the files having errors.

Follow next steps to set up pre commit git hook with Drupal coding standards:

  1. composer require drupal/coder dealerdirect/phpcodesniffer-composer-installer
  2. Create hooks directory on root project.
  3. Create pre-commit file into hooks directory.
  4. Add pre-commit code to hooks/pre-commit file.
  5. Add phpcs.xml code to phpcs.xml file.
  6. Add PHP commands on scripts section of composer.json file.

Install Drupal Coder at project label.

composer require drupal/coder dealerdirect/phpcodesniffer-composer-installer

Add composer changes, commit them and push to repository, so that other developers in the team can install the 'pre-commit' git hook, pulling changes from repository and running composer install.

git pull <remote_name> <branch_name>
composer install

How to use it

git commit -m "The commit message."
  • This is going to execute Drupal Coder on each file and returns messages on terminal in case of finding errors on files.

Example:

Command

 git commit -m "Testing Drupal Coder."

Terminal output

Drupal Coder pre-commit hook – commit with the --no-verify option to skip the hook

Running Drupal Coder.

E 1 / 1 (100%)



FILE: ...js/scripts.js

----------------------------------------------------------------------

FOUND 5 ERRORS AFFECTING 5 LINES

----------------------------------------------------------------------

 16 | ERROR | [x] Expected 1 space after FUNCTION keyword; 0 found

 23 | ERROR | [x] Whitespace found at end of line

 38 | ERROR | [x] Whitespace found at end of line

 45 | ERROR | [x] Whitespace found at end of line

 79 | ERROR | [x] Expected 1 newline at end of file; 0 found

----------------------------------------------------------------------

PHPCBF CAN FIX THE 5 MARKED SNIFF VIOLATIONS AUTOMATICALLY

----------------------------------------------------------------------

Time: 384ms; Memory: 30MB

[develop 5e53248] Testing Drupal Coder.
 1 file changed, 1 insertion(+)

Ignore files

Just add --ignore flag to PHPCS command and speficy the files you want to be ignored by PHP CodeSniffer.

"--ignore=*/style.css, */sq-payment-form.cs

The complete command will be as shown below.

PHPCS=("$BIN/phpcs" "--standard=$STANDARD" "--ignore=*/style.css, */sq-payment-form.css" "--filter=gitstaged" "--encoding=utf-8" "-p" ".")
"${PHPCS[@]}"
#!/bin/bash
STANDARD="./phpcs.xml"
EXTENSIONS="php,module,theme,inc,install,test,profile,js"
BIN="./vendor/bin"
echo
echo "Drupal Coder pre-commit hook – commit with the --no-verify option to skip the hook"
echo
# Check whether PHP_CodeSniffer can be found
if [ ! -f "$BIN/phpcs" ]
then
echo "Drupal Coder not found – is it installed? 'composer require drupal/coder'"
echo
exit 1
fi
# Retrieve staged files
FILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD)
# Run the sniffer
echo "Running Drupal Coder."
echo
PHPCS=("$BIN/phpcs" "--standard=$STANDARD" "--extensions=$EXTENSIONS" "--ignore=*/*.css" "--filter=gitstaged" "--encoding=utf-8" "-p" ".")
"${PHPCS[@]}"
# Syntax Error
if [ $? != 0 ]
then
echo "Please fix each violations detected."
echo
exit 1
fi
# Syntax OK
if [ $? == 0 ]
then
echo "No violations detected"
echo
exit 0
fi
"scripts": {
"post-install-cmd": [
"php -r \"copy('hooks/pre-commit', '.git/hooks/pre-commit');\"",
"php -r \"chmod('.git/hooks/pre-commit', 0755);\""
]
}
<?xml version="1.0"?>
<ruleset>
<file>.</file>
<config name="testVersion" value="5.5-"/>
<config name="color" value="1"/>
<rule ref="Drupal"/>
<rule ref="Generic.PHP.UpperCaseConstant">
<exclude-pattern>*.js</exclude-pattern>
</rule>
</ruleset>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment