Set Up PHP CodeSniffer for Local Development

January 10, 2020
Written by
Michael Okoko
Contributor
Opinions expressed by Twilio contributors are their own

PHP_CodeSniffer (PHPCS) is a tool that validates your code against a set of predefined standards and ensures that such standards are maintained across the team. This tutorial will walk you through automating those validations during development by setting up PHPCS on Sublime TextVisual Studio Code, and PHPStorm.

The accompanying sample project is available at https://github.com/idoqo/twilio-greeter. You can also download the phpcs.xml to use in your existing project.

Prerequisites

Completing this tutorial requires the following prerequisites:

Installing PHP Code Sniffer

All of the editors below require PHPCS to be pre-installed, and since we plan to use it across projects, we will install it globally with the following command:

$ composer global require "squizlabs/php_codesniffer=*"

You can check your installation with:

$ phpcs

Setting up PHPCS with Sublime Text

The Sublime Text package repository has two plugins that can be used to interface with PHPCS: SublimeLinter-phpcs and Phpcs. We are going to use the standalone Phpcs plugin as it offers more configuration options.

Launch package control with (Shift+Ctrl+P or Shift+Cmd+P). Select “Package Control: Install package” from the popup menu and search for “Phpcs”. The top result is what we are looking for as shown in the image below:

 

Sublime Text Editor 3 Extensions

Next, we will set up the package configuration file by going to Preferences > Package Settings > Settings - User from the Menu Bar, paste the code block below into the newly created phpcs.sublime-settings and save it.

{  
   // full path to our phpcs binary,   
   // replace "YOUR_HOME_DIRECTORY" with the path to your home folder  
   "phpcs_php_path": "/home/idoko/.config/composer/vendor/bin/phpcs",  
  
   // run phpcs on every PHP file after we save it.  
   // you can ignore folders by excluding them in the phpcs.xml config file  
   "phpcs_execute_on_save": true,  
  
   // mark lines that have violated the rules  
   "phpcs_show_gutter_marks": true,  
  
   // ignore blade templates, you can add more file extensions in the array  
   // Do not execute for twig files  
    "extensions_to_blacklist": ["blade.php"],  
  
   // you can even pass additional arguments to phpcs  
    "phpcs_additional_args": {  
        "-n": ""  
    },  
}

For a complete list of all the possible config options, you can look up the example settings in the plugin source code.

The next time you modify a PHP file and save, PHPCS should run automatically and report any violations that occur in the popup menu.

Setting up PHPCS with Visual Studio Code

Using PHPCS on VS Code is relatively easy as the vscode-phpcs plugin does a lot of heavy-lifting for us. To install the plugin:

  • Open the Quick Open dialog on VS Code (with Ctrl+P or Cmd+P)
  • Type “ext install phpcs” to find the extension and
  • Click the cloud icon to install.

Once installed, restart VS Code and the plugin will automatically search your global composer path for a PHPCS installation. Note that it also searches your project root for existing rulesets (which is the purpose of the phpcs.xml file in our sample project).

Now, open a PHP file you want to sniff. Red lines should appear in all the places with violations as shown below:

 

VS Code PHPCS errors

Setting up PHPCS with PHPStorm

PHPStorm natively supports code inspection with PHP_CodeSniffer, though configuring it is quite some work.

First, launch the Settings dialog (Ctrl+Alt+S) and navigate to Languages & Frameworks > PHP > Quality Tools. Expand the PHP Code Sniffer on the Quality Tools page and select Local from the Configuration dropdown. Click the “three dots button” beside the dropdown highlighted below:

 

PHP Storm Code Sniffer dialog

Specify the full path of the PHPCS executable in the new dialog that opens (which is $YOUR_COMPOSER_BIN_PATH/phpcs). You can click the Validate button to confirm it’s working and click “Apply” when you are done.

PHP Storm with Code Sniffer working

You should now see a different error on the Quality Tools page telling you that CodeSniffer inspection is not enabled.

Code inspections are how PHPStorm detects (and corrects) problems such as dead code, misspellings, and of course, code style violations in your project.

In the Settings dialog, go to Editor > Inspections. From the inspections screen, expand the PHP | Quality tools node and enable “PHP CodeSniffer validation”.

In the configuration pane that is now enabled, select “Custom” from the “Coding standard” dropdown, locate the ruleset configuration (phpcs.xml in our project directory), and apply your changes.

PHP Storm with successful PHCS sniffing

This way we can specify our preferences in the phpcs.xml file and have it applied across our project (irrespective of the tool we are using). Our code is now automatically being checked against our preferred standard and errors originating from PHPCS will be prefixed with phpcs.

PHP Storm with PHPCS checks

Conclusion

PHP Code Sniffer helps achieve uniformity of code styles and having your editor automatically check for violations goes a long way in improving the quality of your codebase.

I hope you enjoyed the post and if there are issues or questions, please feel free to reach out to me via Email or on Github.