How to use PHPCS and PHPCBF with a single WordPress file

PHPCS and PHPCBF are two well-know PHP scripts used in the PHP development to (PHPCS) detect violations of a defined coding standard, and (PHPCBF) to automatically correct coding standard violations. 

I usually use both with composer in my WordPress projects, adding them as dependency in the composer.json file.

But sometimes I need to check some individual PHP file without adding PHPCS and PHPCBF as dependency. In this post, I am going to explain how I do this.

I follow the instructions from the WordPress Coding Standards for PHP_CodeSniffer project to do a standalone installation.

I am going to install the PHP_CodeSniffer project and the WordPress Coding Standards for PHP_CodeSniffer in the ~/code/utils. To do this, I access the folder, and then I clone both projects:

cd ~/code/utils
git clone https://github.com/squizlabs/PHP_CodeSniffer.git phpcs
git clone -b master https://github.com/WordPress/WordPress-Coding-Standards.git wpcs

Finally, I add a new folder where PHPCS will look for the WordPress standards.

cd phpcs
./bin/phpcs --config-set installed_paths ../wpcs

Now you can see the code standards installed executing:

./bin/phpcs -i

The installed coding standards are MySource, PEAR, PSR1, PSR2, PSR12, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs and WordPress-Extra

I add these two alias to be able to execute in my local path. I add the “wp” prefix, so this alias doesn’t interfere with another PHPCS installed with composer:

alias wpphpcs="~/code/utils/phpcs/bin/phpcs --standard=WordPress "
alias wpphpcbf="~/code/utils/phpcs/bin/phpcbf --standard=WordPress "

Now, I access to the folder of the file I want to fix or check:

cd ~/code/wordpress/my-project

And then I can execute the PHPCBF or the PHPCS utility:

wpphpcbf class-stats.php
F 1 / 1 (100%)



PHPCBF RESULT SUMMARY
---------------------------------------------------------------------------------------------------------------------------------------------
FILE                                                                                                                         FIXED  REMAINING
---------------------------------------------------------------------------------------------------------------------------------------------
/Users/amieiro/code/wordpress/my-project/class-stats.php     207    47
---------------------------------------------------------------------------------------------------------------------------------------------
A TOTAL OF 207 ERRORS WERE FIXED IN 1 FILE
---------------------------------------------------------------------------------------------------------------------------------------------

To show the warnings and errors, I execute:

wpphpcs class-stats.php

And to show only the errors, I execute:

wpphpcs -n class-stats.php

5 comments

  1. Thanks for sharing this! I think there is a typo here: `alias wpphpcbf=”~/code/utils/phpcs/bin/phpcs “` should be phpcbf.

  2. Thank you for the feedback, Alex. I had updated the command in my .zshrc file, but not here. Updated!

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.