Skip to content
PHP

Configuring PHP CodeSniffer Per Project for Different Coding Standards in Sublime Text 3

3 min read

I’m a keen advocate for following coding standards. They help maintain consistency in code, especially when developing with others, and can generally help you to write better code. I work with Sublime Text for developing in PHP and have it set up to check against whichever coding standard I am currently using as I code. For PHP development the tool to use is PHP CodeSniffer which I’ve previously written about setting up for Sublime Text 3 on this site.

I work with a lot of different projects using different frameworks like CakePHP and Laravel. As a result I regularly find myself having to switch between different coding standards. This is relatively simple to do using the phpcs package for Sublime Text; you can right-click in a file and select ‘PHP Code Sniffer’ then ‘Switch coding standard’. However, you can make this a little easier by configuring projects to override the default settings for phpcs.

Overriding the Default Standard

Every project you create in Sublime Text has a configuration file named something like my-project.sublime-project. You can edit this whilst in the context of a plugin by going to ‘Edit Project’ from the ‘Project’ menu.

To start with it will probably look something like this:-

{
	"folders":
	[
		{
			"path": "/var/www/vhosts/my-project"
		}
	]
}

We can override a package’s settings for the specific project in this file. So let’s say that by default we’ve got phpcs setup to use the PSR2 standard, we can switch the current project to the CakePHP standard like this:-

{
	"folders":
	[
		{
			"path": "/var/www/vhosts/my-project"
		}
	],
	"settings":
	{
		"phpcs":
		{
			"phpcs_additional_args": {
				"--standard": "CakePHP",
				"-n": ""
			},
		}
	}
}

The value of --standard can also be an absolute path to a ruleset.xml file if you need to load a specific standard that is not directly installed with the code sniffer.

Changing the Path to phpcs

If we need to change the executable path for phpcs for the project we can do that too:-

{
	"folders":
	[
		{
			"path": "/var/www/vhosts/my-project"
		}
	],
	"settings":
	{
		"phpcs":
		{
			"phpcs_executable_path": "/var/www/vhosts/my-project/vendor/bin/phpcs",
			"phpcs_additional_args": {
				"--standard": "CakePHP",
				"-n": ""
			},
		}
	}
}

This is useful when you’re using a coding standard that might require a different version of phpcs. For example, the CakePHP 2 coding standard requires an older version than the newer CakePHP 3 standard. If we require the coding standard via Composer for the project we can handle this using Sublime Text’s project specific settings.

Final Comments

It’s important to note that when you make any changes to override the default phpcs settings in a project file you will need to close the project and re-open it before they get registered. Inconvenient but it works!

You can override any of the phpcs settings in your project file, not just the two described here.

Hopefully this has shed some light on how you can easily work with multiple PHP coding standards in Sublime Text 3. Happy coding!

© 2024 Andy Carter