Skip to content

Instantly share code, notes, and snippets.

@GaryJones
Last active October 24, 2023 19:12
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save GaryJones/0604e6cd94eae756436740ad70f92ff8 to your computer and use it in GitHub Desktop.
Save GaryJones/0604e6cd94eae756436740ad70f92ff8 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.

@Zodiac1978
Copy link

@GaryJones Thank you very much for this tutorial! I just struggled on the last two steps. There is no menu entry "Tools -> SublimeLinter -> Open User Settings." for me (but there are other ways to get to the settings file), the quotes need to be double quotes and the phpcs settings needed to get capsuled in a linters block. This settings are working for me:

// SublimeLinter Settings - User
{
	"paths": {
	    "linux": [],
	    "osx": ["~/code/phpcs/scripts"],
	    "windows": []
	},
	"linters":{
		"phpcs": {
		    "@disable": false,
		    "args": [],
		    "excludes": [],
		    "standard": "WordPress"
		},
	}
}

@ArneGockeln
Copy link

Works as expected! Thank you very much for your tutorial!

@ian-pvd
Copy link

ian-pvd commented Dec 13, 2018

Thanks for this Gist. Been futzing with the config on this for ages and was always 90% there, but this helped me sort it out! 👍

@skorasaurus
Copy link

This was helpful but for me on ubuntu 16.04 + sublime 3 + phpcs 3.4 + WPCS 2.0 (Note that wpcs 2.0.0 only supports phpcs 3.3.1+ as of this writing)

specifying that standard on its own line as you and @**Zodiac1978 ** did not work for me; sublimelinter was still using the phpcs default standard even though I verified that my WordPress standards were loaded (phpcs -i)

Instead, I had to put the standard as an argument.
and
what did work for me was the following; i

"paths":
	{
	"linux": ["~/prg/php", "/usr/bin/"], 
	"osx": [], 
	"windows": []
	},
        "linters": {
            "phpcs": {
                "@disable": false,
                "args": ["--standard=Wordpress-Core"],
                "excludes": [
                    "*/*.html"
                ],
            },```

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