Setting Up A PHP CodeSniffer For WordPress using Composer

A PHP CodeSniffer is a program that helps developers keep code clean and uniform or in sync across teams. In PHP, you can easily set up a code sniffer program to help lint and watch your files while you work and ensure that you keep to standards with everyone in the team.
Let’s get started.
Step 1. Install Composer
The first thing would be to ensure you have Composer installed on your computer. No point going forward if you don’t have that installed. You can install Composer on your computer using Homebrew like so:
brew install composer
Step 2. Install PHPCS
Next would be to install PHPCS (PHP CodeSniffer) globally on your Computer terminal using Composer like so:
composer global require squizlabs/php_codesniffer
Step 3. Expose PHPCS to system $PATH
To ensure you can access PHPCS globally or anywhere on your computer, you need to expose it to the PATH environment variable on your computer by adding the following line to the .zshrc or .bashrc file found on your root folder:
export PATH="$PATH:$HOME/.composer/vendor/squizlabs/php_codesniffer/bin"
If you’re not sure where to find your .zshrc or .bashrc file, you can type the following command on your terminal:
cd ~ && ls -al
It will take you to your root folder and display all the files in your root folder (including the hidden ones).
Step 4. Confirm PHPCS
Once the above step is completed, we need to ensure that the PHPCS is installed properly and accessible on your terminal, type in the following command:
phpcs -i
You should see something similar to this:
The installed coding standards are MySource, PEAR, PSR1, Squiz, Zend
Step 5. Install WP Coding Standard
Next, we install WP Coding standards as a global dependency. This will enable us get access to the WP coding standards package via composer like so:
composer global require wp-coding-standards/wpcs
Step 6. Clone PHP Compatibility Git Repo
We’ll also need to clone the PHP Compatibility Git repo into our .composer vendor bin folder like so:
cd ~/.composer/vendor && git clone https://github.com/wimg/PHPCompatibility.git
Step 7. Configure PHPCS Paths
Now that you have WP Coding Standards and PHP Comptability installed successfully, all that’s left is to configure the PHPCS paths like so:
phpcs --config-set installed_paths $HOME/.composer/vendor/PHPCompatibility, $HOME/.composer/vendor/wp-coding-standards/wpcs
Now run the following command on your terminal:
phpcs -i
You should see something similar to this:
The installed coding standards are MySource, PEAR, Zend, Squiz, PSR1, PHPCompatibility, WordPress-VIP, WordPress, WordPress-Extra, WordPress-Docs and WordPress-Core
Congratulations!!!
You’re now all set up to start using PHPCS to lint your PHP files in your WordPress projects.
How to use PHPCS
To use PHPCS in your WP plugin or theme repo, you have to ensure your WP plugin or theme repo has a phpcs.xml set up with WP rulesets defined.
Here’s an example of an Auttomatic’s phpcs.xml with WP rulesets defined:
https://github.com/Automattic/_s/blob/master/phpcs.xml.dist
Copy the code in the above XML file and save it into your own phpcs.xml file in the root path of your WP project. Now, you are ready to run your PHPCS and PHPCBF commands.
To sniff for errors in your WP project, run the following command on your terminal like so:
phpcs
To fix errors in your PHP files you can type the following command on your terminal like so:
phpcbf
Hope this helps in your WP projects. Please leave a comment below, thanks.