How useful is PHP CodeSniffer? Code Standards Enfo

2019-03-07 21:25发布

I'm dabbling with the idea of setting up PHP CodeSniffer on our continuous integration server in an effort to improve the quality of our code-base. After reading the documentation I'm very excited about the idea of normalizing and enforcing our coding standards. However, I'm left wondering about the actual improvement to our product. I'm well aware that the sniffer only detects violations to a defined coding-standard but what type of benefits does a clean, consistent, code-base provide? Is it worth the extra work to refactor a project with 100k+ lines of code to conform to the PEAR standard?

For those who are not familiar with PHP CodeSniffer or code-smell in general, here is an example output:

FILE: /path/to/code/myfile.php
FOUND 5 ERROR(S) AFFECTING 2 LINE(S)
--
2 | ERROR | Missing file doc comment
20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE"
47 | ERROR | Line not indented correctly; expected 4 spaces but found 1
51 | ERROR | Missing function doc comment
88 | ERROR | Line not indented correctly; expected 9 spaces but found 6

Strictly speaking, the user/client would not notice any difference in a product that was refactored to be standards-compliant but I'm wondering if there are other hidden benefits

Right now our code is by no means sloppy and we try to follow our own personal standards which, for the most part, are derived from Pear's Coding Standards but a trained eye can spot the differences.

So my question is how much do they improve the quality of a product. What kind of latent benefits resulted from it?

Am I just being obsessive-compulsive with my desire to move our product closer to a set of standards? Would it be worth it? If so, What kind of strategy did you use to implement the code-sniffer and correct the subsequent violations that were detected?

8条回答
狗以群分
2楼-- · 2019-03-07 21:46

Note that if you are using Eclipse or Zend IDE you can benefit from automatic tools that makes the respect of a standard less costly. You can also use a continuous integration tool like Hudson or PHPUndercontrol.

  • PDT is a cool editor for PHP
  • PDT-Tools are some plugins for PDT with an automatic formatting tool
  • DTLK (Dynamic Toolkit Library) can be used to launch some external scripts to check your files.

You can also have a look at PHP Checkstyle which I think is easier to configure (disclaimer : I've worked on it)

Some other tools are listed on the "documentation" page of the web site.

查看更多
不美不萌又怎样
3楼-- · 2019-03-07 21:52

Count me in among those who evangelise CodeSniffer. After being highly sceptical for years, I now use it on every project I'm working on. Why?

As Grace Hopper and/or Andrew Tanenbaum famously said,

The wonderful thing about standards is that you have so many to choose from.

Likewise, it's almost always a Bad Idea™ to create your own coding standard; creating one comprehensive enough to cover all your code is hard, and, much more importantly, it will not be to the liking of the next person to maintain your code, who will try to "improve" your standard so that it suits his long-held coding style. Adopting an appropriate outside standard, whether it's Zend or PEAR or Kohana or JoeBobBriggsAndHisFifthCousin, allows you to concentrate on the content rather than the formatting. Better still, tools like PHP CodeSniffer either support the standard "fresh out of the tin" or those who have gone before have almost certainly implemented support as an add-on.

Mixing coding standards with existing code not written to that standard will give you fits, unless you adopt two simple, complementary rules.

Exclude files which predate your adoption of the coding standard from being checked, through the --ignore command-line option or equivalent configuration-file settings. But, when you do modify any part of a source file, update the entire file to comply with your chosen standard.

I just wrote a new blog post about this sort of thing.

查看更多
登录 后发表回答