Installing Drupal Code Sniffer on Vim, Sublime Text, Visual Studio Code, Komodo, TextMate, Atom, Emacs & Geany

Last updated on
11 March 2021

This documentation needs work. See "Help improve this page" in the sidebar.

Directions how to integrate Drupal Code Sniffer into:

Vim

See this great, but slightly out-dated, blog post here.

The Vimrc project includes the needed Vim configuration: you will still have to install Drupal Code Sniffer and the Syntastic plugin for vim.

You'll probably want some git integration too.

Once those are installed, to get code standards review on every save, and details in your vim status line, just add this to your .vimrc:

let g:syntastic_php_phpcs_args="--standard=Drupal --extensions=php,module,inc,install,test,profile,theme"
if has('statusline')
  set laststatus=2
  " Broken down into easily includeable segments
  set statusline=%<%f\ " Filename
  set statusline+=%w%h%m%r " Options
  set statusline+=%{fugitive#statusline()} " Git Hotness
  set statusline+=\ [%{&ff}/%Y] " filetype
  set statusline+=\ [%{getcwd()}] " current dir
  set statusline+=%#warningmsg#
  set statusline+=%{SyntasticStatuslineFlag()}
  set statusline+=%*
  let g:syntastic_enable_signs=1
  set statusline+=%=%-14.(%l,%c%V%)\ %p% " Right aligned file nav info
endif

NeoVim

To get nice asynchronous phpcs working you can use the neomake plugin. Once you have that installed all you need to put in your init.vim is:

let g:neomake_php_phpcs_args_standard = 'Drupal'

If you want to run neomake every time you save a php file, you can do something like:

"run Neomake on php files on save
 function! RunAutoPHP()
   if &filetype == 'php'
     Neomake
   endif
 endfunction
 
 autocmd! BufWritePost * call RunAutoPHP()

Sublime Text

Sublime Text 3: https://github.com/mattwithoos/SublimeText3DrupalCoder
Sublime Text 2: (obsolete) https://github.com/sirkitree/DrupalCodingStandard

Komodo

See this blog post on this. http://blog.8bitplateau.net/drupa-code-sniffer-komodo/

TextMate

See: http://nuams.com/blog/installing-drupal-code-sniffer-textmate

Atom

Use the plugin Linter-phpcs and change the standard to "Drupal", or place a phpcs.xml in your project with the content:

<?xml version="1.0"?>
<ruleset name="drupal_project">
  <description>Make this project use the Drupal coding standard.</description>
  <rule ref="Drupal" />
</ruleset>

Visual Studio Code (vscode)

Install the extension for PHPCS. See https://marketplace.visualstudio.com/items?itemName=ikappas.phpcs

Note that this extension will only scan .inc, .php files. To get it to scan other Drupal files like .module, .install, .theme, .profile, .css and .js, see these issues on the github issue tracker #17 css and js files and #159 module files.

For a shortcut to code cleanup phpcbf, create a new task in /.vscode/tasks.json with something similar to:

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Format Drupal PHP",
      "type": "shell",
      "command": "phpcbf",
      "args": [
        "--standard=Drupal",
        "--ignore-annotations",
        "--extensions=inc,install,module,php,profile,theme",
        "--tab-width=2",
        "-vvv", // very verbose
        "${file}"
      ],
      "presentation": {
        "reveal": "always",
        "panel": "new"
      }
    }
  ]
}

You can then add a keyboard shortcut to this task. There is a PHPCBF extension, however, it doesn't work with Drupal standards because it isn't compatible with version 2.x of PHPCS, which is required (as opposed to version 3.x).

Emacs

Details on running phpcs from within Emacs: http://pear.php.net/manual/en/package.php.php-codesniffer.reporting.php (scroll down to the Printing an Emacs Report section, and be sure to replace the --standard=PEAR bit of the command with --standard=Drupal.
You can also integrate phpcs/drupalcs with Emacs' flymake-mode, see http://marmalade-repo.org/packages/flymake-phpcs.

Geany

Adapted from docs.joomla.org.

  • Open a PHP file
  • On the top menu, select Build > Set Build Commands.
  • Select the middle line in the PHP Commands section and name it “Codesniffer”
  • Enter this code in the Command:
    phpcs --extensions=php,module,inc,install,theme --standard=Drupal --report-width=999 "%f" | sed -e 's/^/%f |/' | egrep 'WARNING|ERROR'
  • Enter this code in the Error regular expression field:
    (.+) [|]\s+([0-9]+)
  • Select OK.
  • If the message window is not open at the bottom, display it by selecting it in the top View menu.
  • When viewing any PHP file, press F9 to run phpcs and see the errors found.

Help improve this page

Page status: Needs work

You can: