PHP Code Sniffer is a tool to detect violations of a defined coding standard such as PSR2. Read more about it here: https://github.com/squizlabs/PHP_CodeSniffer
If you would also like to check out the accompanying YouTube video, here’s the link:
Now let’s go ahead and set it up.
- Add Code Sniffer to composer.json file
First we need to include a dependency for squizlabs/php_codesniffer in our composer.json file. For example:
{
"require-dev": {
"squizlabs/php_codesniffer": "3.*"
}
}2. Install the dependency through PHPStorm’s interface
Then let’s go to our project’s composer.json file in PHPStorm, and click “Install”.
Press enter or click to view image in full size
This should run composer update for us and also setup PHP Code Sniffer(PHPCS) to our project.
3. Enable Code Sniffer and select coding standard
Once that is complete we’ll have to enable PHPCS through the settings. File > Settings > Editor > Inspections > PHP > Quality Tools > PHP Code Sniffer Validation. Let’s tick the box and select our coding standard. If you do not see any options under coding standard, click on the “Refresh” icon on its right side. It will then populate the drop down with default coding standards. We can then select our desired coding standard using the drop down menu.
Press enter or click to view image in full size
If in case you get an error while clicking the “Refresh” icon. You’ll have to select the path to PHPCS manually.
Press enter or click to view image in full size
Check the optional step 4 below for that.
4. (Optional step, only do this if you get the error above) Add Code Sniffer Path to PHPStorm
To set PHPCS manually, let’s go to:
File > Settings > Languages & Frameworks > PHP > Code Sniffer
There, under Configuration, click on the ‘…’ icon on the right.
Press enter or click to view image in full size
A window will pop up. Then under “Code Sniffer path:” click on the folder icon on the right to open another window to select the path to phpcs executable.
Press enter or click to view image in full size
Navigate to : your-project’s-root-folder/vendor/bin/phpcs
Select phpcs executable if you are on Linux/Mac and select phpcs.bat if you’re on Windows. Then the “Ok” button will light up. We can now click on it.
Then click “Appy” and “Ok” to close the previous Code Sniffer popup window.
Now we can go back to step 3 and click the refresh button again. The coding standards should be listed on the dropdown. Select one and we’re ready to go.
5. Enjoy the PHP Code Sniffer integration in PHPStorm!
Now, it should give us a line by line analysis of the coding standard violations in the editor. We’ll get a yellow underline if it violates any rules. If we hover over the line, we’ll get the exact violation message.
Press enter or click to view image in full size
Congratulations! We’ve set up PHPCS on PHPStorm! Yuh-hoo!
Press enter or click to view image in full size
If you liked the article, give claps! :) Also feel free to share it with your friends in Facebook/Twitter.
My other series on medium:
Cheers! :)