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 to check your code against defined coding standards and get alerted if there are 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 return the path of the current PHPCS installation. If nothing was returned, then you probably haven’t installed phpcs before.

You can find the version of PHPCS you are running by running 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
pwd # copy this
ln -s {paste here}/PHPCompatibility/PHPCompatibility wpcs/PHPCompatibility
ln -s {paste here}/VIP-Coding-Standards/WordPressVIPMinimum wpcs/WordPressVIPMinimum
phpcs --config-set installed_paths {Paste here again}/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 WordPress, PHPCompatibility, WordPress-Extra, WordPress-Docs, WordPress-Core ruleset there.

If somehow it doesn’t show you, you need to exec 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

Fixing PHPCS Errors

Whitelisting code which flags errors

Scroll to Top