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 you 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.

Recommended Resources

Fixing PHPCS Errors

Whitelisting code which flags errors