Tito Miguel Costa
Refactoring ideas

Coding Standards

PHP_CodeSniffer, PHP-CS-Fixer and NetBeans to the rescue

Since I'm mostly a Symfony programmer, I try to follow as much as possible their coding standards, although I do not sympathize with some of their decisions.

There are mainly two points I disagree, for me:

  • A line break before braces in control structures increase readability (already happens on classes and methods)
  • I prefer to use underscores for the name of variables instead of the camel case notation

Sometimes I forget to follow the standards and it is boring to check back the code and fix it. Fortunately, there are a few tools that can help.

PHP_CodeSniffer is a pear package that analyzes source code and reports the errors.

One limitation of PHP_CodeSniffer is that it doesn't automatically fixes the errors reported, PHP-CS-Fixer for the rescue.

PHP-CS-Fixer is a phar file that complements PHP_CodeSniffer, fixing our source code taking into consideration the standard that project follows.

For last, there is NetBeans, with a simple ALT+SHIFT+F, it automatically formats code. You can download my whole configurations and import them in Tools > Options or use just this file just for PHP and Editor configurations (from NetBeans 7.2).

With these three tools, there's no reason not to adopt a conding standard and implement it in your PHP projects.

8 December, 2012
Tools, NetBeans, Code