What is a good standard to use with PHP_CodeSniffer?

My codebase adheres (or should) to the Zend coding standard. I use the Zend standard with PHP_CodeSniffer, but I see a discussion that the Zend standard for phpcs is incompatible and not supported and does not necessarily adhere to the Zend coding standard. Therefore, I am looking for a standard that will be more consistent, current and widely used. What should i use?

I heard about Squiz, PEAR, and PSR2, but I don’t know anything about them. What are the differences between the two?

+7
php codesniffer
source share
2 answers

The PEAR standard is the most basic and standard from which most others evolved. This has been a long time, but has not been updated for quite some time. It was originally used by PEAR packages (e.g. PHP_CodeSniffer), and I think this is a good standard for beginners.

The PEAR standard contains 26 main checks.

The PSR2 standard is relatively new and was developed by PHP-FIG. It takes many forms in many forms. I think this is the standard for choice if you want to stick to one, and you really don't know what to choose.

The PSR2 standard contains 40 main checks.

The Squiz standard is the one I use at work. I developed PHP_CodeSniffer, so it’s not surprising that the standard I use is very strict. It also contains checks for JS and CSS files, as they are used in our large web applications. This is definitely not the standard for beginners, and it changes regularly. But it contains a lot of good checks that can be included in a custom coding standard.

The Squiz standard contains 124 main checks.

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

+15
source share

There are slight differences, and it is mainly a question of how you are using, or what code standard is being applied by someone else.

PSR2 has been adopted by most frameworks.

0
source share

All Articles