PHPCS configured for WPCS on MacOS using PHP 8.3

Antique tools on a shelf

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.


3 responses to “PHPCS configured for WPCS on MacOS using PHP 8.3”

  1. Baki Goxhaj Avatar

    It is a lot more dev-friendly to do this through composer.

  2. Baki Avatar

    It’s a lot more dev-friendly to do this through composer, either global or per project.

    1. Jonathan Avatar

      Yup, if you’re already using composer that’s definitely another way to tackle the problem. I tend to prefer to not install software globally via composer, so this is my preferred way.

Leave a Reply to JonathanCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.