Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good standard to use with PHP_CodeSniffer?

My codebase adheres (or should) to the Zend Coding Standard. I've been using the Zend standard with PHP_CodeSniffer, but I see discussion saying the Zend standard for phpcs is inconsistent and unmaintained and doesn't necessarily stick to the Zend Coding Standard. So I'm looking for a standard that's more consistent, current, and broadly used. What should I use?

I've heard of "Squiz", "PEAR", and "PSR2", but I don't know anything about them. What are the differences between them?

like image 504
Brian Kendig Avatar asked Jan 22 '14 16:01

Brian Kendig


People also ask

What is Squizlabs PHP_CodeSniffer?

GitHub - squizlabs/PHP_CodeSniffer: PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.

How do I use CodeSniffer?

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.

What is a code sniffer?

Code Sniffer is a static test that uses static code analysis to detect violations of the Magento Coding Standard to prevent common coding errors.


1 Answers

The PEAR standard is the most basic, and the standard that most others evolved from. It's been around for a long time, but hasn't been updated in quite a while. It was originally used by PEAR packages (like PHP_CodeSniffer) and I think it is a good beginner standard.

The PEAR standard contains 26 main checks.

The PSR2 standard is relatively new and has been developed by the PHP-FIG. It is being adopted in various forms by many frameworks. I think this is the standard to choose if you want to adhere to one and you really have no idea what to pick.

The PSR2 standard contains 40 main checks.

The Squiz standard is the one I use at work. I developed PHP_CodeSniffer, so it might not be surprising that the standard I use is very strict. This one also contains checks for JS and CSS files because it is used on our large web applications. It is definitely not a beginner standard and it changes regularly. But it contains a lot of good checks that can be incorporated into a custom coding standard.

The Squiz standard contains 124 main checks.

So my suggestion for you is to try out the PSR2 standard and see how you go. If you start feeling like it is not working for you in small ways, you can take a look at creating your own ruleset.xml file to create your own custom coding standard. There are docs for that process here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml

like image 159
Greg Sherwood Avatar answered Oct 12 '22 15:10

Greg Sherwood