# how do I use phpcbf?

When I run civilint in the build kit I get a long report which ends with this line:

PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY 

Well that sounds great! Where is this mythical phpcbf and how do I use it?

When I ran it with just the name of the file it really broke things!!

The phpcs/phpcbf standard in use is a modified Drupal standard, so you call the standard of "Drupal":  phpcbf --standard="Drupal" filename.php  However, if you ALSO have unmodified copies of the Drupal standard installed, the trick is to make sure that you're calling the correct version of phpcbf. For instance, I get this:  mycomputer» which phpcbf /home/jon/local/civicrm-buildkit/bin/phpcbf  If your default phpcbf isn't the one installed with civicrm-buildkit, make sure to specify the full path to the one that is.

If that doesn't work: civilint is a script. So you can edit it in a text editor. In the latest version, line 179 reads:  xargs $BINDIR/phpcs --standard="$PHPCS_STD" < "$TMP/php.txt"  Add an "echo" into it like so:  xargs echo$BINDIR/phpcs --standard="$PHPCS_STD" < "$TMP/php.txt" 

Run civilint again:  civilint myfile.php  Instead of outputting the result of phpcs it should instead show the command it WOULD have run. In my case, it's:  /home/jon/local/civicrm-buildkit/bin/phpcs --standard=/home/jon/local/civicrm-buildkit/vendor/drupal/coder/coder_sniffer/Drupal fastactionlinks.php 

Change phpcs to phpcbf and run that command:  /home/jon/local/civicrm-buildkit/bin/phpcbf --standard=/home/jon/local/civicrm-buildkit/vendor/drupal/coder/coder_sniffer/Drupal fastactionlinks.php 

Note Don't forget to remove the echo when you're done!

• I get "ERROR: the "Drupal" coding standard is not installed. The installed coding standards are PHPCS, Zend, PSR1, PSR2, PEAR, MySource and Squiz" on buildkit vagrant which a civilint which definitely works. – JohnFF May 6 '17 at 15:48
• @JohnFF I edited my answer to give more details. Hopefully this fixes things for you. If you're still having trouble, please post details! – Jon G - Megaphone Tech May 6 '17 at 22:38
• Thanks Jon. Why not wrap it up for us? CiviCbf? "Patch Welcome!" as Xavi says... – JohnFF May 8 '17 at 0:09
• To be honest, I don't use it! My IDE (Netbeans) fixes the vast majority of these issues for me. I happened to use phpcbf once and only once the day before I saw this. I was curious how it could possibly auto-fix a particular issue. I don't imagine I'll use it again for Civi. – Jon G - Megaphone Tech May 10 '17 at 4:24

One use of phpcbf is as a way to automatically format (or'beautify' the code you write in your editor (providing your editor supports it).

For example, Atom supports phpcbf via the beautify package.

Buildkit provides a wrapper (or two) around phpcbf set up with appropriate defaults. You can find it in the civicrm-buildkit/bin directory. Specifying that your editor use this file will ensure that your code is 'beautified the CiviWay.

In my case, I set the phpcbf executable to be /home/michael/src/civicrm-buildkit/bin/phpcbf-civi in Atom's Settings > Packages > Atom Beautify > Executables.

fixing code automatically is hard, and can break things indeed.

I'm assuming you ran this one?

https://github.com/squizlabs/PHP_CodeSniffer/wiki/Fixing-Errors-Automatically

if you git diff the file you "fixed", what did you see?

• heya Xavier! I ran phpcs /path/to/file` and it made a huge mess of the file - it looks like it needs to point to a config file but I couldn't figure it out! – ErichBSchulz Mar 7 '17 at 23:48