Question

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?

Was it helpful?

Solution

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

OTHER TIPS

There are small differences and it's mostly a matter of how you are used, or what code standard is enforced by someone else.

PSR2 has been adopted by most frameworks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top