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
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
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.
git clone https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs
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.
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
Now install the linter for PHPCS.
CMD-Shift-P -> Install Package -> sublimelinter-phpcs
(There are also sublimelinter-*
packages for Sass, CSS, XML, JSHint etc.)
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": []
},
"phpcs": {
"@disable": false,
"args": [],
"excludes": [],
"standard": "WordPress"
},
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.
@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: