Run PHP CodeSniffer code beautifier and fixer from PhpStorm


In this tutorial i’m going to show you how you can run the PHP Code Beautifier and Fixer, or phpcf for short, from inside PhpStorm.

I am going to assume you already have Composer, PHP CodeSniffer installed and downloaded the WordPress Coding Standards rules/sniffs.

Having that all set, we need to tell PhpStorm where the CodeSniffer is installed. Go to Settings -> Tools -> External Tools. There we will create the configuration needed.

Click on the + icon. Fill the form with the following values:

single-file

single-file

  • Name: the name you want to call it. This will be used to invoke the script through PhpStorm.
  • Description: A short description of what the script does.
  • Program: the path to phpcbf.
  • Parameters: a list of parameters, will explain those later.
  • Working directory: the variable $ProjectFileDir$ is used here.

Personally i created 3 sets of configurations. One to fix a single file, other to fix all php files in the current directory non-recursively, and another one to fix all php files in the current folder recursively. So let me show you the other 2 configs:

current folder

current folder

all folders

all folders

You can find a list of all parameters simply running phpcbf -h or you can go to PHP CodeSniffer documentation page. Here i’ll list the ones used in the examples above:

  • –standard: The name or path of the coding standard to use.
  • –extensions: A comma separated list of file extensions to fix (only valid if fixing a directory).
  • -l: Local directory only, no recursion

And the variables:

  • $FileDir$/$FileName$: parses the current file only.
  • $FileDir$: parses the whole directory.

Here is the phpcbf in action:

Console

Console

After everything is setup, you can even assign keyboard shortcuts to run those scripts. Go to Settings -> Keymap:

keymap

keymap

Search for the ‘Name’ you inserted when created the link to the external tool phpcbf, click on the green pencil icon, and input the keyboard shortcut you want:

keyboard shortcut

keyboard shortcut

I hope this tutorial help you be more productive and make sure the code meet the WordPress Standards. Here’s some useful links:

 

Advertisements

Web Engineer at 10up

Posted in PhpStorm, WordPress
3 comments on “Run PHP CodeSniffer code beautifier and fixer from PhpStorm
  1. boerl says:

    Thx for the tutorial.

    You could make one configuration which works on both a single file and a directory. By using the $FilePath$ variable instead of $FileDir$/$FileName$.

    Like

  2. […] Run PHP CodeSniffer code beautifier and fixer from PhpStorm […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

WordPress.com VIP: Enterprise content management platform

Our fully managed cloud platform, expert guidance and support, and diverse partner ecosystem free you to focus on your business objectives.

Making the Complex Simple

Software Development from John Sonmez's Perspective

When I Have Time by Sara Rosso

Tech Tips, Biz Info and How-Tos to Bridge the Gap between Meek and Geek

Job Tips For Geeks

career advice for programmery people...

Sarfraz Ahmed's Blog

PHP, MySQL, jQuery, JavaScript, Ajax, CSS, HTML and more

Moving Target

Technology, Productivity & Lessons learned starting a startup.

mjtoolbox

MJ's Technical Blog

Henrik Warne's blog

Thoughts on programming...

Coding Adventures

A place to share my coding experiences

%d bloggers like this: