Skip to content

Instantly share code, notes, and snippets.

@anestan
Forked from GaryJones/notes.md
Created August 9, 2017 15:34
Show Gist options
  • Save anestan/91700b524d9d6373a94d4107ac629a0d to your computer and use it in GitHub Desktop.
Save anestan/91700b524d9d6373a94d4107ac629a0d to your computer and use it in GitHub Desktop.
Add PHPCS + WPCS to Sublime Text 3

Install PHP_CodeSniffer (PHPCS) via git

You can use the .phar for PHPCS, but it's easier to pull the repo down from git to then choose what version to use.

cd ~
mkdir -p code
cd code
git clone https://github.com/squizlabs/PHP_CodeSniffer.git phpcs

Use PHP_CodeSniffer 2.9

WPCS does not support PHPCS 3.0 as it is a complete rewrite (namespaces etc.), so use 2.9 branch for now.

git checkout 2.9.0

Add path/to/phpcs/scripts to $PATH

Tell Mac OS where it should look for executables. Add the following to the ~/.bash_profile (or equivalent for other shells) file (creating it if it doesn't exist):

export PATH=~/code/phpcs/scripts:$PATH

Check it is working by doing which phpcs, which should return the correct path. You may need to close and re-open the terminal.

Install WordPress Coding Standards (WPCS) via git

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

Add WordPress Coding Standard to PHP_CodeSniffer

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

Check it is installed with phpcs -i. You should see WordPress, WordPress-Core, WordPress-Extra, WordPress-Docs and WordPress-VIP amongst the built-in standards.

Install SublimeLinter package

SublimeLinter 3 is the framework that allows linter subpackages to work (think of it like PostCSS, which doesn't do anything on its own).

CMD-Shift-P -> Install Package -> SublimeLinter

Install sublimelinter-phpcs package

Now install the linter for PHPCS.

CMD-Shift-P -> Install Package -> sublimelinter-phpcs

(There are also sublimelinter-* packages for Sass, CSS, XML, JSHint etc.)

Add ~/code/phpcs/scripts to osx sublime-settings

We also need to tell Sublime Text where to look for the PHPCS executable.

Tools -> SublimeLinter -> Open User Settings.

"paths": {
    "linux": [],
    "osx": [
    	'~/code/phpcs/scripts'
    ],
    "windows": []
},

Change standard to WordPress

"phpcs": {
    "@disable": false,
    "args": [],
    "excludes": [],
    "standard": "WordPress"
},

Restart Sublime Text

Open a PHP file and look for red outlines for issues. Move cursor to there and check status bar for description. Turn on Tools -> SublimeLinter ->Debug Mode and View -> Show Console for more information. You can also change the settings so that errors are shown on save etc. See repo for what these mean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment