Force PHP coding standards
Following coding standards when you are writing a piece of code is a good practice. Either we are talking about a tiny or a huge project where many people gets involved, coding standards are important – especially when other people will get to read your code. Code standards ensure that any project follows the same patterns and conventions and do not inherit the habits of their developers. Based on my experience, many projects lack of such standards and as a result many files, classes and methods follow entirely different patterns making the code reading really hard.
There a lot of tools available that can assist you to force coding standards but first of all you will have to define your project coding standards. You may consider PEAR, WordPress or Zend Framework PHP coding standards. If you are contributing to an open source application like WordPress or Drupal, it goes without saying that you have to follow the corresponding project standards.
NetBeans is a powerful IDE, recently added support for PHP. It allows you to define almost every option for your code including tabs and indents, alignment, braces, blank lines, spaces and wrapping. If you are really paranoid about your code formatting NetBeans is your way to go. Editor formatting options are located under Preferences > Editor > Formatting. According to your setup, you may have one or more languages available to configure. Preview is also available when changing formatting preferences and of course you can always format any file according to your formatting options ( Source > Format ). Really helpful if you are dealing with a bad formatted file.
Coda is the elegant text editor from Panic available only for Mac OSX. Code formatting support is provided via an excellent plugin called Coda PHP & Web Toolkit written by Mario Fischer. The plugin is very easy to install but when it comes to configuration you may find yourself editing some php files. Unfortunately, the plugin does not provide as many options as the NetBeans via its interface but you can adjust it to your needs by editing
phptidy-coda.php. Eoin Gallagher has compiled a set of instructions on how to configure Coda web toolkit according to WordPress code standards.
PHP Tidy is a tool for formatting PHP code. PHP Tidy default formatting options are based on PEAR standards but those can be changed – like Coda – via phptidy.php. Formatting an existing file can be achieved by executing the following command
phptidy.php replace yourfile.php . More information on PHP Tidy can be found at the official website.
Whatever tool you choose to assist you, you should always have in mind that such tools are usually capable to maintain only a subset of the available coding standards. For instance, they may not force Yoda conditions or even the usage of OOP Patterns. The usage of such standards relies entirely on the developers.
- Previous post: Upgrade your WordPress theme
- Next post: