|
1 | | -# phpcs-wpcs-vscode |
2 | | -How to install PHP CodeSniffer and the WordPress Coding Standard Rules in Visual Studio Code. |
| 1 | +# PHP CodeSniffer and WordPress Coding Standards with VS Code |
| 2 | + |
| 3 | +_Last Updated 2016-11-17 by @tommcfarlin_ |
| 4 | + |
| 5 | +This guide is meant to provide all of the steps necessary to easily get up and running with PHP CodeSniffer, the WordPress Coding Standard ruleset, and Visual Studio Code. |
| 6 | + |
| 7 | +[!Visual Studio Code](http://d.pr/i/SsDU+) |
| 8 | + |
| 9 | +All of the resources used in this guide are linked at the bottom. This guide is also licensed [MIT](https://github.com/tommcfarlin/phpcs-wpcs-vscode/blob/master/LICENSE). If you'd like to contribute, then please feel free to open issues or issue pull requests. I'll be happy to merge them and also add your username to [CONTRIBUTING](https://github.com/tommcfarlin/phpcs-wpcs-vscode/blob/master/CONTRIBUTING.md). |
| 10 | + |
| 11 | +If you're looking for a corresponding blog post, please see [this page](https://tommcfarlin.com/php-codesniffer-in-visual-studio-code). |
| 12 | + |
| 13 | +As always, don't forget to checkout the [CHANGELOG](https://github.com/tommcfarlin/phpcs-wpcs-vscode/blob/master/CHANGELOG.md) to track everything that's changed since the initial release of this guide. |
| 14 | + |
| 15 | +______ |
| 16 | + |
| 17 | +## 1. Verifying PHP |
| 18 | + |
| 19 | +The following steps assume you have PHP installed and globally accessible on your system. |
| 20 | +You can test this by entering the following command in the terminal: |
| 21 | + |
| 22 | +``` |
| 23 | +$ php -v |
| 24 | +``` |
| 25 | + |
| 26 | +And you should see something like this: |
| 27 | + |
| 28 | +``` |
| 29 | +PHP 5.6.25 (cli) (built: Sep 6 2016 16:37:16) |
| 30 | +Copyright (c) 1997-2016 The PHP Group |
| 31 | +Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies |
| 32 | +``` |
| 33 | + |
| 34 | +If you're looking for how to use a different version of PHP installed elsewhere on your system, |
| 35 | +this is not the guide for that. If, however, you're curious as to _where_ the version of PHP you're |
| 36 | +using is stored, you can enter: |
| 37 | + |
| 38 | +``` |
| 39 | +$ which php |
| 40 | +``` |
| 41 | + |
| 42 | +And you should see something similar to this: |
| 43 | + |
| 44 | +``` |
| 45 | +/usr/bin/php |
| 46 | +``` |
| 47 | + |
| 48 | +That should give you enough information for the rest of this guide. |
| 49 | + |
| 50 | +## 2. Installing Composer |
| 51 | + |
| 52 | +Installing Composer globally means that you'll be able to access it from anywhere on your system (that is, in any directory regardless of where you are). To do this, you can read the [manual](https://getcomposer.org/doc/00-intro.md) or follow the quick steps below (which summarize the |
| 53 | +manual, anyway): |
| 54 | + |
| 55 | +1. Grab [the latest snapshot](https://getcomposer.org/composer.phar) of Composer. Save it somewhere you'll remember. |
| 56 | +2. Move the file you just downloaded to the `/usr/local/bin/` directory on your machine. |
| 57 | + |
| 58 | +To do this, open your terminal navigate to the directory where you downloaded `composer.phar`. Move the file to the diretory mentioned above by issuing the following command: |
| 59 | + |
| 60 | +``` |
| 61 | +$ mv composer.phar /usr/local/bin/composer |
| 62 | +``` |
| 63 | + |
| 64 | +And now you can access Composer from anywhere in your system. To do try it out, enter the following command in your terminal: |
| 65 | + |
| 66 | +``` |
| 67 | +$ composer about |
| 68 | +``` |
| 69 | + |
| 70 | +You should see something like this: |
| 71 | + |
| 72 | +``` |
| 73 | +Composer - Package Management for PHP |
| 74 | +Composer is a dependency manager tracking local dependencies of your projects and libraries. |
| 75 | +See https://getcomposer.org/ for more information. |
| 76 | +``` |
| 77 | + |
| 78 | +With Composer globally installed, you can now install the WordPress Coding Standards rules. |
| 79 | + |
| 80 | +## 3. Installing PHP CodeSniffer |
| 81 | + |
| 82 | +For the purposes of this document, we're installing PHP CodeSniffer on a project-by-project basis. To do this, we're going to be using Composer. |
| 83 | + |
| 84 | +From the integrated terminal within Visual Studio Code, enter the following command: |
| 85 | + |
| 86 | +``` |
| 87 | +$ composer require "squizlabs/php_codesniffer=*" |
| 88 | +``` |
| 89 | + |
| 90 | +This will create `composer.json`, tell it where to locate the PHP CodeSniffer, and install it in a `vendor` directory. Once this is done, we need the WordPress Coding Standard ruleset. |
| 91 | + |
| 92 | +## 4. Installing the WordPress Coding Standards Rules |
| 93 | + |
| 94 | +I recommend placing the rules in a directory you can refer to often. Personally, I use a `projects` directory in my Dropbox directory to manage all of my work because it obviously provides backups automatically (and no, I don't recommend storing secure files there). |
| 95 | + |
| 96 | +From within your directory of choice, say `dropbox/projects`, enter the following command in the terminal: |
| 97 | + |
| 98 | +``` |
| 99 | +$ composer create-project wp-coding-standards/wpcs:dev-master --no-dev |
| 100 | +``` |
| 101 | + |
| 102 | +This will create a `wpcs` directory in your `projects` directory and it makes it esay to tell each project where the WordPress Coding Standards are stored because, remember, we'll be using Composer on a project-by-project basis. |
| 103 | + |
| 104 | +## 5. Tell PHPCS About WPCS |
| 105 | + |
| 106 | +From within Visual Studio's integrated terminal, make sure that you're in your project's directory and then issue the following command: |
| 107 | + |
| 108 | +``` |
| 109 | +$ ./vendor/bin/phpcs --config-set installed_paths /path/to/dropbox/projects/wpcs |
| 110 | +``` |
| 111 | + |
| 112 | +And this will tell your project's copy of PHPCS where the WordPress Coding Standards are. |
| 113 | + |
| 114 | +## 6. Update User Settings |
| 115 | + |
| 116 | +Finally, we need to let Visual Studio what we're going to be using to sniff out the code in our project and what rules to use. This is really easy to do. In Visual Studio, hit the `⌘,` (or whatever your operating system uses) command to open `settings.json`. |
| 117 | + |
| 118 | +Make sure the file looks like the following (though you may need to tweak based on your existing settings) |
| 119 | + |
| 120 | +```json |
| 121 | +// Place your settings in this file to overwrite the default settings |
| 122 | +{ |
| 123 | + // PHPCS |
| 124 | + "phpcs.enable": true, |
| 125 | + "phpcs.standard": "WordPress", |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +And this will enable PHPCS and will also tell it to use the standard WordPress ruleset. If this doesn't start working on the code your have automatically, then restart Visual Studio Code. |
| 130 | +___ |
| 131 | + |
| 132 | +## Resources |
| 133 | + |
| 134 | +- [Visual Studio Code](https://code.visualstudio.com/) |
| 135 | +- [Getting Started](https://getcomposer.org/doc/00-intro.md) |
| 136 | +- [The Latest Composer Snapshot](https://getcomposer.org/composer.phar) |
| 137 | +- [PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) |
| 138 | +- [WordPress Coding Standards](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards) |
| 139 | +- [My corresponding blog post](https://tommcfarlin.com/php-codesniffer-in-visual-studio-code) |
0 commit comments