Skip to main content

Using vim and several different languages and need to switch between coding standards. 

1. Install requirements

sudo apt-get install vim

sudo apt-get install vim-syntastic

sudo apt-get install phpcs (older versions)

sudo apt-get install php-codesniffer

2) Get Drupal coding standards

cd /usr/local/src/

sudo mkdir coder && sudo chown $(whoami) coder && git clone git://git.drupal.org/project/coder.git && echo "Coder cloned OK" (older version)

sudo mkdir coder && sudo chown $(whoami) coder && git clone https://git.drupalcode.org/project/coder.git && echo "Coder cloned OK"

cd coder

composer update

3) Incorporate those standards into phpcs

#check to see list of known syntax 

phpcs -i

#add Drupal

cd /usr/share/php/PHP/CodeSniffer/Standards/

sudo ln -s /usr/local/src/coder/coder_sniffer/Drupal

Find where your CodeSniffer.conf file is stored

phpcs --config-show

It should show something like

Using config file: /etc/php-codesniffer/CodeSniffer.conf 

Or list no config file. If blank then to add more code_sniffer data and find where the config file is type

sudo phpcs --config-set installed_paths /usr/local/src/coder/coder_sniffer

Edit that file to look like this:

<?:php
 $phpCodeSnifferConfig = array (
  'default_standard' => 'Drupal',
  'php_path' => '/usr/bin/php',
  'installed_paths' => '/usr/local/src/coder/coder_sniffer',
)
?>

#check to see list of known syntax again

pcpcs -i

 

# check to make sure this is working with a particular file (say foo.php) that has known errors.

 

phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme foo.php

 

4) To use vim with specified standard

vim -c 'let g:syntastic_php_phpcs_args = "--standard=XXXX" '

where XXXX can be anything that showed up with phpcs -i

 

5) To make this permanent for .module files, edit .vimrc and add the following

" Syntastic
let g:syntastic_php_checkers=['php', 'phpcs']
if expand("%:e")=="module"
  let g:syntastic_php_phpcs_args = '--standard=Drupal --extensions=php,module,inc,install,test,profile,theme'
else
  let g:syntastic_php_phpcs_args = '--standard=PSR12 -n'
endif

 

Note: In prior versions of phpcs the "--extensions" flag was not required. Now it is.

Note: If you have phpmd you can change the first line to read
   let g:syntastic_php_checkers=['php', 'phpcs', 'phpmd']