Upload Image
error logo

Drupal Coding standards provide a set of rules for how your code should be formatted, whether you should use tabs or spaces, what the Naming Convection (Functions, Constants, Global Variables, Classes, and Files) concept in Drupal Standard code, Indenting and Whitespaces, line spacing, guidelines to use operators, array, PHP code tags and more.

For this you need to install Coder and PHP CodeSniffer.

Step 1: Install Coder to Drush’s central store.

If you don’t want to install “Coder” for each Drupal site then install it to Drush’s central store. ( Drush for installation of “Coder”)

Prompt> drush pm-download coder --destination=%HOMEPATH%/.drush

Step 2: Install PHP CodeSniffer

PHP CodeSniffer is a set of two PHP scripts; the main `phpcs` script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second `phpcbf` script to automatically correct coding standard violations. PHP CodeSniffer is an essential development tool that ensures your code remains clean and consistent. It is a PEAR Package.

So before configuration of PHP CodeSniffer, install PEAR Package Manager(You must have PHP and PEAR on your machine.)

Prompt> go-pear (This will install PEAR Package Manager)

OR

Prompt> php go-pear.phar

After this local installation set the environment variable by double clicking “DrushEnv.bat” at path prompt>%homepath/drush

Now it’s time to verify its work or not.

I. Simply type command pear.

  Prompt> pear

  This will show the list of commands like

 command list image

 

II. You can also check pear version

Prompt> pear version

Pear version image

 

III. You can also take help of pear by using pear help command

Prompt> pear help

Prompt> pear help command_name

As for ex:

Prompt> pear help install

 

To use PEAR and PEAR compatible packages in your applications, you normally include them into your PHP scripts using require_once(). For this to work, PEAR's php_dir must be a part of PHP's include path.

IV. First, check where PEAR installs .php files:

Prompt> pear config-get php_dir

V. Now it's time to find which configuration file is used by your PHP installation.

Prompt> php --ini

 Php ini image

To get PEAR working properly, you need to adjust PHP's include_path. After you found php.ini, open it in an editor and modify php.ini

  • Search for the line include_path.
  • Now that you found it, you probably will see a semicolon (;) at the beginning. This means the line is a comment. Add a new line below it.
  • In this line, write:

include_path=".;C:\Program Files\Drush\Php\pear "

After setting up PEAR Package Manager, configure the PEAR Setup. PEAR has a number of configuration options that you can change.

      Prompt> pear config-show

     Prompt> pear config-get php_dir

    Prompt> pear config-set preferred_state beta

You will get message like config-set succeeded Once PEAR start working on your machine then you can install some PEAR’s Packages.

    Prompt> pear install package_name

    Prompt> pear install –f package_name (forcing the command to execute use –f )

    Prompt> pear install PHP_CodeSniffer-1.4.2 (download PHP codeSniffer version 1.4.2)

    Prompt> pear install PHP_CodeSniffer (download latest version of PHP CodeSniffer)

Running CodeSniffer

Check by using any phpcs commande.

    Like    Prompt> phpcs --version

Pear version_1 image

 

Now it’s time to check your code against Code Standard

Prompt> phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme /file/to/drupal/example_module

Prompt> phpcs --standard=Drupal /path/to/example.module

Prompt> phpcs –standard=PEAR /path/to/example.module

Prompt> phpcs –standard=MySource /path/to/example.module

Prompt> phpcs --standard=Drupal /path/to/example.css

Prompt> phpcs –standard=PEAR /path/to/example.css

Prompt> phpcs –standard=MySource /path/to/example.css

Test:

Prompt> phpcs –standard=Drupal path/to/…/sites/all/themes/templates/page—home_page.tpl.php

Error_0 image