Installing phpcs on a Mac ft. VSCode & WordPress, The Really Simple Guide

1. Install Composer

There are different ways to install composer, I find it easier to simply use brew. (if you don’t have brew installed, you can follow the docs here).

Once brew is installed you can just run:

brew install composer

Now check if is installed by running:

composer --version

You should see something like this:

Composer version 1.10.6 2020-05-06 10:28:10

Done!.

2. Install phpcs

Let’s use composer to install phpcs by running this command:

composer global require squizlabs/php_codesniffer

After the installation is done, you might wanna check if phpcs got installed by running:

which phpcs

This should return its path, something like:

/Users/username/.composer/vendor/bin/phpcs

I use zsh and got:

zsh: command not found ...

That just means that you need to manually add ~/.composer/vendor/bin/ to your PATH.

You can do that by just editing your ~/.zshrc file, and adding the path at the end of the file, something like this:

export PATH=~/.composer/vendor/bin:$PATH

That should fix it, once you restart your terminal or source it with the updated file, you should see something like this after typing which phpcs :

which phpcs

/Users/username/.composer/vendor/bin/phpcs

Done.

3. Installing WordPress Coding Standards Rule Set

Create a directory where you wanna save these rules, let’s call it rules (you can call it whatever you want), you can place it anywhere you prefer, for this example, I will create it in my root directory:

mkdir rules

Then cd into it:

cd rules

Now following the WP Coding Standards Repo instructions we can use git to clone the rule set by typing (you should have git installed to run this command):

git clone -b master https://github.com/WordPress/WordPress-Coding-Standards.git wpcs

After this, you should have a wpcs folder in your rules directory.

Now add its path to the PHP_CodeSniffer configuration by running:

phpcs --config-set installed_paths ~/rules/wpcs

You should see something like this:

Using config file: /Users/adrian/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer.conf

Config value "installed_paths" added successfully

Then you can run:

phpcs -i

To see the installed code standards, and the WordPress ones should be at the end, like this:

The installed coding standards are PEAR, Zend, PSR2, MySource, Squiz, PSR1, PSR12, WordPress, WordPress-Extra, WordPress-Docs and WordPress-Core

Done!

4. Install phpcs VSCode Extension

Installation instructions here.

It may ask for the executable path to be added to your vscode settings.json, just add the one you get after typing which phpcs in terminal, like so:

which phpcs

/Users/username/.composer/vendor/bin/phpcs

Then in your settings.json just add:

{
  //...,
  "phpcs.executablePath": "/Users/username/.composer/vendor/bin/phpcs"
}

Done!

That’s it.

You may also like