After doing a few months in React land where the code tooling is good and Vim isn't seen as the insane tool of a dilettante fool I got really used to being able to use to auto format code. Took a bit of setting up but once it was working all that tedious whitespace was sorted at a key stroke. Along with Deoplete, Tern JS and Ultisnips it made for a very pleasant coding experience with auto complete with some introspection from TernJS and whatever React snippet I wanted add to Ultisnips (the pace of change is so fast with React that 3rd party snippets were always out of date). But that is another story.

I wanted a similar experience and after some experimentation I found there was nothing suitible - I tried php-cs-fixer via Vim-php-cs-fixer but it only works on saved files (php-cs-fixer doesn't do standard out, and is intolerant of Drupal Coding Standards - specifically it only allows lower case constants by default), and I already was using phpcbf a lot to fix in buffer issues. If you install Coder module it installs Drupal Coding Standards code sniffs.

To get that working in Vim isn't too difficult - you will need to have set the PHP filetype:

  autocmd BufNewFile,BufRead *.{module,install,drush,theme,view,profile} set filetype=php.drupal
  autocmd BufNewFile,BufRead *.info.yml setlocal filetype=yaml.drupal
  autocmd BufRead,BufNewFile *.{test,inc} set filetype=php

Then in my ftplugin/php.vim file I have:

" PHP prettify.
function! PHP_prettify()
  let save_pos = getpos(".")
  execute('silent %! phpcbf -q --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md')
  call setpos('.', save_pos)

:command! P call PHP_prettify()

Which means phpcbf formatted code is never more than a couple of key presses away.

There is an example of it working in my vimrc