Coding Standard Standard Sniffer and Fixer

165 views
Skip to first unread message

Stéphane Mourey

unread,
Apr 10, 2013, 10:56:28 AM4/10/13
to php-f...@googlegroups.com
Hi all,

I was aware from sometimes that a tool named "PHP Coding Standard Fixer" (http://cs.sensiolabs.org) exists and, while evaluating it, I googled to find a ruleset for the PSR rules. I discovered that there where some, so some questions came to my mind, and I would like to share them with, so I may get some useful answer :
  • Did you use the "PHP Coding Standard Fixer" ? How good it is at its job ?
  • Which is the most mature effort to translate PSR into PHPCS ruleset, i.e. which should I use ? Which works the better with "PHP Coding Standard Fixer" ?
  • If the "PHP Coding Standard Fixer" is a good tool, did someone think to extend it to make it a generic tool, accepting more standards like PHPCS do ?

Thanks a lot for your answers.

Best regards,

Stéphane Mourey

Greg Sherwood

unread,
Apr 10, 2013, 11:47:22 PM4/10/13
to php-f...@googlegroups.com

On Thursday, 11 April 2013 00:56:28 UTC+10, Stéphane Mourey wrote:
  • Which is the most mature effort to translate PSR into PHPCS ruleset, i.e. which should I use ? Which works the better with "PHP Coding Standard Fixer" ?
PHP_CodeSniffer comes with standards for PSR1 and PSR2. I've tried my best to ensure these are as accurate as possible, so I'd suggest using them:

phpcs --standard=PSR1 /path/to/code
phpcs --standard=PSR2 /path/to/code

PSR2 includes the whole of PSR1 as well, so you only have to run the PSR2 standard over your code if you are trying to follow that one.

I don't use the PHP Coding Standard Fixer, but I can tell you that the 2 projects are unrelated and there is no communication between the developers. Based on feedback I have received, I don't think you should expect the Fixer to actually fix all the errors reported by PHP_CodeSniffer. And based on the bug reports, I think you should probably review the changes it makes fairly carefully, or just make the changes yourself based on the errors reported from PHP_CodeSniffer. 

Beau Simensen

unread,
Apr 11, 2013, 10:53:24 AM4/11/13
to php-f...@googlegroups.com
On Wednesday, April 10, 2013 10:47:22 PM UTC-5, Greg Sherwood wrote:
I don't think you should expect the Fixer to actually fix all the errors reported by PHP_CodeSniffer.

It does a decent job with the things it knows about but I think this is a pretty accurate statement. The version I use (a little old) did not seem to have nearly the same coverage that phpcs does.

 
I think you should probably review the changes it makes fairly carefully, or just make the changes yourself based on the errors reported from PHP_CodeSniffer. 

This is pretty sane advice. When I use it, I generally do so after all of my changes are staged so that I can easily run a `git diff` to review the changes it makes. This also gives me the opportunity to flush those changes somewhat easily if I reject them.


If the "PHP Coding Standard Fixer" is a good tool, did someone think to extend it to make it a generic tool, accepting more standards like PHPCS do?

There are some built in ways to use just the fixers you want to use. You can look at --levels and --fixers options to see what can be done to make your own set of fixers for your own tastes. It doesn't mention it on the page, but a little digging shows how you can make your own fixers, too, to extend it a little more if you need to:


"AFAIK PHP-CS-Fixer is intended to fix issues defined in PSR only. See PR #118 which made it possible to easily add custom fixers. Just extend \Symfony\CS\Fixer::registerBuiltInFixers() and you are done." ( from https://github.com/fabpot/PHP-CS-Fixer/pull/124#issuecomment-7357266 )

Reply all
Reply to author
Forward
0 new messages