This document covers how to setup PHP Code Sniffer to sniff WordPress coding standards in VS Code for Windows and Mac OS.
- Visual Studio Code (Prerequisite)
- PHP
- Composer
- Open Terminal and move to directory where composer is installed i.e.
C:\Users\username\AppData\Roaming\Composer. - Install php_codesniffer package by running
composer require squizlabs/php_codesnifferon your composer directory. - Add
C:\Users\username\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\binto yourenvironment variables PATH. - Once install, verify the installation by running
phpcs --versionon the terminal.
- Install wpcs package by running
composer require wp-coding-standards/wpcson your composer directory.
- Move to the
vendordirectory in your composer directory and clone PHPCompatibility package by runninggit clone https://github.com/wimg/PHPCompatibility.git.
- Move to the
composerdirectory and link wpcs and phpcompatibility standards with phpcs by runningphpcs --config-set installed_paths C:\Users\username\AppData\Roaming\Composer\vendor\PHPCompatibility,C:\Users\username\AppData\Roaming\Composer\vendor\wp-coding-standards\wpcs. - Verify linking by running
phpcs -i.
- Open
settings.jsonand add following lines to it:"phpcs.enable": true, "phpcs.executablePath": "C:\\Users\\username\\AppData\\Roaming\\Composer\\vendor\\bin\\phpcs.bat", "phpcs.standard": "WordPress", "phpcbf.enable": true, "phpcbf.documentFormattingProvider": true, "phpcbf.onsave": true, "phpcbf.executablePath": "C:\\Users\\username\\AppData\\Roaming\\Composer\\vendor\\bin\\phpcbf.bat", "phpcbf.standard": "WordPress", "[php]": { "editor.defaultFormatter": "persoderlind.vscode-phpcbf" }
- Visual Studio Code (Prerequisite)
- PHP
- Composer
- Install phpcs globally
composer global require "squizlabs/php_codesniffer=*"
- Install wpcs package by github
git clone -b master https://github.com/WordPress/WordPress-Coding-Standards.git wpcs
- Move to the
composerdirectory and link wpcs and phpcompatibility standards with phpcs by running
/path/to/composer/vendor/bin/phpcs --config-set installed_paths /path/to/WPCS, /path/to/another-standards
Note:
-
In the place of
/path/to/composer/vendor/bin/phpcsadd the path ofcomposerlocated in your system. -
In the place of
/path/to/WPCSadd the path wherewpcsis stored.
- Verify linking by running
/path/to/composer/vendor/bin/phpcs -i
OUTPUT:
The installed coding standards are PEAR, Zend, PSR2, MySource, Squiz, PSR1, PSR12, WordPress, WordPress-Extra, WordPress-Docs and WordPress-Core.
- Open
settings.jsonand add following lines to it:"phpcs.enable": true, "phpcs.executablePath": "/path/to/composer/vendor/bin/phpcs", "phpcs.standard": "WordPress" "phpcbf.enable": true, "phpcbf.documentFormattingProvider": true, "phpcbf.onsave": true, "phpcbf.executablePath": "/path/to/composer/vendor/bin/phpcbf", "phpcbf.standard": "WordPress", "[php]": { "editor.defaultFormatter": "persoderlind.vscode-phpcbf" } - Restart the VS Code.