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?
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:
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
MyStandard
under /usr/local/PEAR/PHP/CodeSniffer/Standards
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With