CodeSniffer Part 1: Introduction to CodeSniffer

This started as a quick blog post on my way home. I was drunk, and shouldn’t have been blogging. I’d been to the leaving do of two of my great colleagues at Yahoo!

It ended up way too long,  so I have split it up into sections and will post as a series.

This is a rough transcript of a lightning talk I gave during a team meeting at Yahoo! completely off the cuff, but I am in the process of writing this up as a Yahoo! Tech Talk, and also, as this is not internally sensitive, like most of the stuff I’ve been working on for the last two years, I’ll see if it can be release on Yahoo Developers Network.

What is CodeSniffer?

CodeSniffer is almost a SCA tool. It allows a set of rules or a Standard to be applied to source code.

These rules can be used to detect common programming errors or Anti Patterns, it can also be used to define a set of Coding Standards  for your project.

It is down to your project to come up with some Coding Standards or apply a pre-existing set of standards for your team to adhere to.

CodeSniffer comes with a set of coding standards already defined that are regularly used by other teams.

These are:

  • MySource
  • PEAR
  • PHPCS
  • Squiz
  • Zend

Analysing your code using one or more of these standards will highlight how the code you have provided stacks up against the project’s coding standard.

How to install CodeSniffer

$ sudo pear install PHP_CodeSniffer

This will download and install the CodeSniffer utility from the PEAR repository.

How to run CodeSniffer

Simply run:

$ phpcs --standard=<standard> <path to file or directory>

You will be shown a list any errors or warnings in your code.

CodeSniffer will also exit with a non-zero status code. This can be useful if you are using a Makefile to test your code or some build tool like Phing, or if your CodeSniffer rules are being run as part of a Continuous Integration system.

In the next post I will run through some of the standards and reports.

This entry was posted in Development, PHP, Software and tagged , , , . Bookmark the permalink.

6 Responses to CodeSniffer Part 1: Introduction to CodeSniffer

  1. Tragan says:

    Great tip on CodeSniffer. We are always looking at SCA tools to incorporate in our base product.

    And thxs for breaking this up as a series. Easier to consume that way.

  2. Bob says:

    Hey, no problem, my workload has gone slightly nuts recently.

    I’ve one more, the final in the series, to add.

    I’ll try and get it finished off shortly.

  3. Canon 400D says:

    I am lucky I did because now Ive obtained a whole new view of this. Your weblog is definitely worth a read if anybody comes throughout it.

  4. Pingback: PHP CodeSniffer articles | Learn To Do Stuff Good

  5. Pingback: Why You Need Code Sniffers for Web Development | Technology Today World

  6. Pingback: Why You Need Code Sniffers for Web Development – Android

Leave a Reply to Bob Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.