coding standards

Hello Folks, today we are going to install tools for easy drupal development. Like a good law abiding citizen we also must abide laws of drupal coding standards when we code. So here are the tools we need to code according to drupal coding standards:

  • Drupal Coder
  • PHP_Sniffer
  • Composer

Install Composer

To install composer globally on MAC OSX with Homebrew we can just run 

$ brew install composer

on terminal or if you don't have homebrew or on windows or linux goto the official Composer website and install it according to instructions there. 

Install Drupal Coder and PHP Code Sniffer

PHP_CodeSniffer is a library that tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards. It works with Drupal 6, 7, or 8. 

Coder contains "sniffs" for PHP CodeSniffer. These "sniffs" tell PHP CodeSniffer whether code meets Drupal coding standards or not. Specifically there are two rulesets, Drupal and DrupalPractice. The former is intended to enforce the general standards and the latter – previously known as DrupalPractice sniffer – is aimed at module developers who want to avoid common mistakes.

Don't install PHP Code Sniffer separately from Drupal Coder as it is included in Drupal Coder also latest version of PHP Code Sniffer does not work with Drupal Coder. So we just need to install drupal coder which also install PHP Code Sniffer.

Install Coder (8.x-2.x) in your global Composer directory in your home directory. (The directory is given by COMPOSER_HOME configuration option)

$ composer global require drupal/coder

On many systems this will install coder in ~/.composer/vendor/drupal/coder. This location is the location assumed below.

On Windows, the path may look like C:/Users/<WindowsUsername>/AppData/Roaming/Composer, which can also be written as $HOME/AppData/Roaming/Composer. In the code below, simply replace ~/.composer/vendor with $HOME/AppData/Roaming/Composer/vendor.

You can check the installed location by doing:

$ composer global show -P

If the location is not equivalent to ~/.composer/vendor/drupal/coder then you will need to update the commands below. The ~ will be replaced with your user's home directory. A common variant is ~/.config/composer/vendor/drupal/coder.

To make the phpcs and phpcbf commands available globally, add those to your $PATH variable in ~/.profile, ~/.bash_profile, ~/.bashrc or ~/.zshrc:

export PATH="$PATH:$HOME/.composer/vendor/bin"

 

 

Register Coder Standards

PHPCS comes with some included Standards already registered. With Coder Sniffer now installed, we need to register the Drupal and DrupalPractice Standards so that PHPCS can use them. This can be done manually, or by using a Composer installer plugin.

Composer Installer Plugin

To register Composer required PHPCS Standards, during Composer install and update, you can use a Composer Installer Plugin for packages of "type": "phpcodesniffer-standard". This type was added as a feature of Coder Sniffer in version 8.x-2.11 (semver 8.2.11).

The dealerdirect/phpcodesniffer-composer-installer package is one such Composer Installer Plugin for PHPCS Standards. You can use it globally like this:

composer global require drupal/coder:^8.2.12
composer global require dealerdirect/phpcodesniffer-composer-installer

The above command should return something like this:

PHP CodeSniffer Config installed_paths set to ~/.composer/vendor/drupal/coder/coder_sniffer

Manually Set Installed Paths

To manually register the Drupal and DrupalPractice Standard with PHPCS, you must set the installed paths:

$ phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer

Above, command should return Config value "installed_paths" updated successfully on console. If it doesn't, ensure you have latest stable version of PHP_CodeSniffer installed. As of 22nd November 2016, it's 2.7.0 (stable).

Verify Registered Standards

You can verify this has worked with:

$ phpcs -i

Above command should include DrupalPractice and Drupal in the installed coding standards list.

Install Sublime Linter

SublimeLinter is coding linting framework for Sublime Text. Install it via Package Control. We also need SublimeLinter-phpcs to use php code sniffer to lint out code accordingly to Drupal Coding Standards. After installing these go to SublimeLinter Settings via Preference->Package Settings->SublimeLinter and enter these values:

 

{
    "debug": false,
    "delay": 0.3,
    "gutter_theme": "Circle",
    "highlights.time_to_idle": 1.5,
    "highlights.demote_while_editing": "none",
    "highlights.demote_scope": "",
    "lint_mode": "background",
    "linters": {
        "php": {
            "cmd": "/usr/bin/php",
            "@disable": false,
            "args": [],
            "excludes": [],
        },
        "phpcs": {
            "@disable": false,
            "args": [],
            "excludes": [],
            "standard": "Drupal,DrupalPractice"
        }
    },
    "no_column_highlights_line": false,
    "paths": {
        "linux": [],
        "osx": ["/Users/taskin/.composer/vendor/squizlabs/php_codesniffer/scripts"],
        "windows": []
    },
    "show_hover_line_report": true,
    "show_hover_region_report": true,
    "show_marks_in_minimap": true,
    "show_panel_on_save": "never",
    "statusbar.counters_template": "W: {warning} E: {error}",
    "statusbar.messages_template": "{message}",
    "styles": [
        {
            "mark_style": "outline",
            "priority": 1,
            "scope": "region.yellowish markup.changed.sublime_linter markup.warning.sublime_linter",
            "icon": "dot",
            "types": [
                "warning"
            ]
        },
        {
            "mark_style": "outline",
            "priority": 1,
            "scope": "region.redish markup.deleted.sublime_linter markup.error.sublime_linter",
            "icon": "dot",
            "types": [
                "error"
            ]
        }
    ],

    "syntax_map": {
        "html (django)": "html",
        "html (rails)": "html",
        "html 5": "html",
        "javascript (babel)": "javascript",
        "magicpython": "python",
        "php": "php",
        "python django": "python",
        "pythonimproved": "python"
    }
}

 

We need to enter lints which are php and phpcs also to automatically lint php document need to point phpcs scripts path. You need to point the path of php execuble and the coding standards name to lint accordingly:

"linters": {
        "php": {
            "cmd": "/usr/bin/php",
            "@disable": false,
            "args": [],
            "excludes": [],
        },
        "phpcs": {
            "@disable": false,
            "args": [],
            "excludes": [],
            "standard": "Drupal,DrupalPractice"
        }
    },

 

PHP Code Sniffer scripts is located at: 

~/.composer/vendor/squizlabs/php_codesniffer/scripts

We need to add this value to paths value accordingly to our Operating System:

"paths": {
        "linux": [],
        "osx": ["~/.composer/vendor/squizlabs/php_codesniffer/scripts"],
        "windows": []
    },

 

Now when we open php file SublimeLinter lints according to Drupal and DrupalPractice coding standards like this:

Sublime Text 3 SublimeLinter
Etiketler