Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom ruleset for phpcs using PHPStorm

I'm trying to somehow convince the phpcs to use the Zend coding guidelines, -but- to allow me to have variables with underscores (like $blub->foo_bar).

Could anyone tell me how to change the ruleset that phpcs uses from within PHPStorm, please?

like image 207
user1293263 Avatar asked May 14 '12 14:05

user1293263


1 Answers

You'll need to create a custom coding standard if you want to override the built-in ones. This is just a matter of creating an XML file with the standard's definition in it.

In your case, you want the whole Zend coding standard, but you don't want the particular naming convention rule about variable names. So you would create a file called mystandard.xml (name it whatever you want and put it where ever you want) and put in the following contents:

<?xml version="1.0"?>
<ruleset name="MyStandard">
 <description>My custom Zend coding standard.</description>
 <rule ref="Zend">
  <exclude name="Zend.NamingConventions.ValidVariableName"/>
 </rule>
</ruleset>

There is a lot more you can do to customise your standard, including bringing in checks from other standards, overwriting messages or muting some of them. You can take a look at the docs here: http://pear.php.net/manual/en/package.php.php-codesniffer.annotated-ruleset.php

Last I checked, PHPStorm didn't let you use custom coding standards, for whatever reason. Normally, you'd just type in the name of the standard as the full path to the mystandard.xml file. If PHPStorm still doesn't let you do that, the only way to trick it is to actually install your custom standard.

Note that this is not something you normally need to do and it is a bit complicated. But if you want to try it, this is how you do it:

  1. Find where PHPCS is installed on your system. If you don't know where that is, try pear config-show | grep php_dir. PHP_CodeSniffer will be in a directory under there. For me, PEAR is installed in /usr/local/PEAR, so PHP_CodeSniffer is found at /usr/local/PEAR/PHP/CodeSniffer
  2. Create a new directory called MyStandard under /usr/local/PEAR/PHP/CodeSniffer/Standards
  3. Put your mystandard.xml file directly in there but rename it to ruleset.xml

To confirm your standard has been installed, run phpcs -i. You should see

The installed coding standards are MySource, MyStandard, PEAR, PHPCS, Squiz and Zend

Now, PHPStorm will show (hopefully) MyStandard as an option in the drop-down. If it doesn't show up, then they've probably hard-coded the list and there wont be any way to make it work without hacking up the Zend coding standard directly.

I hope that information helps find you a solution. If not, you can always run PHPCS directly on the command line using your custom standard:

phpcs --standard=/path/to/mystandard.xml /path/to/code
like image 164
Greg Sherwood Avatar answered Sep 29 '22 11:09

Greg Sherwood