development (in)action

Cleaning Up Your Product

I recently made the embarrassing mistake of creating a push request to a well respected PHP author’s project and to my horror I saw he had to ‘detabify’ my push. I had recently reset my Zend Studio workspace and neglected to reconfigure the default format standard from PHP => Zend.

Once I sorted out my studio client, I began to look into ways to test this all before it’s even committed, to insure I have not overlooked a detail. To help with syntax, formatting and style, I have employed PHP_CodeSniffer, a PEAR package. Its use is simple and straightforward with informational documentation; Rulesets or Sniffs are varying and customizable in the form of a ruleset xml file. Available Sniff Collections are grouped as follows:

Each brings its own standards ruleset for customization. I have chosen to implement the Zend Sniff in  the form of a file named phpcs.xml, I exclude certain file types .js, .css all test files, data files and, for good measure, .svn directories.

I place this file in a directory inside /tests/build/ where you place your file strictly depends ons on your personal preferences. To install PHP_CodeSniffer execute the following commands:

There are several choices of reports, for the purpose of command line reporting I choose one or all:

  • source [provides a description of what rules the violations are related]
  • full [detail of all violations in the code base, line of violation and rule
  • summary [ a high level assessment of all rule violations]

Examples of the report type’s outlines and the associated commands to produce each with the afore mentioned rules file.

[gist id=”965105”/]

While it’s possible, the report will be sizable depending on the projects size; however this is an opportune time to begin cleaning up your existing code base; but more importantly, it gives you the tools required to insure your code is clean, consistant and within community expectations.

If you, your project or employer have your own specification of code standards, I would encourage you to create your own rule set in combinations. Should the existing sniffs not be sufficient, it’s advised to create your own sniffs to accommodate your needs.

The PHP_CodeSniffer project provides an annotated xml ruleset file to serve as a guideline and tutor; as well as the documentation associated with its use.