sponsor Vim development Vim logo Vim Book Ad

phpcs.vim : PHP coding rules checking utility

 script karma  Rating 6/3, Downloaded by 1272  Comments, bugs, improvements  Vim wiki

created by
Kai ZHANG
 
script type
utility
 
description
This plugin relies on PHP_CodeSniffer package.It parse the result of phpcs and redirect it into vim's quickfix buffer.
So users can navigate the codding rule error place use ':cnext' and ':cprev'.
PHP_CodeSniffer is a utility developed for checking codding rules for PHP scripts.
For more information about PHP_CodeSniffer, see http://pear.php.net/package/PHP_CodeSniffer/

To use this plugin, you must have these global variables set:
1. The list of coding rule standards to follow, with each one seperated by comma.                                                        
     let g:phpcs_std_list="Zend, PEAR"

2. Your VCS type(Only needed when running 'Phpcs commit')
     let g:phpcs_vcs_type = 'svn'

     Currently only support 'svn' or  'cvs'.

3. Set the max allowed output lines.
     This is useful in case there are too many errors and memory is running out.
    
      let g:phpcs_max_output = 0 " Unlimited output.
      or
      let g:phpcs_max_output = 2000 " Output limited to 2000 line

Following command are provided:

:Phpcs          " Check the current file, if not in DIFF mode.
                       "  Check the modified lines only, if in DIFF mode. And in DIFF mode, call  :Phpcs twice, it will switch back and update the DIFF view.
:Phpcs commit   " Checking PHP  files to be commited in the directory recursively
:Phpcs all           " Checking PHP files in the current directory recursively

You can map shortcuts for your convenience. For example, in my working environment, we do following mappings:
" input mode map
inoremap <F5>  <ESC>:Phpcs<CR>
inoremap <F7> <ESC>:cprev<CR>
inoremap <F8> <ESC>:cnext<CR>

"normal mode map
noremap <F5>  <ESC>:Phpcs<CR>
noremap <F7> <Esc>:cprev<CR>
noremap <F8> <ESC>:cnext<CR>

Note for the Phpcs in DIFF mode:
1. Why support Phpcs in DIFF mode?
Due to historical reasons, there exist a lot of old codes not following the new coding standards. Our policy is that, for the new codes added, must following the coding standards; but keep the old codes as it is.
So we only need to check the new codes to be committed.

2. Which scenario does this mode work?
In this scenario, we use vimdiff for the extra diff command of svn diff. Before we commit the codes, we run svn diff, which in turn will call vimdiff to show what has been changed in codes.
Here we call :Phpcs command, to do the checking, and that will only output errors/warning in the lines modified in this commit.

3. The Phpcs in DIFF mode is not flexible!
You should make sure that:  ONLY TWO WINDOWS EXIST IN DIFF VIEW, THE 1ST ONE IS THE ORIGINAL FILE, THE 2ND ONE IS THE MODIFIED FILE.
If you use other plugins and they open windows automatically in vimdiff mode, you should close it first manually or add codes to do this in phpcs.vim.
In the 1.1 version, I added codes to detect MiniBufExpl plugin, and close it automatically. Just for your reference.
 
install details
Put phpcs.vim at ~/.vim/plugin/phpcs.vim.
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
phpcs.vim 1.1 2012-03-26 7.0 Kai ZHANG Fixed the issue that when minibufexpl plugin enabled, the window operation is totally wrong in DIFF mode.
phpcs.vim 1.0 2012-02-14 7.0 Kai ZHANG Initial upload.
ip used for rating: 3.19.31.73

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github