Squiz Labs Blog - The latest news from the R&D division of Squiz®

Subscribe to our RSS feeds

Analysis of Coding Conventions

Coding standards tend to vary between projects. Even projects that use the same written standard can vary in a number of ways as standards tend to leave a lot of room to apply your own coding style in various areas. To get a better idea of what coding styles PHP developers are using, I used PHP_CodeSniffer to analyse and report on a number of big and small public PHP projects hosted on Github.

The result of the analysis is here: http://squizlabs.github.io/PHP_CodeSniffer/analysis/

There are currently 63 projects being analysed, with trend data recorded for about the last 8 months. The report currently lists 42 different coding style conventions ranging from "how many spaces surround the concat operator" to the classic "tabs or spaces for line indent". The coding conventions are listed from most contentious to least contentious. So the ones at the top show the most variations between projects while the ones at the bottom are generally accepted by all projects.

Each coding convention provides a list of projects that use each of the style variations within it. Clicking on any of those projects will take you to a project-specific report showing the coding conventions used within it and how often the same style is followed within the project. So you can take a look at the coding conventions for CakePHP, Drupal, Symfony2 or even PHP_CodeSniffer itself. 

How did you pick the projects to analyse?

A combination of popular PHP projects on Github, projects that I've used, and projects that have received some attention while the report was being built.

Can you include my project in the report?

If it is a public PHP project on Github, I sure can. All the repositories that are being analysed are listed in the repos.json file.

For each one, I need to know the name of the project, the URL (organisation/repository), the path in the project where PHP_CodeSniffer should start analysing code (default: /), any files or directories that should be ignored (default: none), and a list of additional PHP file extensions to check beyond .php and .inc (default: none). Here is an example:

    "name": "My Project",
    "url": "squizlabs/my-project",
    "path": "src",
    "ignore": "3rdparty/*,includes/*",
    "extensions": "phtml,module"

There are also 63 other examples in the repos.json file to take a look at. Submit a PR or send me a gist on Twitter. If you have no idea what the values should be, get me the Github URL and I'll figure it out for you.

How is the report generated?

From version 2.0.0a2, PHP_CodeSniffer collects metrics about the code it is checking. These are used to determine coding conventions and can be displayed using --report=info. The coding convention report uses a custom PHP_CodeSniffer report that outputs these metrics as a JSON file instead of to the screen. To save a lot of time, a custom coding standard is used to limit checks to just the sniffs that generate the metrics that the report wants to use. Some ugly custom wrapper scripts deal with updating the repostories, keeping trend data for each coding convention, and generating the HTML reports. The whole thing runs through HHVM to make it as fast as possible.

Stay up to date on all PHP_CodeSniffer changes, including new features and releases, by subscribing to the RSS feed or following me on Twitter.

Squiz Labs

R & D division of Squiz Pty Ltd

Open source web experience management solutions

Squiz Labs is the research and development division of Squiz, the company behind the Squiz Suite of web experience management and development tools.

Our PHP and JavaScript developers are responsible for producing innovative enterprise-quality software while our interface designers and testing team ensure our products both look great and are easy to use.