Setting up PHP_CodeSniffer & Auto Formatter for WordPress Development in PhpStorm

Increase your WordPress Code Quality automagically in PhpStrom.

Let’s configure PhpStrom for WordPress Development

1. Install Composer, PHP_CodeSniffer and WordPress Coding Standards for PHP_CodeSniffer

What is Composer?

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

What is PHP_CodeSniffer?

PHP_CodeSniffer is a set of two PHP scripts;

  • phpcs is a script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard.
  • phpcbf is a script to automatically correct coding standard violations.

Installation:

Let’s install both of theme.

  1. Installed Composer using the official doc: Introduction – Composer (getcomposer.org)
  2. Fire up your terminal and type these commands.
composer global require squizlabs/php_codesniffer wp-coding-standards/wpcs

Now add the installed WordPress coding standard to the phpcs default standards list by running this command on the terminal,

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

2. Configure WordPress Code Style in PhpStrom.

Navigate to PhpStrom -> Preferences -> Editor -> Code Style -> PHP and then click “Set from…” and from there, choose WordPress.

3. Configure PhpStrom for PHP_CodeSniffer code linting.

First type this command on your terminal,

which phpcs

and copy the path, for me it is in /Users/laranz/.composer/vendor/bin/phpcs

Navigate to PhpStrom -> Preferences -> Languages & Frameworks -> PHP -> Quality Tools -> PHP_CodeSniffer and in PHP_CodeSniffer path, paste the above path you got from terminal command and click Validate, if all works fine, it gives us a OK message. 👍🏽

PhpStrom - PHP Settings

One more step remaining to enable the code linting, Navigate to PhpStrom -> Preferences -> Editor -> Inspections -> PHP -> Quality tools -> PHP_CodeSniffer validation from there enable the validation and in the coding standard choose “WordPress”.

PhpStrom PHP_CodeSniffer validation

4. Configure PhpStrom for PHP_CodeSniffer code Formatting.

PHP_CodeSniffer comes with a handy script phpcbf to format our code using any of the installed Standards, in our case of course, the WordPress way.

For Visual Studio Code, we rely mostly on third-party plugins to do this, but for PhpStrom it has a build in plugin called “File Watchers“, in short “It is a PhpStorm system that tracks changes to your files and runs a third-party standalone application.”, in our case we need to run phpcbf on each save. To do so,

First type this command on your terminal,

which phpcbf

and copy the path, for me it is in /Users/laranz/.composer/vendor/bin/phpcbf

Navigate to PhpStrom -> Preferences -> Tools -> File Watchers and click the “+” icon, and we need to configure it like below.

  • Program – /Users/laranz/.composer/vendor/bin/phpcbf
  • Argument – --standard=WordPress $FilePath$
  • In Advance Settings – uncheck all options.

PhpStrom File Watcher

5. In Action.

Now when you type it’ll lint the code, and when you save it’ll auto format it to match the WordPress coding standards. Horray!!

PHPCodeSniffer - PhpStrom

Share your love
Joe

Joe

Your friendly neighbourhood WordPress guy!

Articles: 3

Leave a Reply

Your email address will not be published. Required fields are marked *