PHP_CodeSniffer (PHPCS)

Just like how WordPress follows a set of coding standards, PHP has its own coding standards. Since WordPress is primarily based on PHP, following these coding standards is a must if you want to develop reliable web applications.

You can use PHP_CodeSniffer which reviews your code against the coding standards and receive warnings incase of any violations.

PHPCS Installation

If you are not sure if you had installed PHPCS previously, you can check your system by running the which phpcs command. It should either return the path of the current PHPCS installation or it would return nothing – which states that you haven’t installed phpcs before.

You can find the version of PHPCS that you are working on by executing phpcs --version.

Composer Install/Update

Assuming you have PHP and Composer already installed,

Go to composer dir cd ~/.composer

composer global require "squizlabs/php_codesniffer=*"

If you have PHPCS already installed, you can run composer update squizlabs/php_codesniffer to update it.

And you can check the version by running ./vendor/bin/phpcs --version

Add ~/.composer/vendor/bin to $PATH in .bashrc or .zshrc and reload the terminal or open a new tab.

Direct Download

curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar && chmod +x phpcs.phar && mv phpcs.phar /usr/local/bin/phpcs

curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar && chmod +x phpcbf.phar && mv phpcbf.phar /usr/local/bin/phpcbf

You can also change /usr/local/bin/ to a custom path like ~/.bin and add that custom path to $PATH as mentioned above in the Composer Install/Update section.

Install WPCS and PHPCompatibility

Direct download

You can install WPCS and VIP Coding Standards via git repo as mentioned below.

cd ~/Documents
mkdir Coding-Standards && cd $_
git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs
git clone -b master https://github.com/Automattic/VIP-Coding-Standards.git
git clone -b master https://github.com/wimg/PHPCompatibility.git PHPCompatibility
ln -s ${PWD}/PHPCompatibility/PHPCompatibility wpcs/PHPCompatibility
ln -s ${PWD}/VIP-Coding-Standards/WordPressVIPMinimum wpcs/WordPressVIPMinimum
phpcs --config-set installed_paths ${PWD}/wpcs # remember this path

via composer

You can install it via composer using the following.

composer global require dealerdirect/phpcodesniffer-composer-installer \
	automattic/vipwpcs \
	phpcompatibility/php-compatibility \
	phpcompatibility/phpcompatibility-wp:* --update-no-dev

Note: You won’t need to set installed_paths when used with the composer as dealerdirect/phpcodesniffer-composer-installer will take care of it.

You should check out each GitHub repo for coding standards and see its purpose.

Check and Fix phpcs path

Try phpcs -i, it should show the coding standards and rule sets that are available, something like – WordPress, PHPCompatibility, WordPress-Extra, WordPress-Docs, WordPress-Core.

If these rulesets still didn’t show up, you need to execute the last command phpcs --config-set... again. (you should do this everywhere you update phpcs).

Update WPCS and PHPCompatibility

phpcs --config-show

Copy path given in installed_paths. If you don’t get that path check the above section on how to fix that.

cd {Paste here}
git pull origin master 
cd PHPCompatibility
git pull origin master

Note: You should update wpcs every month or so or on every release.

Recommended Resources