├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE └── README.md /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | This project adheres to [Semantic Versioning](http://semver.org/) and to 5 | [Keep a Changelog](http://keepachangelog.com). 6 | 7 | ## 1.2.0 - 2020-07-31 8 | 9 | ### Update 10 | 11 | - `CONTRIBUTING.md` with contributors 12 | - `README.md` with new links, versions of software, and instructions 13 | 14 | ## 1.1.0 - 2016-11-17 15 | 16 | ### Fixed 17 | 18 | - Visual Studio Code screenshot not properly appearing in the README 19 | 20 | ## 1.0.0 - 2016-11-17 21 | 22 | ### Added 23 | 24 | - Adds the README, LICENSE, CHANGELOG, CONTRIBUTING, and LICENSE 25 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | - [Jeremy Smith](https://github.com/jeremysawesome) 2 | - [Evan Mullins](https://github.com/circlecube) 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Tom McFarlin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP CodeSniffer and WordPress Coding Standards with VS Code 2 | 3 | _Last Updated 2020-07-31 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](https://tommcfarlin.com/wp-content/uploads/2020/07/visual-studio-code-974x1024.png) 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 corresponding blog posts, please see: 12 | 1. [Setting Up PHP CodeSniffer in Visual Studio Code](https://tommcfarlin.com/php-codesniffer-in-visual-studio-code) 13 | 2. [Setting Up PHP CodeSniffer Per Project](https://tommcfarlin.com/php-codesniffer-per-project/) 14 | 15 | 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. 16 | 17 | ______ 18 | 19 | ## 1. Verifying PHP 20 | 21 | The following steps assume you have PHP installed and globally accessible on your system. 22 | You can test this by entering the following command in the terminal: 23 | 24 | ``` 25 | $ php -v 26 | ``` 27 | 28 | And you should see something like this: 29 | 30 | ``` 31 | PHP 7.4.7 (cli) (built: Jun 12 2020 00:04:10) ( NTS ) 32 | Copyright (c) The PHP Group 33 | Zend Engine v3.4.0, Copyright (c) Zend Technologies 34 | with Xdebug v2.9.6, Copyright (c) 2002-2020, by Derick Rethans 35 | with Zend OPcache v7.4.7, Copyright (c), by Zend Technologies 36 | ``` 37 | 38 | If you're looking for how to use a different version of PHP installed elsewhere on your system, this is not the guide for that. If, however, you're curious as to _where_ the version of PHP you're using is stored, you can enter: 39 | 40 | ``` 41 | $ which php 42 | ``` 43 | 44 | And you should see something similar to this: 45 | 46 | ``` 47 | /usr/local/bin/php 48 | ``` 49 | 50 | That should give you enough information for the rest of this guide. 51 | 52 | ## 2. Installing Composer 53 | 54 | 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 manual, anyway): 55 | 56 | 1. Grab [the latest snapshot](https://getcomposer.org/composer.phar) of Composer. Save it somewhere you'll remember. 57 | 2. Move the file you just downloaded to the `/usr/local/bin/` directory on your machine. 58 | 59 | 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: 60 | 61 | ``` 62 | $ mv composer.phar /usr/local/bin/composer 63 | ``` 64 | 65 | And now you can access Composer from anywhere in your system. To do try it out, enter the following command in your terminal: 66 | 67 | ``` 68 | $ composer about 69 | ``` 70 | 71 | You should see something like this: 72 | 73 | ``` 74 | Composer - Package Management for PHP 75 | Composer is a dependency manager tracking local dependencies of your projects and libraries. 76 | See https://getcomposer.org/ for more information. 77 | ``` 78 | 79 | With Composer globally installed, you can now install the WordPress Coding Standards rules. 80 | 81 | ## 3. Installing PHP CodeSniffer 82 | 83 | 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. 84 | 85 | From the integrated terminal within Visual Studio Code, enter the following command: 86 | 87 | ``` 88 | $ composer require "squizlabs/php_codesniffer=3.*" 89 | ``` 90 | 91 | 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. 92 | 93 | ## 4. Installing the WordPress Coding Standards Rules 94 | 95 | I recommend placing the rules in a directory you can refer to often. Personally, I use a `projects` directory to manage all of my work. 96 | 97 | From within your directory of choice, say `/projects`, enter the following command in the terminal: 98 | 99 | ``` 100 | $ composer create-project wp-coding-standards/wpcs:dev-master --no-dev 101 | ``` 102 | 103 | This will create a `wpcs` directory in your `projects` directory and it makes it easy to tell each project where the WordPress Coding Standards are stored because, remember, we'll be using Composer on a project-by-project basis. 104 | 105 | ## 5. Tell PHPCS About WPCS 106 | 107 | From within Visual Studio's integrated terminal, make sure that you're in your project's directory and then issue the following command: 108 | 109 | ``` 110 | $ ./vendor/bin/phpcs --config-set installed_paths /path/to/dropbox/projects/wpcs 111 | ``` 112 | 113 | And this will tell your project's copy of PHPCS where the WordPress Coding Standards are. 114 | 115 | ## 6. Update User Settings 116 | 117 | 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`. 118 | 119 | Make sure the file looks like the following (though you may need to tweak based on your existing settings) 120 | 121 | ```json 122 | // Place your settings in this file to overwrite the default settings 123 | { 124 | // PHPCS 125 | "phpcs.enable": true, 126 | "phpcs.standard": "WordPress", 127 | "phpcs.executablePath": "./vendor/bin/phpcs", 128 | "phpcs.showWarnings": true, 129 | "phpcs.composerJsonPath": "composer.json", 130 | } 131 | ``` 132 | 133 | 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. 134 | ___ 135 | 136 | ## Resources 137 | 138 | - [Visual Studio Code](https://code.visualstudio.com/) 139 | - [Getting Started](https://getcomposer.org/doc/00-intro.md) 140 | - [The Latest Composer Snapshot](https://getcomposer.org/composer.phar) 141 | - [PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) 142 | - [WordPress Coding Standards](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards) 143 | - [Setting Up PHP CodeSniffer in Visual Studio Code](https://tommcfarlin.com/php-codesniffer-in-visual-studio-code) 144 | - [Setting Up PHP CodeSniffer Per Project](https://tommcfarlin.com/php-codesniffer-per-project/) 145 | --------------------------------------------------------------------------------