WSL and WPCS in PHPStorm

To install the WordPress Coding Standards (WPCS) for PHP_CodeSniffer in PhpStorm when using Windows Subsystem for Linux (WSL), you’ll essentially be working through WSL for most of the setup. This approach ensures that the standards are applied correctly within your WSL environment, where your PHP and PHP_CodeSniffer are installed. Here’s a step-by-step guide:

1. Open WSL Terminal

Launch your WSL terminal from Windows. You can do this by searching for “WSL” in the start menu or by opening a command prompt and typing wsl.

2. Install PHP_CodeSniffer

If you haven’t installed PHP_CodeSniffer (PHPCS) yet, you can do so using Composer. If you don’t have Composer installed, you’ll need to install it first.

Install Composer:

curl -sS | php
sudo mv composer.phar /usr/local/bin/composer

Install PHP_CodeSniffer:

composer global require "squizlabs/php_codesniffer=*"

Make sure the ~/.composer/vendor/bin directory is in your PATH. You can ensure this by adding the following line to your .bashrc.zshrc, or equivalent shell configuration file:

export PATH="$PATH:$HOME/.composer/vendor/bin"

Then, source your profile to apply the changes:

source ~/.bashrc

(or use .zshrc or the relevant file for your shell).

3. Install WordPress Coding Standards

Step 1: Install WPCS globally via Composer:

composer global require wp-coding-standards/wpcs

Step 2: After installation, you need to configure PHP_CodeSniffer to use the WPCS by adding its standard directory to PHP_CodeSniffer’s configuration.

You can do this by running:

phpcs --config-set installed_paths ~/.composer/vendor/wp-coding-standards/wpcs

Replace ~/.composer/vendor/wp-coding-standards/wpcs with the correct path if your global Composer directory is different.

4. Configuring PhpStorm to Use WPCS through WSL

Step 1: Open PhpStorm and go to File > Settings (or PhpStorm > Preferences on macOS).

Step 2: Navigate to Languages & Frameworks > PHP > Quality Tools.

Step 3: Under PHP_CodeSniffer, click on the ... next to Configuration: to set the path. Since you’re using WSL, you’ll want to specify the path to the PHPCS executable within your WSL environment, e.g., /home/yourusername/.composer/vendor/bin/phpcs.

Step 4: In the same settings area, you can set the coding standard to WordPress by selecting it from the list of available standards in the PHP_CodeSniffer validation section.

If WPCS does not appear in the list, you might need to refresh the standards by clicking the refresh button next to the list.

5. Applying WPCS to Your Project

With WPCS installed and configured in PhpStorm, you can now use it to check your WordPress projects against these standards. You can run inspections manually or configure PhpStorm to highlight issues in real-time as you code.

Remember, since you’re working through WSL, your projects should ideally be located within the WSL filesystem for compatibility and performance reasons. You can access your WSL files from Windows File Explorer using \\wsl$\\ (replace “ with the name of your Linux distribution, such as Ubuntu).

This setup ensures that you’re using WPCS within the context of your WSL environment, leveraging the full power of PhpStorm’s integration capabilities.