PHPCS configured for WPCS on MacOS using PHP 8.3

That’s a lot of esses.
While my daily driver IDE is PhpStorm, I also have VS Code configured for all my video lessons and workshops. I do this because VS Code is free, and can be configured to work similarly to PhpStorm, so it’s an easy one to suggest to new developers.
One of the VS Code extensions I have installed is the PHP Sniffer & Beautifier extension. This extension allows me to use PHP_CodeSniffer to auto-format my code to a specific coding standard.
The CLI tools that make this possible are phpcs and phpcbf, which come bundled with PHP_CodeSniffer.
Getting PHP_CodeSniffer working on the latest version of PHP (currently 8.3), required a few extra steps, so I thought I’d document them.
Installation
Install php-code-sniffer globally:
brew install php-code-sniffer
Clone the WPCS Github repo
git clone git@github.com:WordPress/WordPress-Coding-Standards.git wpcs
Clone the PHPCS Utils GitHub repo
git clone git@github.com:PHPCSStandards/PHPCSUtils.git phpcsutils
Clone the PHPCS Extra Github repo
git clone git@github.com:PHPCSStandards/PHPCSExtra.git phpcsextra
Configure the phpcs standards to include phpcsutils, phpcsextra and wpcs, using the paths of wherever the repos exist.
phpcs --config-set installed_paths /Users/jonathanbossenger/phpcsutils,/Users/jonathanbossenger/phpcsextra,/Users/jonathanbossenger/wpcs
To check that the standards are installed
phpcs -i
You should get something like the following output:
The installed coding standards are MySource, PEAR, PSR1, PSR2, PSR12, Squiz, Zend, PHPCSUtils, Modernize, NormalizedArrays, Universal, WordPress, WordPress-Core, WordPress-Docs and WordPress-Extra
And you are good to go.
Something worth remembering is that you might need to update the various git repos every now and then, otherwise phpcs might stop working.
Leave a Reply to Baki GoxhajCancel reply