Validating Drupal Custom modules with PHPCS

Submitted by nmeegama on Thu, 10/30/2014 - 10:39

Using PHP Code Sniffer

Running coder module locally on your custom modules will be sufficient in this case (The other contributed modules will already have been validated by drupal.org)

Download the following module from here.

Running code validation using the command line

 

Figure out which shell you're using:
 $ echo $SHELL

 Take note of the text after the last /: this is your shell. Common ones are bash and zsh.

If you're using bash, edit $HOME/.bashrc.

 If you're using zsh, edit $HOME/.zshrc.
At the bottom of the file, add the alias:
alias drupalcs="phpcs --standard=$path_to_coder_module/coder_sniffer/Drupal --extensions='php,module,inc,install,test,profile,theme,js,css,info,txt'"

Re-run your .bashrc or .zshrc file so it picks up your new alias.
 $ source ~/.bashrc

... or...

 $ source ~/.zshrc

... or, restart your shell / terminal.

Now you can use the alias like this:
$ drupalcs sites/all/modules/mymodule

Another Method

Using the PAReview module.
Add the module from here to the code
PHP Code Sniffer Again should be installed.

sites/all/modules/pareviewsh/pareview.sh sites/all/modules/views

Please find a simple script to run you validations below you can always change the error_level and warnning_level to your liking

Code validation script
#!/bin/bash
PROJECT_NAME="Contentlinks"
PROJECTS_WWW_ROOT="/var/www/repo/"
PROJECT_CUSTOM_MODULE_ROOT="sites/all/modules/custom/"
error_level=6
warning_level=6
cd $PROJECTS_WWW_ROOT
cd $PROJECT_NAME
echo '================ Running code validation using PHP Code Sniffer '$PROJECT_NAME ' ==========================='
#specify all the custom modules here
CUSTOM_MODULES=(
contentlinks
)
for module in "${CUSTOM_MODULES[@]}"
do
    echo "========= Validating custom module: $module ========"
    phpcs_output=$(phpcs -p -v --report=json --standard=sites/all/modules/coder/coder_sniffer/Drupal  --extensions='php,module,inc,install,test,profile,theme,js,css,info,txt' --error-severity=$error_level --warning-severity=$warning_level $PROJECT_CUSTOM_MODULE_ROOT$module)
    phpcs_summary_output=$(phpcs -p -v --report=summary --standard=sites/all/modules/coder/coder_sniffer/Drupal  --extensions='php,module,inc,install,test,profile,theme,js,css,info,txt' --error-severity=$error_level --warning-severity=$warning_level $PROJECT_CUSTOM_MODULE_ROOT$module)
    echo $phpcs_output | grep -q "\"totals\":{\"errors\":0"
     
    if ! [ $? -eq 0 ];then
        echo "==== Validation error found in $module ======"
        echo $phpcs_summary_output
        exit 1
    else
        echo "==== Validation Successful $module ======"
        echo $phpcs_summary_output
    fi
    echo "========= Finished Validating custom module: $module ========"
done
echo '================ Finished Running code validation using PHP Code Sniffer '$PROJECT_NAME ' ==========================='

 

 

 

 

Requirements for the module

The module needs to run locally or on a  development environment. The module should not be active on the Production or staging environments

Module
Installation comand
PHP Code Sniffer pear install PHP_CodeSniffer
Set up execution time to a higher value

option 1:

in the settings.php file of your project add the following lines

ini_set('max_execution_time', 300); 

option 2:

in the php.ini file

max_execution_time = 300
 

Option 1 is better because it wont effect other projects running on your local or development environment

Set memory limit to a higher value

option 1:

in the settings.php file of your project add the following lines

ini_set('memory_limit', '128M'); 

option 2:

in the php.ini file

memory_limit = 128M
 

Option 1 is better because it wont effect other projects running on your local or development environment