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 write clean and consistent code.
You can use PHP_CodeSniffer, which reviews your code against the coding standards, and receive warnings in case of any violations.
This section provides the steps for installing PHPCS globally on the machine.
PHPCS Installation
If you are not sure if you have 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,
composer global require "squizlabs/php_codesniffer=*"
If you have PHPCS already installed, you can run the following command to update it.
composer update squizlabs/php_codesniffer
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
To direct download, PHPCS you can run the following:
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar && chmod +x phpcs.phar && mv phpcs.phar /usr/local/bin/phpcs
To direct download, PHPCBF you can run the following:
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 the git repo as mentioned below.
cd ~/Documents
mkdir Coding-Standards && cd $_
git clone -b main https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs
git clone -b main 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, and 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
If installed directly
phpcs --config-show
Copy the 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 main
cd PHPCompatibility
git pull origin master
If installed via Composer
- Run the following command:
composer global update automattic/vipwpcs phpcompatibility/php-compatibility phpcompatibility/phpcompatibility-wp
Note: You should update WPCS every month or so or on every release.
Recommended Resources
- PHP_CodeSniffer on GitHub
- Installing and configuring PHP Code Sniffer in PhpStorm
- Installing and configuring PHP Code Sniffer in VSCode
- WordPress Coding Standards
Ideas to Explore:
- What are “sniffs” in PHP_CodeSniffer?
- Can PHP_CodeSniffer be integrated into CI/CD pipelines?
- Which file is commonly used to configure custom coding standards in PHPCS?
- How do you ignore specific error codes or standards in a file while using PHPCS?
- What is the name of the tool that automatically fixes some coding standard violations detected by PHPCS?
- Which PHP_CodeSniffer command can be used to list all available coding standards?
- How can you specify which coding standard to use when running PHPCS?