5

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!!

3 Answers 3

2

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!

4
  • 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, 2017 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! May 6, 2017 at 22:38
  • Thanks Jon. Why not wrap it up for us? CiviCbf? "Patch Welcome!" as Xavi says...
    – JohnFF
    May 8, 2017 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. May 10, 2017 at 4:24
1

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.

0

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?

1
  • 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! Mar 7, 2017 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.